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

Got it. I think it is better the achieve it through a Business rule, because Client script can always get into infinitive loop. Please create a Business rile that runs before both update & insert.



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




var caller = current.caller_id.getDisplayValue();


var short_desc = current.short_description;


if(short_desc && short_desc.indexOf(': ') == -1){


current.short_description = caller + ": " + short_desc;


}




})(current, previous);



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



Thanks


Antin


antin_s
ServiceNow Employee
ServiceNow Employee

Have considered the scenario where the Caller has been changed. Please find the updated script.



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




var caller = current.caller_id.getDisplayValue();


var short_desc = current.short_description;


if(short_desc && short_desc.indexOf(caller+': ') == -1){


if(short_desc.indexOf(': ') != -1)


current.short_description = caller + ": " + short_desc.split(': ')[1];


else


current.short_description = caller + ": " + short_desc;


}




})(current, previous);



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



Thanks


Antin


antin_s
ServiceNow Employee
ServiceNow Employee

Hi Jeff,



Did you try the Business Rule or you are looking for a Client Script only?



Thanks


Antin


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);


}


}


}  


Hi Jeff Stocton.



If I have answered your question, please mark my response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list. https://community.servicenow.com/docs/DOC-5601