The CreatorCon Call for Content is officially open! Get started here.

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

Hi Shishir,



Good Day



I have an concern in your code, why do we need to write a function doAlert().



I tried with the below   script which works fine!



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



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



if(!shortd.includes(caller)){



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



g_form.setValue('short_description', newshortd);



}



Pls give your   feedback (Like, Helpful or Correct) if it is quite useful.




Thanks,


Priyanka R


Hi Priyanka,



With callback function processing happens asynchronously, and browser (and script) processing will continue normally until the server returns the reference value.



Please check this for more info: GlideForm (g form) - ServiceNow Wiki


Hi Shishir,



Call back function is the recommended one for the reference field, Got it!



Thanks for your response



Thanks,


Priyanka R


Raghu Loganatha
Kilo Guru

Jeff,          



Using OnChange Client script for this case is not the best way in my opinion and the reason for that is : Lets say caller_id is not a read only field and it can be modified at any point. If user adds "x" as caller now and realizes it should be on another's name and changes it to "Y".



Your short description will look like this initially : Shortdescription = x *********


later it will look like : shortdescription = y x ******



Best way to get this done is by using OnSubmit Client Script on a BR. s:




Script for BR is as follow:



current.short_description = current.caller_id.name + " " +current.short_description;



~Raghu,


Created BR and it does work.   However, I have other things that will feed the Incident form that would be doing the same thing.   When that happens it creates a short description that has Caller: Caller: newshortd



I worked with the conditions and got it stop doing it, but when looking at this the use case was for immediate gratification.   I type in the short description and immediately when I tab out or go to the next field it changes to Caller: ShortDescription



I really would like to keep this as a Client Script (for instant gratification) but figure out a way to simply not run it if something is already in the short description.