Issue with client callable script include

Community Alums
Not applicable

Hi All,

 

I have written an OnChange client script and client-callable script includes in order to set value of Vendor field in Incident task table according to value of assignment group:

 

Here is the client script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    g_form.addInfoMessage("New Value is" + newValue);
    var ga = new GlideAjax('Set_Vendor_to_sync_with_asssignment_group');
    ga.addParam('sysparm_name', 'fetchVendor');
    ga.addParam('assignment_group',newValue);
    ga.getXML(generateResponse);

    function generateResponse(response) {
        var vendor = response.responseXML.documentElement.getAttribute("answer");
        g_form.addInfoMessage("Response is"+vendor);
        g_form.setValue('u_vendor', vendor);
    }
    //Type appropriate comment here, and begin script below

}
 
And here is the Client- callable Script includes:(role : snc_internal)
 
var Set_Vendor_to_sync_with_asssignment_group = Class.create();
Set_Vendor_to_sync_with_asssignment_group.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    fetchVendor: function(assignment_group) {
        gs.info("fetchVendor function called" +assignment_group);
        var mapping = gs.getProperty('vendor.group.mapping');
        var mappingObj = JSON.parse(mapping);
        var vendor = mappingObj[assignment_group];
        gs.info("vendor according to group is"+vendor);
        return vendor;
    },
    type: 'Set_Vendor_to_sync_with_asssignment_group'
});
 
My Script includes is working as expected when I call it from background script, but when it is being called with the same assignment group value from Client script, it is not working as expected. On checking logs, I found that assignment group is coming as undefined when function is being called from Client script.
 
Can someone please help me with this?
 
Thanks,
Hrithik Nirupam.
 
1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@Community Alums Please update your client script and script include as follows.

 

Client Script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    g_form.addInfoMessage("New Value is" + newValue);
    var ga = new GlideAjax('Set_Vendor_to_sync_with_asssignment_group');
    ga.addParam('sysparm_name', 'fetchVendor');
    ga.addParam('sysparm_assignment_group',newValue); //Always use sysparm_ prefix before the parameter names
    ga.getXML(generateResponse);

    function generateResponse(response) {
        var vendor = response.responseXML.documentElement.getAttribute("answer");
        g_form.addInfoMessage("Response is"+vendor);
        g_form.setValue('u_vendor', vendor);
    }
    //Type appropriate comment here, and begin script below

}

 

Script Include:

 

 

var Set_Vendor_to_sync_with_asssignment_group = Class.create();
Set_Vendor_to_sync_with_asssignment_group.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    fetchVendor: function(assignment_group) {
       var assignmentGroup = this.getParameter("sysparm_assignment_group")?this.getParameter("sysparm_assignment_group"):assignment_group;
        gs.info("fetchVendor function called" +assignmentGroup);
        var mapping = gs.getProperty('vendor.group.mapping');
        var mappingObj = JSON.parse(mapping);
        var vendor = mappingObj[assignmentGroup];
        gs.info("vendor according to group is"+vendor);
        return vendor;
    },
    type: 'Set_Vendor_to_sync_with_asssignment_group'
});

There is a slight difference in fetching the parameter passed from the client side. As you need to use this.getParameter to get the parameter value.

 

The changes made in the script include script will allow you to fetch values for both server side and client side.

 var assignmentGroup = this.getParameter("sysparm_assignment_group")?this.getParameter("sysparm_assignment_group"):assignment_group;

 

Please mark the response helpful and accepted solution if it manages to answer your question.

View solution in original post

2 REPLIES 2

Sandeep Rajput
Tera Patron
Tera Patron

@Community Alums Please update your client script and script include as follows.

 

Client Script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    g_form.addInfoMessage("New Value is" + newValue);
    var ga = new GlideAjax('Set_Vendor_to_sync_with_asssignment_group');
    ga.addParam('sysparm_name', 'fetchVendor');
    ga.addParam('sysparm_assignment_group',newValue); //Always use sysparm_ prefix before the parameter names
    ga.getXML(generateResponse);

    function generateResponse(response) {
        var vendor = response.responseXML.documentElement.getAttribute("answer");
        g_form.addInfoMessage("Response is"+vendor);
        g_form.setValue('u_vendor', vendor);
    }
    //Type appropriate comment here, and begin script below

}

 

Script Include:

 

 

var Set_Vendor_to_sync_with_asssignment_group = Class.create();
Set_Vendor_to_sync_with_asssignment_group.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    fetchVendor: function(assignment_group) {
       var assignmentGroup = this.getParameter("sysparm_assignment_group")?this.getParameter("sysparm_assignment_group"):assignment_group;
        gs.info("fetchVendor function called" +assignmentGroup);
        var mapping = gs.getProperty('vendor.group.mapping');
        var mappingObj = JSON.parse(mapping);
        var vendor = mappingObj[assignmentGroup];
        gs.info("vendor according to group is"+vendor);
        return vendor;
    },
    type: 'Set_Vendor_to_sync_with_asssignment_group'
});

There is a slight difference in fetching the parameter passed from the client side. As you need to use this.getParameter to get the parameter value.

 

The changes made in the script include script will allow you to fetch values for both server side and client side.

 var assignmentGroup = this.getParameter("sysparm_assignment_group")?this.getParameter("sysparm_assignment_group"):assignment_group;

 

Please mark the response helpful and accepted solution if it manages to answer your question.

Community Alums
Not applicable

Thank you @Sandeep Rajput .