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

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

Priyanka_786
Tera Guru
Tera Guru

Hi @Hritik ,

 

I am assuming if you are using Utah or further version, easy configuration will be help you on this (Auto-populate feature for catalog item variables) instead of using scripting way. For more information refer this blog.

https://www.servicenow.com/community/developer-articles/auto-populate-a-variable-based-on-a-referenc...

 

Kindly mark helpful/accepted if it helps you.

Regards,

Priyanka Salunke

Hi Priyanka,

 

Thanks for the reference. I used Auto populate feature, but I encountered another problem.

https://www.servicenow.com/community/developer-forum/need-help-with-populating-manager-field-after-c...

 

Could you please and help me here ? Auto poulation does not work if we create request via REST API

@Hritik ,

 

As per the research, unfortunately, Auto population feature does not work if request is created via REST API currently.  Servicenow might enhanced it in upcoming release. You need to use scripting way (by Business rule) for now.

Kindly mark helpful/accepted if it assists you.

Regards,

Priyanka Salunke