Automatically update short_description to start with caller_id

jstocton
Kilo Expert

I have been asked to automatically put the caller_id in the beginning of the short_description on an Incident Form.   I have reviewed with our ServiceDesk and validated their use case asking for this change and it seems legit.   Newbie here....I think the right way to go is a Client Script to dynamically update the form onChange.   What am I missing?

Here's what I got and how crazy am I:

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

   

if (isLoading){  

          return;

    }

   

    //try to ensure we don't loop and keep processing short description field changes

    if (oldValue != ''){

  return;

    }

   

    //Alter Short Description with "caller_id" colon space short_description

   

    //Get contents of caller_id field and put into var caller

    var caller = g_form.getReference('caller_id');

   

    //Get contents of short_description and put into var shortd

    var shortd = g_form.getValue('short_description');

   

    //Combine var caller and var shortd into one var called newshortd

    var newshortd = (caller +': ' +shortd);

   

    //Set the new value of short_description with the var newshortd

    g_form.setValue('short_description', newshortd);

}

1 ACCEPTED SOLUTION

Please try below, if this helps.



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


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


return;  


}  


var shortd = g_form.getValue('short_description');


var caller = g_form.getReference('caller_id', doAlert);


function doAlert(caller) {


if(!shortd.includes(caller.name)){


var newshortd = caller.name +': ' + shortd;


g_form.setValue('short_description', newshortd);


}


}


}  


View solution in original post

30 REPLIES 30

antin_s
ServiceNow Employee
ServiceNow Employee

Hi Jeff,



The below script should help.



var caller = g_form.getReference('caller_id').name;


var caller = g_form.getReference('caller_id').name;



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


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


          return;


    }




    //Alter Short Description with "caller_id" colon space short_description


 


    //Get contents of caller_id field and put into var caller


    var caller = g_form.getReference('caller_id').name;


 


    //Get contents of short_description and put into var shortd


    var shortd = g_form.getValue('short_description');


 


    //Combine var caller and var shortd into one var called newshortd


    var newshortd = (caller +': ' +shortd);


 


    //Set the new value of short_description with the var newshortd


    g_form.setValue('short_description', newshortd);


   


}



Hope this helps. Mark the answer as correct/helpful based on impact.



Thanks


Antin


So that solves the issue of the reference coming back as object object and it actually puts in the caller name....excellent!



However, I believe it is looping.     I am doing an onChange for short_description AND changing the short_description which I believe is firing the onChange again and again...looping.



I have tried several methods to keep it from looping.   How can I make sure the onChange simpy returns if there is something already in the short description?



I get the following error:


onChange script error: RangeError: Maximum call stack size exceeded function onChange_incident_short_description_0(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue === ''){ return; } var caller = g_form.getReference('caller_id').name; var shortd = g_form.getValue('short_description'); var newshortd = (caller +': ' +shortd); g_form.setValue('short_description', newshortd); }




try BR as follows and that will fix your issue and will stop triggering onchange script which is on short_description.


antin_s
ServiceNow Employee
ServiceNow Employee

Can you try something like below?



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


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


          return;


    }




    //Alter Short Description with "caller_id" colon space short_description


 


    //Get contents of caller_id field and put into var caller


    var caller = g_form.getReference('caller_id').name;


 


    //Get contents of short_description and put into var shortd


    var shortd = g_form.getValue('short_description');


 


      if(!shortd){


      //Combine var caller and var shortd into one var called newshortd


      var newshortd = (caller +': ' +shortd);




      //Set the new value of short_description with the var newshortd


      g_form.setValue('short_description', newshortd);


  }


}



Hope this helps. Mark the answer as correct/helpful based on impact.



Thanks


Antin