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

This doesn't change the Short Description



If I change :   if(!shortd) to if(shortd) it runs but I get the onChange script error: RangeError: Maximum call stack size exceeded....


but it does change the short description at fail


It just puts the caller in many many many times with the : shortdescription at the very end.


antin_s
ServiceNow Employee
ServiceNow Employee

Can you show me whats your onChange Client Script on short_description?


I commented everything I had out....and put what you suggested in.   Again, this fails to update the Short Description at all "UNLESS" I remove the "!" from the if(!shortd).     Then it runs and loops...fails...but eventually puts in caller caller caller ....   caller caller : newshortd



So with an empty form, I select a caller from the picker


I then type "This is a short description" in the short_description field


When it runs it loops and I get the error but the short description field does get changed to....


caller name caller name caller name caller name caller name....over and over


at the end it puts in a colon and space --   caller name caller name caller name: This is a short description



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


  }


}


antin_s
ServiceNow Employee
ServiceNow Employee

Hi Jeff,



I hope the above Client Script runs on change of Caller. Do you have a client script for the change Short Description also?



What is your user case?



You want to populate the Caller name on Short Desc when Short Desc is empty. What do you want to do when Short Description changes?


The use case



When an ITIL user fills in an incident form to include Caller and Short Description the onChange Client Script will automatically replace the Short Description by putting the Caller into the Existing Short Description as a Prefix:


Example:


I select the caller:   Bob Jones


I type in the short description:   This is me typing the short description


When I tab out of the short description or click to the next field the short description would become


          Bob Jones: This is me typing the short description



The issue I believe is that the onChange is watching short_description and when the onChange script changes the short_description it fires repeatedly until is simply overflows and fails.     I need a way for the onChange to run once and find a condition that would cause it to end.