Auto- Populate service level field based on selection of affected CI on Incident table

deepikagang
Tera Contributor

Hi All,

Want to populate custom field (u_service_level) based on selection of affected ci reference to cmdb_ci table.

 

deepikagang_0-1739783778431.png

 

Thank you in advance

 

Best regards

Deepika

15 REPLIES 15

sravya chipilla
Tera Contributor

Configure the Business Rule:

  • Name: Set Service Level from CI
  • Table: incident
  • When: Before
  • Insert/Update: (Check both)
  • Condition: cmdb_ci != empty
  • Script:
    (function executeRule(current, previous /*null when async*/) {
    var grCI = new GlideRecord('cmdb_ci');
    if (grCI.get(current.cmdb_ci)) {
    current.u_service_level = grCI.u_service_level;
    }
    })(current, previous);

    please mark correct if this solution helps.

neetusingh
Giga Guru

@deepikagang - 

Below are the scripts to populate a custom field (`u_service_level`) on the incident table based on the selected "Affected CI" (Configuration Item) from the `cmdb_ci` table:

 

Below is an onchange client script that will be written on Incident table -

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === '') {

        return;

    }

    //Get the sys_id of the selected Affected CI

    var ciSysId = g_form.getValue('cmdb_ci');

    var ga = new GlideAjax('ServiceLevelUtil'); //Call a GlideAjax

    ga.addParam('sysparm_name', 'getServiceLevel'); //Specify function name

    ga.addParam('sysparm_ci_sys_id', ciSysId); //Pass the CI sys_id

    ga.getXMLAnswer(function(answer) {

        if (answer) {

            g_form.setValue('u_service_level', answer); //Set the u_service_level field with the returned value

        }

    });

}

 

Below is a Script Include -

var ServiceLevelUtil = Class.create();

ServiceLevelUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getServiceLevel: function() {

        var ciSysId = this.getParameter('sysparm_ci_sys_id');

        var serviceLevel = '';

 

        var ci = new GlideRecord('cmdb_ci');

        if (ci.get(ciSysId)) {

            serviceLevel = ci.u_service_level; // Assuming the CI has a field called u_service_level

        }

        return serviceLevel;

    },

    type: 'ServiceLevelUtil'

});

 

Ankur Bawiskar
Tera Patron
Tera Patron

@deepikagang 

it's not field of your table.

I could see in screenshot it's a dot walked field from Support Service.

If it's dot walked then it will get auto-populated based on Support Service selected.

What's your business requirement?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar ,

Yes its a dot walked field.
Requirement is to auto- populate the dot walked field (u_service_level) based on affected ci on incident form.

 

Thanks in advance.

@deepikagang 

Not a good practice and won't help.

2 ways

1) You can create your own field on your table and then set it with the change of CI

no script include required, you can use getReference() with callback

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading) {
		return;
	}

	if(newValue == ''){
		g_form.clearValue('yourNewField');
	}

	var ref = g_form.getReference('cmdb_ci', callBackMethod);
}

function callBackMethod(ref){
	if(ref.u_service_level)
		g_form.setValue('yourNewField', ref.u_service_level); 
}

OR

2) dot walk the Service Level from Affected CI on your form then whenever Affected CI is changed the Service level will be populated and no scripting required

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader