Issue: Field and its value not loading in the catalog item (Script include and client script)

Service now11
Tera Contributor

Hi,

 

I have created a "model number" variable and its corresponding value as part of a variable set, along with another variable called "model." This setup is applied to multiple catalog items, each with a unique value. I’ve used a Script Include and Client Script to load the variable field along with its value. However, while other variables ("model" and its value) populate correctly when entering the form, the model number variable is not populated with its value. 

Can someone help me identify why the model number is missing, and guide me on how to properly load the model number?

Below is the code:

 

Client Script:

 
function onLoad() {
    var util = new GlideAjax("ServiceCatalogItem");
    var rec = g_form.getUniqueValue();
    util.addParam("sysparm_name", 'getCatItemModel');
    util.addParam("sysparm_cat_id", rec);
    util.getXML(callBack);
    var answer;

    function callBack(response) {
        answer = response.responseXML.documentElement.getAttribute("answer");

        g_form.setValue("u_model", answer);
        g_form.setValue("u_model number", answer);
    }
}
 
Script Include: 
var ServiceCatalogItem = Class.create();
ServiceCatalogItem.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getCatItemModel: function() {
        var sys_id = this.getParameter('sysparm_cat_id');

        var item = new GlideRecord('sc_cat_item');
        item.addQuery("sys_id", sys_id);
        item.query();
        item.next();

        if (item) {
            return item.model.name;   
//model number field should also loaded with its value
        } else {
            return false;
        }

    },
    type: 'ServiceCatalogItem'
});
 
Thank You
1 ACCEPTED SOLUTION

dgarad
Giga Sage

Hi @Service now11 

change code as below and try.

 

Client Script:

 
function onLoad() {
    var util = new GlideAjax("ServiceCatalogItem");
    var rec = g_form.getUniqueValue();
    util.addParam("sysparm_name", 'getCatItemModel');
    util.addParam("sysparm_cat_id", rec);
    util.getXML(callBack);
    var answer;

    function callBack(response) {
        answer = response.responseXML.documentElement.getAttribute("answer");
var sepAnswer = answer.split('$');

        g_form.setValue("u_model", sepAnswer[0]);
        g_form.setValue("u_model number", sepAnswer[1]);
    }
}
 
Script Include: 
var ServiceCatalogItem = Class.create();
ServiceCatalogItem.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getCatItemModel: function() {
var modelDetails='';
        var sys_id = this.getParameter('sysparm_cat_id');

        var item = new GlideRecord('sc_cat_item');
        item.addQuery("sys_id", sys_id);
        item.query();
         if (item.next()) {
            return modelDetails.item.model.name+','+modelDetails.item.model.number;   
//model number field should also loaded with its value
        } else {
            return false;
        }

    },
    type: 'ServiceCatalogItem'
});

 

If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

View solution in original post

8 REPLIES 8

model_number is correct backend table field. it may be u_model_number.

If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

Ankur Bawiskar
Tera Patron
Tera Patron

@Service now11 

you can achieve this without scripting using Auto populate feature starting from Utah release

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

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

Service now11
Tera Contributor

Hi @Ankur Bawiskar ,

 

Thank you for your response. The fields are read-only. I need for non-reference field.

@Service now11 

the auto populate feature is to bring data from referenced table

Did you explore that?

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