Script is not working as expected !!!

suuriyas
Tera Contributor

HI Community,

 

I have written a script but in variables it is not getting aligned correctly

 

script include:

getSapCatalogValues: function() {
        var sap = this.getParameter('sysparm_sap');
        var sapVal = '';
        var gr = new GlideRecord("u_sap_catalog");
        gr.addQuery("u_reference_number", sap);
        gr.query();
        if (gr.next()) {
            sapVal = gr.u_short_description + ',' + gr.u_description + ',' + gr.u_questions + ',' + gr.u_approval + ',' + gr.u_assignment_group;
        }
        return sapVal;
    },
catalog client script:
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    var sap = g_form.getValue('reference_number');
    var ga = new GlideAjax('KAL_CatalogUtils');
    ga.addParam('sysparm_name', 'getSapCatalogValues');
    ga.addParam('sysparm_sap', sap);
    ga.getXML(callback);

    function callback(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        answer = answer.split(',');
        g_form.setValue('title', answer[0]);
        g_form.setValue('description', answer[1]);
        g_form.setValue('questions_information', answer[2]);
        g_form.setValue('approval_details', answer[3]);
        g_form.setValue('assignment_group', answer[4]);
    }
}
 
the issue is in sap catalog table in short desc field if the value is like lion,tiger,hippo then all these value need to be populate in title field but whats happening is it is getting break after , 
in catalog form it is like
title : lion
description : tiger
question:hippo
but i want title: lion,tiger,hippo
 
if in sap catalog table if any of the field value does not have , in it then it is working fine but if the value has , then it is not working as expected
what am i missing here?
Thanks in advance
Suuriya
1 ACCEPTED SOLUTION

@suuriyas 

update as this

getSapCatalogValues: function() {
        var sap = this.getParameter('sysparm_sap');
        var sapVal = {};
        var gr = new GlideRecord("u_sap_catalog");
        gr.addQuery("u_reference_number", sap);
        gr.query();
        if (gr.next()) {
            sapVal['shortDescription'] = gr.getValue('u_short_description');
			sapVal['description'] = gr.getValue('u_description');
			sapVal['question'] = gr.getValue('u_questions');
			sapVal['approval'] = gr.getValue('u_approval');
			sapVal['group'] = gr.getValue('u_assignment_group');
        }
        return JSON.stringify(sapVal);
    },

Client script

var answer = response.responseXML.documentElement.getAttribute("answer");
        var parsedData = JSON.parse(answer);
        g_form.setValue('title', parsedData.shortDescription);
        g_form.setValue('description', parsedData.description);
        g_form.setValue('questions_information', parsedData.question);
        g_form.setValue('approval_details', parsedData.approval);
        g_form.setValue('assignment_group', parsedData.group);

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

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@suuriyas 

sorry didn't get your question.

very difficult to understand without screenshots.

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

suuriyas_0-1734442734784.png

HI @Ankur Bawiskar 

in catalog form there is a variable called referenec number on selecting the value then other variables like title, desc, question and info, approval and assignment group will get auto populated.

For this to work i have written a script as mentioned in post, data will be coming from sap catalog table.

 

If you see the image in question & information variable "Please provide following data along with request
1. Selection Parameters (e.g. GL Code, GL Code Desc, Co Code, Posting " this needs to be present but only "Please provide following data along with request
1. Selection Parameters (e.g. GL Code" is filled as there is , in the value it is getting breaked and remaining are filled in other variables.

@suuriyas 

update as this

getSapCatalogValues: function() {
        var sap = this.getParameter('sysparm_sap');
        var sapVal = {};
        var gr = new GlideRecord("u_sap_catalog");
        gr.addQuery("u_reference_number", sap);
        gr.query();
        if (gr.next()) {
            sapVal['shortDescription'] = gr.getValue('u_short_description');
			sapVal['description'] = gr.getValue('u_description');
			sapVal['question'] = gr.getValue('u_questions');
			sapVal['approval'] = gr.getValue('u_approval');
			sapVal['group'] = gr.getValue('u_assignment_group');
        }
        return JSON.stringify(sapVal);
    },

Client script

var answer = response.responseXML.documentElement.getAttribute("answer");
        var parsedData = JSON.parse(answer);
        g_form.setValue('title', parsedData.shortDescription);
        g_form.setValue('description', parsedData.description);
        g_form.setValue('questions_information', parsedData.question);
        g_form.setValue('approval_details', parsedData.approval);
        g_form.setValue('assignment_group', parsedData.group);

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