Autopopulate Custodian L4 Name and another field Custodian L4 Name2 based on Custodian Name

Naman Jain2412
Tera Expert

Hi, 

 

I have created a Script Include and Client Script however it is working fine when I select one custodian Name and it's autopopulating the Custodian L4 Name however when I select two user in Custodian Name it's not autopopulating the another person Custodian L4 Name 2 that I have created another field. Please let me know the resolution of the same based on the below script attached for the reference and let me know in case of any query,

 

SCRIPT INCLUDE SCRIPT 

Custodian L4 Name

 

 autopopulateL4Name: function() {
        gs.info("user")
        var userID = this.getParameter('sysparm_user_id');


        varuserIDArray = userID.split(',');
        var result = [];
        for (var i = 0; i < userIDArray.length; i++)


            var userGR = new GlideRecord('sys_user');
        userGR.addQuery('sys_id', userID);
        userGR.query();
        if (userGR.next()) {

            var l4name = userGR.department.u_l4_headempcode.getDisplayValue();
            gs.info("l4name" + l4name)


            var l4name2 = userGR.department.u_l4_headempcode.getDisplayValue();
            gs.info("l4name2" + l4name2)


        }
        return l4name;
    },

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

    // Check if a user is selected
    var userID = g_form.getValue('u_custodian_name');

        var ga = new GlideAjax('sn_compliance.Autopopulate_L4_Name');
        ga.addParam('sysparm_name', 'autopopulateL4Name');
        ga.addParam('sysparm_user_id', userID);
        ga.getXMLAnswer(function(response) {
            // g_form.addInfoMessage(response)
            if (response) {
                g_form.setValue('u_custodian_l4_name', response);
                g_form.setValue('u_custodian_l4_name2', response);

            }

        });
   
    }
2 ACCEPTED SOLUTIONS

Mohammad Danis1
Giga Guru

Hi @Naman Jain2412 ,
As per your script it looks  like Field "Custodian Name" is of type "LIST".

 

Please find the working script:

SCRIPT INCLUDE SCRIPT 

Custodian L4 Name:

 autopopulateL4Name: function() {
        var userIDs = this.getParameter('sysparm_user_id');

        var userIDArray = userIDs.split(',');
        var result = [];
        for (var i = 0; i < userIDArray.length; i++){
        var userGR = new GlideRecord('sys_user');
        userGR.addQuery('sys_id', userIDArray[i]);
        userGR.query();
        if (userGR.next()) {
        result.push(userGR.department.u_l4_headempcode.getDisplayValue());
        }
        return result.toString();
    },

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

    // Check if a user is selected
    var userID = g_form.getValue('u_custodian_name');

        var ga = new GlideAjax('sn_compliance.Autopopulate_L4_Name');
        ga.addParam('sysparm_name', 'autopopulateL4Name');
        ga.addParam('sysparm_user_id', userID.toString());
        ga.getXML(pop_details);
    
 function pop_details(response) {
      var values = response.responseXML.documentElement.getAttribute('answer').toString().split(',');
                g_form.setValue('u_custodian_l4_name', values[0]);
                g_form.setValue('u_custodian_l4_name2', values[1]);

}
}

 

It's working in PDI 🙂


Let me know if you still stuck somewhere.

View solution in original post

@Naman Jain2412 , Please share the updated script.
The one I shared should work.

View solution in original post

12 REPLIES 12

It is working however when I am saving the form it's automatically clear the field value. can you pls let me know which should be added so that it doesn't clear the value & data should also available when we check show XML.

 

 

Can you share your client script

If my response was helpful, please mark it as correct and helpful.
Thank you.

 //Type appropriate comment here, and begin script below


      // Check if a user is selected
    var userID = g_form.getValue('u_custodian_name');

        var ga = new GlideAjax('sn_compliance.Autopopulate_L4_Name');
        ga.addParam('sysparm_name', 'autopopulateL4Name');
        ga.addParam('sysparm_user_id', userID.toString());
        ga.getXML(pop_details);
   
 function pop_details(response) {
      var values = response.responseXML.documentElement.getAttribute('answer').toString().split(',');
                g_form.setValue('u_custodian_l4_name', values[0]);
                g_form.setValue('u_custodian_l4_name2', values[1]);

}
}