Auto populate field with multiple variables

davidvetter
Tera Contributor

Hi all.

 

I need to auto-populate a field on a catalog item. I have a vendor field that references a 'Company' table and want the address from that table to auto-populate. When I try to do this, I can only select one of the attributes of the address....street, city, state, or zip.

 

How can I select the address in it's entirety vice only one of the attributes?

4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

Since the data doesn't exist in the Company table as a single field, you need to retrieve multiple fields into a multi-line text variable with "\n" to denote a new line.  You can do this with Get Reference  or Glide Ajax , both very useful tools to have in your belt.  Give the script(s) a shot and if you get stuck post them here using the insert code </> icon and we'll get it sorted.

kaushal_snow
Giga Sage

Hi @davidvetter ,

 

If you're using Utah or later, the built-in Auto‑populate feature within variable is the simplest way. For earlier versions or more complex logic, use a Script Include with GlideAjax and an onChange Client Script. Reference qualifiers don’t auto-populate values, they only filter available options.

 

Try this once and let me know. If you find this helpful, please accept this as a solution and hit the helpful button..

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/

Rafael Batistot
Kilo Patron

hi @davidvetter 

 

May you try an onChange catalog script with glideAjax 

 

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

// GlideAjax call to get address info
var ga = new GlideAjax('GetVendorAddress');
ga.addParam('sysparm_name', 'getAddress');
ga.addParam('sysparm_vendor_id', newValue);
ga.getXMLAnswer(function(response) {
var addressData = JSON.parse(response);

// Set values to the other variables
g_form.setValue('address', addressData.address);
g_form.setValue('city', addressData.city);
g_form.setValue('state', addressData.state);
g_form.setValue('zip', addressData.zip);
});
}



Script include 

 

// Script Include: GetVendorAddress
// Accessible from Client: true
var GetVendorAddress = Class.create();
GetVendorAddress.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getAddress: function() {
var vendorId = this.getParameter('sysparm_vendor_id');
var company = new GlideRecord('core_company');

if (company.get(vendorId)) {
var result = {
address: company.getValue('address'),
city: company.getValue('city'),
state: company.getValue('state'),
zip: company.getValue('zip')
};
return JSON.stringify(result);
}

return JSON.stringify({
address: '',
city: '',
state: '',
zip: ''
});
}
});

 

If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.

Ankur Bawiskar
Tera Patron
Tera Patron

@davidvetter 

2 approaches

1) either have individual variables to hold street, city, state, zip etc and then use Auto populate feature to populate those based on Company variable

Auto-populate a variable based on a reference type variable (Utah) 

OR

2) have a multi-line text variable and use GlideAjax or getReference callback to populate all the details in single variable, like this

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }

    if (newValue == '') {
        g_form.clearValue('detailsVariableName'); // your variable name here
    }

    var ref = g_form.getReference('companyVariableName', callBackMethod); // your variable name
}

function callBackMethod(ref) {
    var completeDetails = 'Street:' + ref.street + '\n' + 'City:' + ref.city + '\n' + 'State:' + ref.state + '\n' + 'Zip:' + ref.zip;
    g_form.setValue('detailsVariableName', completeDetails); // your variable name here
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader