Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

VaranAwesomenow
Mega Sage

Example Fix script on Incident :

var grInc = new GlideRecord('incident');
incSysId = '6be681e4334092107915af255d5c7b4a';
grInc.get(incSysId);
var servceId = grInc.business_service.toString();
var response = new CustomCSDMGlideAjaxUtils().getService(servceId);
var responseObj = JSON.parse(response);
var impact = responseObj.impact;
var group = responseObj.group.toString();
gs.print( " servceId= "+ servceId + " response= " + response +  "responseObj= " + responseObj + "impact=" + impact + " group=" + group);
 
 

This code is written in JavaScript and is used to interact with a ServiceNow instance. It retrieves information about an incident record and prints out some of its attributes.

Here's a breakdown of what the code does:

1. `var grInc = new GlideRecord('incident');`: This line creates a new GlideRecord object, which is a way to interact with data in ServiceNow. The record type is set to 'incident', which means it's an incident record.

2. `incSysId = '6be681e4334092107915af255d5c7b4a';`: This line sets the `incSysId` variable to a specific incident record's system ID.

3. `grInc.get(incSysId);`: This line retrieves the incident record with the specified system ID.

4. `var servceId = grInc.business_service.toString();`: This line retrieves the business service associated with the incident record and converts it to a string.

5. `var response = new CustomCSDMGlideAjaxUtils().getService(servceId);`: This line calls a method named `getService` on an object of type `CustomCSDMGlideAjaxUtils`, passing the `servceId` as a parameter. This method is likely to retrieve information about the business service.

6. `var responseObj = JSON.parse(response);`: This line converts the response from the `getService` method to a JSON object.

7. `var impact = responseObj.impact;`: This line retrieves the impact value from the JSON object.

8. `var group = responseObj.group.toString();`: This line retrieves the group value from the JSON object and converts it to a string.

9. `gs.print( " servceId= "+ servceId + " response= " + response + "responseObj= " + responseObj + "impact=" + impact + " group=" + group);`: This line prints out the values of the `servceId`, `response`, `responseObj`, `impact`, and `group` variables. The `gs.print` function is used to print out the values.

The output of this code would be the values of the incident record's business service, the response from the `getService` method, the JSON object, the impact, and the group.

 

Example Business rule on change request : 

 

(function executeRule(current, previous /*null when async*/ ) {
    //This BR is created to handle scenarios when change request is created from other task records such as incident or problem.
    //In all ther changes client script running on affected service auto populates impact and assignment group.
    // Add your code here
    var servceId = current.business_service.toString();
    var response = new CustomCSDMGlideAjaxUtils().getService(servceId);
    var responseObj = JSON.parse(response);
    var impact = responseObj.impact;
    var group = responseObj.group.toString();
    if (JSUtil.notNil(impact)) {
        current.impact = responseObj.impact;
    }
    if (JSUtil.notNil(group)) {
        current.assignment_group = responseObj.group.toString();
    }

})(current, previous);

 

This is a JavaScript code written in ServiceNow, specifically a Business Rule (BR) script. It's designed to execute a set of actions when a Change Request is created from another task record, such as an Incident or Problem.

Here's a breakdown of what the code does:

1. It defines a function `executeRule` that takes two parameters: `current` and `previous`. The `previous` parameter is null when the script is executed asynchronously.
2. The function checks if the Change Request was created from another task record by checking if the `current.business_service` property is not null. If it is, the script continues to execute.
3. It creates a new instance of the `CustomCSDMGlideAjaxUtils` class, which is a custom class that extends the `AbstractAjaxProcessor` class. This class is used to make AJAX calls to retrieve data from the ServiceNow instance.
4. The script calls the `getService` method on the `CustomCSDMGlideAjaxUtils` instance, passing the `servceId` (which is the ID of the business service associated with the Change Request) as a parameter.
5. The `getService` method returns a JSON response, which is parsed into a JavaScript object using the `JSON.parse` method.
6. The script extracts the `impact` and `group` values from the JSON object and assigns them to the `impact` and `assignment_group` properties of the Change Request record, respectively.
7. The script uses the `JSUtil.notNil` method to check if the `impact` and `group` values are not null before assigning them to the Change Request record.

In summary, this script is designed to populate the `impact` and `assignment_group` properties of a Change Request record when it is created from another task record. It uses a custom class `CustomCSDMGlideAjaxUtils` to make an AJAX call to retrieve the required data.

 

Example client script on problem:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    var service = g_form.getValue('business_service');
    var ga = new GlideAjax('CustomCSDMGlideAjaxUtils'); // getServiceInfo is the script include name
    ga.addParam('sysparm_name', 'getService');
    ga.addParam('sysparm_service', service); // set service

    /* Call GetUserInfo.managerName() with user set to Fred Luddy and use the callback function ManagerParse() to return the result when ready */
    ga.getXMLAnswer(ManagerParse);

}

function ManagerParse(response) {
    var responseObj = JSON.parse(response);
    var impact = responseObj.impact;
    var group = responseObj.group;
        g_form.setValue('impact', impact);
    g_form.setValue('assignment_group', group);
}

 

I'd be happy to explain this code to you.

This code is written in ServiceNow's JavaScript and is used to auto-populate the `impact` and `assignment_group` fields on a form based on the value selected in the `business_service` field.

Here's a breakdown of what the code does:

1. The `onChange` function is called whenever the value of the `business_service` field changes.
2. The function checks if the value is being loaded (i.e., it's not a real-time change) or if the value is empty. If either of these conditions is true, the function returns without doing anything.
3. The function creates a new instance of the `GlideAjax` class, which is used to make an AJAX call to a script include named `CustomCSDMGlideAjaxUtils`.
4. The function adds two parameters to the `GlideAjax` object: `sysparm_name` is set to `getService`, and `sysparm_service` is set to the value of the `business_service` field.
5. The function calls the `getXMLAnswer` method on the `GlideAjax` object, passing in the `ManagerParse` function as a callback function. This means that when the AJAX call returns a response, the `ManagerParse` function will be called with the response as an argument.
6. The `ManagerParse` function takes the response from the AJAX call and parses it as JSON.
7. The function extracts the `impact` and `group` values from the JSON response and sets the corresponding fields on the form using the `g_form.setValue` method.

In summary, this code is used to auto-populate the `impact` and `assignment_group` fields on a form based on the value selected in the `business_service` field. It uses a script include named `CustomCSDMGlideAjaxUtils` to make an AJAX call to retrieve the required data.

Script include :

var CustomCSDMGlideAjaxUtils = Class.create();
CustomCSDMGlideAjaxUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    getService: function(serviceId) {
        //Same function is used to make server side as well as client side call, when making call from server side send serviceId
        //When making call from client side sent parameter sysparm_service
        var impact = 4;
        var group = '';
        var service = '';
        var serviceCrit = 'notdefined';
        var busCriticality = '';
        service = this.getParameter("sysparm_service");
        if (JSUtil.notNil(serviceId)) {
            service = serviceId;
        }
        var grService = new GlideRecord('cmdb_ci_service');
        grService.get(service);
        group = grService.support_group.toString();
        busCriticality = grService.busines_criticality.toString();
        var impactMatcher = new GlideRecord('u_impact_data_lookup');
        var enQuery = '^active=true^u_business_criticality=' + busCriticality;
        impactMatcher.addEncodedQuery(enQuery);
        impactMatcher.query();
        if (impactMatcher.next()) {
            impact = impactMatcher.u_impact.toString();
        }
        // Build the payload. You can return additional data if needed.
        var result = {
            "impact": impact,
            "group": group
        };
        return JSON.stringify(result);
    },
    type: 'CustomCSDMGlideAjaxUtils'
});

 

This code is written in JavaScript and defines a custom class called `CustomCSDMGlideAjaxUtils` that extends the `AbstractAjaxProcessor` class. This class is used to process AJAX requests in ServiceNow.

The `getService` method is defined in this class, which takes a `serviceId` parameter. This method is used to retrieve information about a service record in ServiceNow.

Here's a breakdown of what the `getService` method does:

1. It sets some default values for variables: `impact`, `group`, `service`, `serviceCrit`, and `busCriticality`.

2. It checks if a `serviceId` parameter is provided. If it is, it sets the `service` variable to that value. Otherwise, it sets the `service` variable to the value of the `sysparm_service` parameter.

3. It creates a new GlideRecord object for the `cmdb_ci_service` table and retrieves the record with the specified `service` value.

4. It retrieves the `support_group` and `busines_criticality` values from the service record and sets them to the `group` and `busCriticality` variables respectively.

5. It creates a new GlideRecord object for the `u_impact_data_lookup` table and constructs a query to retrieve the `u_impact` value based on the `busines_criticality` value.

6. It executes the query and retrieves the `u_impact` value if a record is found.

7. It builds a JSON object with the `impact` and `group` values and returns it as a string using `JSON.stringify`.

The `type` property is set to `'CustomCSDMGlideAjaxUtils'` to identify this class as a custom AJAX processor.

This class can be used to make server-side or client-side calls to retrieve information about a service record in ServiceNow. The `getService` method can be called with a `serviceId` parameter to retrieve the service information.

Version history
Last update:
‎08-12-2024 10:42 PM
Updated by:
Contributors