Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Client Script to Populate Field based on Another

JamesT-1
Tera Contributor

I have a need to populate a field (target) on the incident form based on the value of another field (source) also on the incident form. The source field is the configuration item field. If the configuration item field is an asset then the target field will automatically populate with the asset tag of that asset. I'm horrible at scripting and hoping someone can provide some assistance.

Thanks!

1 ACCEPTED SOLUTION

Najmuddin Mohd
Mega Sage

Hello @JamesT-1 ,

You can write an on change client script on field select as Configuration item,

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

    //Type appropriate comment here, and begin script below

   // g_form.addInfoMessage(newValue);


    var cmdbCI = g_form.getReference('cmdb_ci', assetTag); 
    
	function assetTag(cmdbCI) { 
        
         // **********Change the field 'u_ci_asset_tag' with name of custom field (target field)

           g_form.setValue('u_ci_asset_tag',cmdbCI.asset_tag); 
    }

}


If the above information helps you, Kindly mark it as Helpful and Accept the solution.
Regards,
Najmuddin.

View solution in original post

4 REPLIES 4

Najmuddin Mohd
Mega Sage

Hello @JamesT-1 ,

You can write an on change client script on field select as Configuration item,

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

    //Type appropriate comment here, and begin script below

   // g_form.addInfoMessage(newValue);


    var cmdbCI = g_form.getReference('cmdb_ci', assetTag); 
    
	function assetTag(cmdbCI) { 
        
         // **********Change the field 'u_ci_asset_tag' with name of custom field (target field)

           g_form.setValue('u_ci_asset_tag',cmdbCI.asset_tag); 
    }

}


If the above information helps you, Kindly mark it as Helpful and Accept the solution.
Regards,
Najmuddin.

Thanks Najmuddin,

 

That actually worked! So, I'm assuming I'd need to run a fixscript to populate pre-existing incidents. Any idea on what that fixscript would look like?

Hello @JamesT-1 ,

var grIncident = new GlideRecord('incident');
grIncident.query();

while(grIncident.next()){

// ***** change u_ci_asset_tag with your target field
    gr.u_ci_asset_tag = gr.cmdb_ci.asset_tag;
    gr.setWorkflow(false);
    gr.update();
}


Also, to learn scripting you can enroll in Scripting in ServiceNow Fundamentals On demand course.
It helps you to understand scripting in a simpler way.

 

LINK:

https://learning.servicenow.com/lxp/en/now-platform/scripting-in-servicenow-fundamentals-on-demand-w...

If the above information helps you, Please mark it as Helpful and Accept the solution.
Regards,
Najmuddin.

MAYANK9
Tera Expert

I am assuming your target field is of string type then you can configure in field's dictionary itself, but it will show up once form is saved after selecting the CI.

 

MAYANK9_1-1739300427552.png

 

MAYANK9_0-1739300347227.png