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

Shraddha Kadam
Mega Sage

Hello @Naman Jain2412 ,

 

As per your script you are only returning l4name try to return l4name2 as well.

For your reference, please update the code -

Script Include -

 autopopulateL4Name: function() {
var data = '';

        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)

         data = l4name + ' , ' + l4name2
        }
        return data;
    },

});

 

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.getXML(getResponse);
          
 function getResponse(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]);
        }
    }

 

Let me know if you faced any issues.

 

Thank you

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

Thanks for your response.I have tried via the same script however it is autopopulating the value for only 1 field as Custodian L4 Name not autopopulating the value when I have selected 2 users for another field as Custodian L4 Name 2. pls check and let me know the issue.

Nishant8
Giga Sage

Hello @Naman Jain2412, there are a few problems in your script, can you please use followings and share the outcome?

- update autopopulateL4Name function in Script include as below

autopopulateL4Name: function() {
        gs.info("user");
        var userID = this.getParameter('sysparm_user_id');
        var userIDArray = userID.split(',');
        var result = [];
        var userGR = new GlideRecord('sys_user');
        for (var i = 0; i < userIDArray.length; i++) {
            userGR.get(userIDArray[i]);
            userGR.query();
            if (userGR.hasNext()) 
                result.push( userGR.department.u_l4_headempcode.getDisplayValue() ); 
        }
        return JSON.stringify( result );
    }

 - 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');
    if (userID) {
        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) {
                response = JSON.parse(response);
                g_form.setValue('u_custodian_l4_name', response[0]);
                g_form.setValue('u_custodian_l4_name2', response[1]);

            }
        });
    }

}

 

Regards,

Nishant