ServiceNow Connector App Document

Business Rules Configuration for Task

Business Rules configuration is required for automating processes and enforcing business logic within the platform. It enables proper synchronization of data between ServiceNow and monday.com.

The ServiceNow administrator must complete the Business Rule configuration. Ensure accuracy while adding scripts and updating user details within the scripts.

The user must configure business rules for the following four incident scenarios:

Case 1 - Creating a Task in ServiceNow will automatically create a corresponding item in monday.com.

Case 2 - Updating a Task in ServiceNow will trigger an update to the related item on monday.com.

Case 3 - Uploading a Task in ServiceNow will upload the same attachment to monday.com.

Case 4 - Adding work notes in ServiceNow will synchronize the comments in monday.com.


Follow the below steps to create Business Rules for Tasks in your ServiceNow platform:

Step 1: Login to the ServiceNow account.

Step 2: Click on All and search Business Rules. Select Business Rules under the Administration option.

7.PNG

Step 3: Click on New.

8.PNG

Step 4: Add new business rules using the below steps.

Case 1: Creating a Task on ServiceNow should create an item on monday.
  1. Add name.

Example: monday_create_item_task

  1. Click on Table dropdown and select Catalog Task[sc_task].

  2. Click on the Advanced checkbox.

image-20250219-063700.png

Select the When to run tab and fill the below details in the fields.

  1. Click on the When dropdown and select the after option.

  2. Add value 100 to the Order field.

  3. Select the Insert checkbox.
    Add below Filter Conditions

  4. Click on the choose field dropdown and select Monday Intg Id.

  5. Click on Starts with dropdown and select is empty.

  6. Click on Update.

24.PNG
  1. Select the Advanced tab and add the below-given script.

10.PNG
  1. Copy the script below and paste it in the Advanced tab.

(function executeRule(current, previous /*null when async*/ ) {
               
    try {
        gs.log("Hello data");
       
    // Check if the business rule is executed by a particular user
    var targetUserId = '89899'; // Replace with the ID of the user you want to check
   
    // Get the ID of the current user
    var currentUserId = gs.getUser().getID();
    gs.log("Current Users "+current.currentUserId);
    // Check if the current user is the target user
    if (currentUserId == targetUserId) {
        // If the current user is the target user, just return without executing further logic
        return;
    }
       
        var request = new sn_ws.RESTMessageV2('MondayGraphQLAPI', 'fetchBoardData');
        var sourceData = "MondayServiceNowIntegration";
        var task_number = current.number;
        var task_sysId = current.sys_id;
        var task_short_description = current.short_description;
        var task_state = current.state;
        var task_urgency = current.urgency;
        var task_impact = current.impact;
        var snow_worknotes = current.work_notes.getJournalEntry(1);
        var task_monday_urgency = "";
        var task_monday_impact = "";
        var task_monday_state ="";
        var boardId="1975527574";
        if (task_state == 1) {
            task_monday_state = "Open";          
        }
        if (task_state == 2) {
            task_monday_state = "Work in Progress";          
        }
        if (task_state == 3) {
            task_monday_state = "Closed Completed";          
        }
        if (task_state == 6) {
            task_monday_state = "Closed Skipped";          
        }
        if (task_state == 7) {
            task_monday_state = "Closed Incomplete";          
        }
        if (task_state == 8) {
            task_monday_state = "Canceled";          
        }
       
        if (task_urgency == 1) {
            task_monday_urgency = "High";
        }
        if (task_urgency == 2) {
            task_monday_urgency = "Medium";
        }
        if (task_urgency == 3) {
            task_monday_urgency = "Low";
        }
        if (task_impact == 1) {
            task_monday_impact = "High";
        }
        if (task_impact == 2) {
            task_monday_impact = "Medium";
        }
        if (task_impact == 3) {
            task_monday_impact = "Low";
        }
        gs.log("The task_short_description" + task_short_description, sourceData);
        gs.log("The State is :" + task_state, sourceData);
        gs.log("The Urgency :" + task_urgency, sourceData);
        var task_description = current.description;
        request.setEndpoint(request.getEndpoint());
        gs.log("##The End Point :" + request.getEndpoint(), sourceData);
 
 
        var query_mutation = 'mutation ($item_name : String!, $columnValues: JSON!) { create_item ( board_id:'+boardId+', item_name: $item_name, column_values: $columnValues ) {  id name }}';
        var data_input = {
    "item_name": task_short_description ? String(task_short_description) : "Default task Name",
    "columnValues": JSON.stringify({
        "text_mknbrf8v": task_description ? String(task_description) : "",  // Replace 'long_text_column' with actual column ID
        "text_mknf5yp9": task_number ? String(task_number) : "",  // Replace 'task_number_column' with actual column ID
        "text_mknbnfjr": task_sysId ? String(task_sysId) : "",  // Replace 'integration_id_column' with actual column ID
        "color_mknhyj50": { "label": task_monday_state },  // Update 'status' column ID if needed
        "color_mkngejdb": { "label": task_monday_impact },  // Replace 'impact_status_column' with actual column ID
        "color_mkngamrz": { "label": task_monday_urgency }  // Replace 'urgency_status_column' with actual column ID
    })
};
 
 
 
        var reqbody = {
            'query': query_mutation,
            'variables': data_input
        };
 
 
        gs.log("The request body :" + JSON.stringify(reqbody), sourceData);
        request.setRequestBody(JSON.stringify(reqbody));
        var response = request.execute();
        var httpStatus = response.getStatusCode();
        var httpResponseBody = response.getBody();
        var responseObject = JSON.parse(httpResponseBody);  
        gs.log("The responseObject #:"+responseObject, sourceData);
   
        gs.log("The Status Code #:" + httpStatus, sourceData);
        gs.log("The Response Body #:" + httpResponseBody, sourceData);
        current.u_monday_intg_id = responseObject.data.create_item.id;
        gs.log("The monnday key #:"+current.u_monday_intg_id, sourceData);
        current.update();
 
        gs.log("####END OF task UPDATE FROM CREATE####", sourceData);
        var now_GR =new GlideRecord('sys_attachment');
        now_GR.addQuery('table_sys_id',current.sys_id);
        now_GR.query();
        while(now_GR.next()){
            var mondayAccountId="72353204";
            var mondayAccessToken="eyJhbGciOiJIUzI1NiJ9.eyJ0aWQiOjQ3NzQ5MTA3MiwiYWFpIjoxMSwidWlkIjo3MjM1MzIwNCwiaWFkIjoiMjAyNS0wMi0yNVQxMzoyMjowNi4wMDBaIiwicGVyIjoibWU6d3JpdGUiLCJhY3RpZCI6MjgwODY3NTMsInJnbiI6ImFwc2UyIn0.0UQXETAbZZfjwBWUAfZT9bFhQDETKJdoBzwgwEGAKWo";
            var mondayFileColumn="file_mkngvvdz";
            var mondayIntegrationIdColumnId="text_mknbnfjr";
            var body = {
                "boardId":"",
                "sys_id":"",
                "columnId":"",
                "itemId":"",
                "mondayToken":"",
                "accountId":"",
                "columnIdOfIntegrationIdInMonday":"",
            };
            body.boardId=boardId;
            body.sys_id=now_GR.getValue('sys_id');
            body.columnId=mondayFileColumn;
            body.itemId = current.getValue('sys_id');
            body.mondayToken=mondayAccessToken;
            body.accountId=mondayAccountId;
            body.columnIdOfIntegrationIdInMonday=mondayIntegrationIdColumnId;
            try{
                var payLoad=JSON.stringify(body);
                var requestOfAttachment = new sn_ws.RESTMessageV2('MondayGraphQLAPI','AttachmentToMonday');
                requestOfAttachment.setRequestBody(payLoad);
                var responseOfAttachment = requestOfAttachment.execute();
                var httpStatusOfAttachment = responseOfAttachment.getStatusCode();
                var httpResponseBodyOfAttachment = httpStatusOfAttachment.getBody();
            }catch(err){
                gs.log(JSON.stringify(err));
            }
        }
       
                                               
                // Add your code here
       
       
    } catch (ex) {
        var message = ex.message;
        gs.log("The error message " + message, sourceData);
 
    }
 
 
})(current, previous);
  1. After pasting the script, you have to replace the below details inside the script.

What to replace

Where to replace

mondayBoardId

Replace with your monday board ID on line number 30.

monday column Ids

Replace with your monday column Id’s on line number 79.

mondayAccountId

Replace with your monday account ID on line number 116.

mondayAccessToken

Replace with your monday My access token on line number 117.

mondayFileColumnId

Replace with your monday Files column Id on line number 118.

mondayIntegrationColumnId

Replace with your monday Integration Id column Id on line number 119.

  1. Click on the Save button to update the script.

11.PNG
  1. Click on Submit.

13.PNG
Case 2: Updating an incident on ServiceNow should update an item on monday.
  1. Add name.

Example: monday_update_item_task

  1. Click on Table dropdown and select Catalog Task[sc_task].

  2. Click on the Advanced checkbox.

image-20250219-073529.png

Select the When to run tab and fill the below details in the fields.

  1. Click on the When dropdown and select the after option.

  2. Add value 100 to the Order field.

  3. Select the Update checkbox.
    Filter Conditions

  4. Click on the choose field dropdown and select Monday Intg Id.

  5. Click on starts with dropdown and select is not empty.

  6. Click on Update.

image-20240729-055305.png
  1. Select the Advanced tab and add the below-given script.

15.PNG

Copy the script below and paste it in the Advanced tab.

(function executeRule(current, previous /*null when async*/ ) {
    // Check if the business rule is executed by a particular user
    var targetUserId = '997787887'; // Replace with the ID of the user you want to check
    gs.log("Executing Task Update Rule");
    // Get the ID of the current user
    var currentUserId = gs.getUser().getID();
    // Check if the current user is the target user
    if (currentUserId == targetUserId) {
        return;
    }
    try {
        var request = new sn_ws.RESTMessageV2('MondayGraphQLAPI', 'fetchBoardData');
        var sourceData = "MondayServiceNowIntegrationUpdateQuery";
        var task_number = current.number;
        gs.log("Task Number: " + task_number);
        var task_sysId = current.sys_id;
        var task_short_description = current.short_description;
        gs.log("Task Short Description: " + task_short_description, sourceData);
        var task_description = current.description;
        var monday_item_id = current.u_monday_intg_id;
        var task_state = current.state;
        var task_urgency = current.urgency;
        var task_impact = current.impact;
        var task_monday_state = "";
        var task_monday_urgency = "";
        var task_monday_impact = "";
        // Map task states to Monday.com
        if (task_state == 1) {
            task_monday_state = "Open";          
        }
        if (task_state == 2) {
            task_monday_state = "Work in Progress";          
        }
        if (task_state == 3) {
            task_monday_state = "Closed Completed";          
        }
        if (task_state == 6) {
            task_monday_state = "Closed Skipped";          
        }
        if (task_state == 7) {
            task_monday_state = "Closed Incomplete";          
        }
        if (task_state == 8) {
            task_monday_state = "Canceled";          
        }
        // Map urgency levels
        if (task_urgency == 1) task_monday_urgency = "High";
        if (task_urgency == 2) task_monday_urgency = "Medium";
        if (task_urgency == 3) task_monday_urgency = "Low";
        // Map impact levels
        if (task_impact == 1) task_monday_impact = "High";
        if (task_impact == 2) task_monday_impact = "Medium";
        if (task_impact == 3) task_monday_impact = "Low";
        gs.log("Monday Item ID: " + monday_item_id);
        var monday_id = JSON.parse(monday_item_id);
        var query_update_mutation = 'mutation ($columnValues: JSON!) {  change_multiple_column_values(item_id:' + monday_item_id + ', board_id:'+YourMondayBoardID+', column_values: $columnValues) { id }}';
        var data_update = {
            "columnValues": "{\"name\":\"" + task_short_description + "\" ,\"long_text__1\":\"" + task_description + "\" ,\"__task_number5__1\": \"" + task_number + "\", \"__integration_id4__1\": \"" + task_sysId + "\",\"status\":{\"label\":\"" + task_monday_state + "\"},\"status_1__1\":{\"label\":\"" + task_monday_impact + "\"},\"status_11__1\":{\"label\":\"" + task_monday_urgency + "\"}}"
        };
        var reqbody = {
            'query': query_update_mutation,
            'variables': data_update
        };
        gs.log("Query Check: " + query_update_mutation);
        gs.log("Request Body: " + JSON.stringify(reqbody), sourceData);
        request.setRequestBody(JSON.stringify(reqbody));
        var response = request.execute();
        var httpStatus = response.getStatusCode();
        gs.log("Status Code: " + httpStatus, sourceData);
        gs.log("Response Body: " + response.getBody(), sourceData);
        gs.log("#### END OF TASK UPDATE QUERY ####", sourceData);
    } catch (ex) {
        var message = ex.message;
        gs.log("Error Message: " + message, sourceData);
    }
})(current, previous);
  1. After pasting the script, you have to replace the below details inside the script.

What to replace

Where to replace

board Id

Replace with your monday board ID on line number 66.

monday column Ids

Replace with your monday column Id’s on line number 68.

  1. Click on the Save button to update the script.

11.PNG
  1. Click on Submit.

13.PNG
Case 3: Uploading an attachment on ServiceNow should upload the attachment on monday.
  1. Add name.

Example: 200_attachment to monday

  1. Click on the Table dropdown and select Attachment [sys_attachment].

  2. Click on the Advanced checkbox.

16.PNG

Select the When to run tab and fill the below details in the fields.

  1. Click on the When dropdown and select the after option.

  2. Add value 800 to the Order field.

  3. Select the Insert checkbox.
    Don’t select or add any values to Filter Conditions.

17.PNG
  1. Select the Advanced tab and add the below-given script.

18.PNG
(function executeRule(current, previous /*null when async*/ ) {

	//When inetgration user is doing this then it should not go to attachment process
	// Check if the business rule is executed by a particular user
    var targetUserId = '7676767'; // Replace with the ID of the user you want to check
 
    // Get the ID of the current user
    var currentUserId = gs.getUser().getID();
    // Check if the current user is the target user
    if (currentUserId == targetUserId) {
			gs.log('Intg Found');
// //         If the current user is the target user, just return without executing further logic
       return;
    }
		var mondayBoardId="Your monday Board ID";
		var mondayAccountId="Your monday Account ID";
		var mondayAccessToken="Your monday Service User ID Token";
		var mondayFileColumn="Your monday File column ID";
		var mondayIntegrationIdColumnId="Your monday IntegrationID column ID";
		var body = {
			"boardId":"",
			"sys_id":"",
			"columnId":"",
			"itemId":"",
			"mondayToken":"",
			"accountId":"",
			"columnIdOfIntegrationIdInMonday":"",
        };
		body.boardId=mondayBoardId;
		body.sys_id=current.getValue('sys_id');
		body.columnId=mondayFileColumn;
		body.itemId = current.getValue('table_sys_id');
		body.mondayToken=mondayAccessToken;
		body.accountId=mondayAccountId;
		body.columnIdOfIntegrationIdInMonday=mondayIntegrationIdColumnId;
		try{
			var payLoad=JSON.stringify(body);
			gs.log(payLoad);
			var request = new sn_ws.RESTMessageV2('MondayAttachmentRestMessage','ServiceNowMondayAttachement');
			request.setRequestBody(payLoad);
			var response = request.execute();
			var httpStatus = response.getStatusCode();
			var httpResponseBody = response.getBody(); 
		}catch(err){
			gs.log(JSON.stringify(err));
		}

})(current, previous);


  1. After pasting the script, you have to replace the below details inside the script.

What to replace

Where to replace

mondayBoardId

Replace with your monday board ID on line number 18.

mondayAccountId

Replace with your monday account ID on line number 19.

mondayAccessToken

Replace with your monday My access token on line number 20.

mondayFileColumnId

Replace with your monday board Files column Id on line number 21.

mondayIntegrationIdColumnId

Replace with your monday board Integration Id column Id on line number 112.

  1. Click on the Save button to update the script.

11.PNG
  1. Click on Submit.

13.PNG
Case 4: Adding work notes on ServiceNow should update a comment on monday.
  1. Add name.

Example: task_comments_to_monday

  1. Click on the Table dropdown and select Catalog Task[sc_task].

  2. Click on the Advanced checkbox.

image-20250219-084006.png

Select the When to run tab and fill the below details in the fields.

  1. Click on the When dropdown and select after option.

  2. Add value 100 to the Order field.

  3. Select the Update checkbox.
    Filter Conditions

  4. Click on the choose field dropdown and select Monday Intg Id.

  5. Click on starts with dropdown and select is not empty.

  6. Click on Add Filter Condition.

  7. Click on choose field dropdown and select Work notes.

  8. Click on starts with dropdown and select changes.

  9. Click on Update.

image-20240517-091051.png
  1. Select the Advanced tab and add the below given script.

20.PNG
(function executeRule(current, previous /*null when async*/ ) {
 
	// Check if the business rule is executed by a particular user
    var targetUserId = '8989999'; // Replace with the ID of the user you want to check
 
    // Get the ID of the current user
    var currentUserId = gs.getUser().getID();
    // Check if the current user is the target user
    if (currentUserId == targetUserId) {
        // If the current user is the target user, just return without executing further logic
        return;
    }
    try {
        // Add your code here
        var request = new sn_ws.RESTMessageV2('MondayGraphQLAPI', 'fetchBoardData');
        var sourceData = "MondayServiceNowIntegrationUpdateComments";
        var task_number = current.number;
        var task_sysId = current.sys_id;
        var task_short_description = current.short_description;
        var snow_worknotes = current.work_notes.getJournalEntry(1);
        var task_description = current.description;
        var monday_item_id = current.u_monday_intg_id;
        request.setEndpoint(request.getEndpoint());
 
 
		var query_update_mutation = 'mutation { create_update (item_id: ' + monday_item_id + 
			' , body: "' +  snow_worknotes + '") {id}} ';
 
        var reqbody = {
            'query': query_update_mutation,
        };
 
        request.setRequestBody(JSON.stringify(reqbody));
        var response = request.execute();
        var httpStatus = response.getStatusCode();
 
        gs.log("The Status Code " + httpStatus, sourceData);
        gs.log("The response body:" + response.getBody(), sourceData);
 
    } catch (ex) {
        var message = ex.message;
        gs.log("The error message " + message, sourceData);
    }
 
})(current, previous);
  1. Click on the Save button to update the script.

11.PNG
  1. Click on Submit.

13.PNG

After completing the business rule configuration for all four cases, users should proceed with the REST message configuration to finalize the setup for their ServiceNow account.

REST message configuration