Set a drop down field as "yes" when a perticular record selected from a reference field

1_DipikaD
Kilo Sage

Hi All,

I have reference field category and a dropdown field field with yes and no on change table. I need to set the drop down value as "yes" when I select a particular record from category field . I have tried the below script but did not get expected result.

 

Actually I was trying on a different dropdown field that's why I take 8 in the script but actually I need yes as output as mentioned above.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   var st = g_form.getValue("department");
   if (st === "5d7f17f03710200044e0bfc8bcbe5d43")
   {
    g_form.setValue("platform", '8');
   }

   //Type appropriate comment here, and begin script below
   
}
 
Thank you
2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@1_DipikaD 

the below will work if you are comparing the correct sysId and the correct choice value

Also use == and not === to compare

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   var st = g_form.getValue("department");
   if (st == "5d7f17f03710200044e0bfc8bcbe5d43") // give correct sysId
   {
    g_form.setValue("platform", '8'); // give correct field name and choice value
   }

   //Type appropriate comment here, and begin script below
   
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   var st = g_form.getValue("department");
   if (st == "5d7f17f03710200044e0bfc8bcbe5d43")
   {
    g_form.setValue("platform", 'sap');
   }

   //Type appropriate comment here, and begin script below
   
}
I tried this it is working but the drop down value is not changing with change with the reference field value .

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@1_DipikaD 

the below will work if you are comparing the correct sysId and the correct choice value

Also use == and not === to compare

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   var st = g_form.getValue("department");
   if (st == "5d7f17f03710200044e0bfc8bcbe5d43") // give correct sysId
   {
    g_form.setValue("platform", '8'); // give correct field name and choice value
   }

   //Type appropriate comment here, and begin script below
   
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   var st = g_form.getValue("department");
   if (st == "5d7f17f03710200044e0bfc8bcbe5d43")
   {
    g_form.setValue("platform", 'sap');
   }

   //Type appropriate comment here, and begin script below
   
}
I tried this it is working but the drop down value is not changing with change with the reference field value .

@1_DipikaD 

did you check what came in alert?

I believe your onchange is written on department variable

Are you sure the platform name is correct and the sap choice value is correct? Any spaces in the choice value?

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   var st = g_form.getValue("department");
   if (st == "5d7f17f03710200044e0bfc8bcbe5d43")
   {
   alert('inside if');
    g_form.setValue("platform", 'sap');
   }

   //Type appropriate comment here, and begin script below
   
}

 If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@1_DipikaD 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader