User field that is auto populated while submitting form disappears after req is submitted

Hritik
Tera Expert

Hi Community,

I need your help with debugging auto populating scripts.

 

I have create script include and client script to auto populate manager field when user is selected on the form. 

When I select user while raising form, the manager name is populated successfully. But after submission of ritm, the manager name is disappeared. 

Also, Client script is selected to Run on Catalog item, RITM, Catalog task. So, I'm confused what is causing this issue.

 

Please find below scripts i've used: 

 

Script include:

// Script Include: ManagerLookup
var ManagerLookup = Class.create();
ManagerLookup.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getManagerName: function () {
        var userId = this.getParameter('sysparm_user_id');
        var userGR = new GlideRecord('sys_user');
        if (userGR.get(userId)) {
            return userGR.manager.getDisplayValue();
        }
        return '';
    },
    type: 'ManagerLookup'
});

 

Client Script: 

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

    // Get the selected user's Sys ID
    var userId = g_form.getValue('variables.employee_to_offboard');

    // Call the Script Include using GlideAjax
    var ga = new GlideAjax('ManagerLookup');
    ga.addParam('sysparm_name', 'getManagerName');
    ga.addParam('sysparm_user_id', userId);
    ga.getXMLAnswer(function (answer) {
        // Assuming your manager field is named 'manager' on the catalog item
        g_form.setValue('variables.manager', answer);
  });

 


}

Hritik_0-1706859474913.png

 

 

Please suggest what I am missing here?

 

 

Thanks in Advance,

Hritik

 

1 ACCEPTED SOLUTION

piyushsain
Tera Guru
Tera Guru

Hi @Hritik 

Is the manager field a reference field or String field, if its reference field, in Script Include just return user.manager . If this is not the case then  if you have a variable within the variable set that has the same name as the internal name of the variable set, the value will not be saved in the RITM

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,
Piyush Sain

View solution in original post

5 REPLIES 5

Hi Priyanka,

Thanks for the response. Noted.

 

But again there's twik in the requirement and now I need to auto populate user name based on Employee Number given that too via API call.

Please see below thread:

https://www.servicenow.com/community/developer-forum/need-help-with-building-logic-to-populate-user-...

 

Please let me know if you can help.