On Change Client Script to auto populate fields

mgjk
Tera Contributor

Hi all,

Greetings. I have created an application called Transfer Tracker.  I wanna auto populate the short description, description field if changed the filed name called Ticket_Number. Ticket_number field is Reference field from Incident Table. Please find the below script. Also find the attached files for the field types which I have created

DescriptionFieldDescriptionFieldShort DescriptionShort DescriptionForm for Transfer TrackerForm for Transfer TrackerTicketNumber FieldTicketNumber Field

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

//Type appropriate comment here, and begin script below

}
var incSD=g_form.getReference('u_ticket_number',populate);
function populate(incSD){
if(incSD.short_description){
g_form.setValue('u_ttshort_description',incSD.short_description);
g_form.setValue('u_tt_description',incSD.description);
}
}


}

1 ACCEPTED SOLUTION

Isaias H
Tera Guru

Hi,

 

It looks like your code is outside of the onChange function.

 

Please try the following version:

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

  var incSD = g_form.getReference('u_ticket_number', populate);

  function populate(incSD) {
    if (incSD && incSD.short_description) {
      g_form.setValue('u_ttshort_description', incSD.short_description);
      g_form.setValue('u_tt_description', incSD.description);
    }
  }
}

 

View solution in original post

4 REPLIES 4

Bert_c1
Kilo Patron

Hi,

 

I suggest that you look at existing onChange Client Scripts for working examples.  Also see:

 

https://developer.servicenow.com/dev.do#!/reference/api/utah/client/c_GlideFormAPI#r_GlideForm-GetRe...

 

and in general:

 

https://developer.servicenow.com/dev.do#!/reference/api/utah/client/c_GlideFormAPI

 

There are many examples there.

Isaias H
Tera Guru

Hi,

 

It looks like your code is outside of the onChange function.

 

Please try the following version:

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

  var incSD = g_form.getReference('u_ticket_number', populate);

  function populate(incSD) {
    if (incSD && incSD.short_description) {
      g_form.setValue('u_ttshort_description', incSD.short_description);
      g_form.setValue('u_tt_description', incSD.description);
    }
  }
}

 

mgjk
Tera Contributor

Hi Isais,

Thanks a lot for the reply. In Previous assignment group field, its showing sys ID I guess instead of  Group name please find the attachment. 

And the script is given below


function onChange(control, oldValue, newValue, isLoading, isTemplate) {
  if (isLoading || newValue === '') {
    return;
  }
 
  var incSD = g_form.getReference('u_ticket_number', populate);
 
  function populate(incSD) {
    if (incSD && incSD.short_description) {
      g_form.setValue('u_ttshort_description', incSD.short_description);
      g_form.setValue('u_tt_description', incSD.description);
g_form.setValue('u_previous_assignment_group', incSD.assignment_group);
    }
  }





 

 

Regards..

Mani

setValue has an additional parameter to specify a display value

DOC: https://developer.servicenow.com/dev.do#!/reference/api/tokyo/client/c_GlideFormAPI#r_GlideFormSetVa...

 

You can try:

g_form.setValue('u_previous_assignment_group', incSD.assignment_group, incSD.assignment_group.getDisplayValue());