Autopopulate field value from different table on different reference+selectbox dropdown(Tricky one)

Sunpreetk
Tera Contributor

I have a field accounts referencing the account table(customer_account) . Inside account table we don't have users (internal user information). i have a record producer, when people go to ui of that i want to autopopulate this account with user company.
I have used a reference qualifier which dynamically filtered the account list , it is attached with script include returning company sys id and advance reference qualifier is working absolutely fine. Problem is I don't want that reference field select box to give me one single company value for choosing. I want system to autopopulate when user land in. I can't use autopopulate field because autopopulate will be based on another dependent question. default will work only on reference table name. BUT Here reference table is customer_account. If we add in defauly getuser.company function it won't work. i dont know why.

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

The only attachment to your post is a screenshot showing an empty field/variable.  What you need to do is use an onLoad Catalog Client Script that uses GlideAjax to call the Script Include, passing in the current user.  The Script Include needs the beginning lines changed so that it's client callable.  You can still use it in the reference qualifier so that the user can't change the value to an invalid one. The Script Include will do what works for the reference qualifier, returning a sys_id which the Client Script will use to populate the variable.

 

Here's an excellent guide on using GlideAjax 

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2... 

 

Using GlideRecord in a client script is not supported nor recommended.  Give the scripts a try and post them using the insert code icon </> if you get stuck.  Also post the reference qualifier. 

View solution in original post

3 REPLIES 3

Badrinarayan
Tera Guru

Hi @Sunpreetk  ,

 

for your usecase , try the below code onload client script

 

function onLoad() {
    // Get the user's company
    var userCompany = g_user.company;

    // Create a GlideRecord to find the Customer Account where company matches the user's company
    var gr = new GlideRecord('customer_account');
    gr.addQuery('company', userCompany); // Filter accounts by the user's company
    gr.query();

    // If there's only one account for this company, set it as the default value in the reference field
    if (gr.next()) {
        // Set the Account field to the Sys_ID of the found Customer Account
        g_form.setValue('accounts', gr.sys_id);
    }
}

 

If this solution resolves your query, please accept the solution and mark it as helpful.

Thanks & regards,
Badrinarayan

Brad Bowman
Kilo Patron
Kilo Patron

The only attachment to your post is a screenshot showing an empty field/variable.  What you need to do is use an onLoad Catalog Client Script that uses GlideAjax to call the Script Include, passing in the current user.  The Script Include needs the beginning lines changed so that it's client callable.  You can still use it in the reference qualifier so that the user can't change the value to an invalid one. The Script Include will do what works for the reference qualifier, returning a sys_id which the Client Script will use to populate the variable.

 

Here's an excellent guide on using GlideAjax 

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2... 

 

Using GlideRecord in a client script is not supported nor recommended.  Give the scripts a try and post them using the insert code icon </> if you get stuck.  Also post the reference qualifier. 

Sunpreetk
Tera Contributor

thanks bard!!