Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Clear field value based on auto populated field

Saranya2
Tera Contributor

Hi All,

-> I have 2 reference fields on incident form "incident manager" and "Managing Group". If incident manager is selected, the respective group should be populated on Managing Group field that the incident manager is a part of.

-> After Managing Group has auto populated, if anyone manually changed that group, then I need to make the "incident manager" field as empty.

-> For auto populating Managing group based on incident manager, I have written on change client script & Display BR, That is working fine.

-> But to clear field value based on manual change of Managing group. I have written, on change client script, but it is conflicting. it is always clearing the field value. can anyone please suggest.

Display BR

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

        var MI_manager = current.u_incident_manager;

        g_scratchpad.grp = 'false';

        var x = gs.getProperty('MI_Groups');

        var grpList = x.split(',');

        for (var i = 0; i < grpList.length; i++) {

            if (gs.getUser().getUserByID(MI_manager).isMemberOf(grpList[i].trim())) {

                g_scratchpad.grp = 'true';

               }

        }

        })(current, previous);

Onchange CS to populate group

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

   if (isLoading || newValue === '') {

      return;

   }

   var grp = g_scratchpad.grp;

   if(grp){

        g_form.setValue('u_managing_group',grp);

 

   }

  } 

Onchange CS to clear field value if group manually changes

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

   if (isLoading || newValue === '') {

      return;

   }

   g_form.clearValue('u_incident_manager');

  }

Thanks.

6 REPLIES 6

Hi @Anil Lande ,

 

I have modified the script, it is not clearing the incident manager field value. 

Anand Kumar P
Giga Patron

Hi @Saranya2 ,
Don't write 2 onchange client scripts on same field its conflict and not work.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
      var grp = g_scratchpad.grp;
    if (control.name === 'u_incident_manager') {
        g_form.clearValue('u_managing_group');
    } else {
        if (grp) {
            g_form.setValue('u_managing_group', grp);
        }
    }
    }
}


Thanks,

Anand