How do I autopopulate the Location field on my Catalog Item, depending Company the User is a part of

patrykprejs
Tera Contributor

Hi All,

 

I am trying to autopopulate the Location field on my Catalog item, the dependency is the User ref field and it should be dependant on what Company the User is a part of.

 

For Example:

 

Name of User: User 1 (User 1 is a part of Company A)

Location: Location 6 (Location 6 is selected because User 1 is part of Company A, if not then clear the value)

 

Does anyone know how I can accomplish this?

I have tried using a SI and a Client Script but had no luck, please see the examples below:

 

Script Include:

var UserDetailsUtil = Class.create();
UserDetailsUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getUserCompany: function() {
        var userSysId = this.getParameter('sys_id');
        var userRecord = new GlideRecord('sys_user');

        if (userRecord.get(userSysId)) {
            var companyName = userRecord.company.name;
            return companyName;
        }
        return '';
    }

});

 
Catalog Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    // Ensure the script runs only when the form is not loading
    if (isLoading || newValue == '') {
        return;
    }

    // Get the sys_id of the user selected
    var userSysId = g_form.getValue('name_of_user');

    // If no user is selected, clear the 'location_to_send_to' field
    if (!userSysId) {
        g_form.clearValue('location_to_send_to');
        return;
    }

    // GlideAjax call to get user information
    var ga = new GlideAjax('UserDetailsUtil'); // GlideAjax class to call Script Include
    ga.addParam('sys_id', userSysId); // Send user sys_id
    ga.getXMLAnswer(function(response) {
        var userCompany = response;

        // Check if the user belongs to 'CompanyA'
        if (userCompany == 'CompanyA') {
            // Set the 'location_to_send_to' field to 'Location6'
            g_form.setValue('location_to_send_to', 'ab2e946b833302109af46060ceaad3b0', 'Location6');
        } else {
            // Clear the 'location_to_send_to' field if the user is not part of 'CompanyA'
            g_form.clearValue('location_to_send_to');
        }
    });
}

 

Thanks for any help in advance.

5 REPLIES 5

Allen Andreas
Administrator
Administrator

Hello,

You don't need to use any scripting for this as you can use low code/no code features like a catalog data lookup definition, which allows you to choose a field to drive auto-population for other fields. See more here: https://docs.servicenow.com/csh?topicname=t_CreatACatDataLookupDefRec.html&version=latest 


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi @Allen Andreas,

 

Thank you for the suggestion, however I don't think that is the answer for my issue.

 

You see the User is Part of Company A, however not the Location, therefore I can't grab the Location from the User if that makes sense to map the field?

 

Which is why I tried the Glide Ajax approach, however if you think that it should still be possible then I am all ears 🙂

 

Thank you,

Patryk

Hello,

Alright, so perhaps you could explain that a bit more in your original post that on the user record...you're saying there is no reference available to the data that you need? So on their user record, the location field doesn't match to what you're trying to set?

 

So instead, you need to evaluate the company of the user, then do some other lookup?

 

If that's the case, then yes, you'd probably have to use GlideAjax. I noticed in your client script that you're not calling the specific function within the script include? Please refer to this helpful GlideAjax cheat sheet and consider reviewing your solution accordingly?  https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2... 

 

Example:

ga.addParam('sysparm_name', 'getUserCompany');

Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi @Allen Andreas,

Correct, so they are a part of a company that has multiple locations, in this case it's like an order form so if a User with Company A needs something then it has to go this Location 6 which is a specific location.

I hope that makes more sense? They are not necessary based there, therefore it has to be sent to this specific location every time if a User with that Company is selected.

I'll have a look, I am quite new to GlideAjax so will try my best but I hope the above makes a little more sense.

 

Thanks,

Patryk