How to add key and value in the response we got from rest message?

Prathamesh Chav
Tera Contributor

Hi Team,

 

I am fetching response from restmessage but from there I am getting company name, start date and end date,

start date and end date I am able to populate on change form, but for company name which is reference field I am trying to fetch sys_id from the same script include which is I am using to call rest message and fetch the responseBody and I am passing that responseBody to the client side.

 

PrathameshChav_0-1740044470079.png

 

 


 
Updating Media

 

try {
 var r = new sn_ws.RESTMessageV2('CLM Integration', 'Default GET');
 r.setStringParameterNoEscape('Apttus__FF_Agreement_Number__c', crn_number);
 // r.setStringParameterNoEscape('Apttus__FF_Agreement_Number__c', '00054549.0');


 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();
}
catch(ex) {
 var message = ex.message;
}
        if(httpStatus == 200){

             var compSys = new GlideRecord("core_company");
 compSys.addEncodedQuery("nameSTARTSWITH" + responseBody.records[0].Account_Name__c);
 compSys.query();
 if (compSys.next()) {
     var sys_Comp = compSys.sys_id;
 }

//adding new line
responseBody.records[0].sys_idd = sys_Comp;
            return JSON.stringify(responseBody);
        }
       

    },
 
getting this error message
PrathameshChav_1-1740044827032.png

 


 

 

23 REPLIES 23

@Prathamesh Chav 

at which line it's giving error?

Did you try to give hard-coded API response and see if it works?

what debugging have you done so far?

The script I shared should work fine, you should enhance it by adding some logs for debugging

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

 
Tried your script and also tried with following like making responseBody tring make parsing it again in JSON
 
 
 
var clmCrmIntegration = Class.create();
clmCrmIntegration.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getCrnNum: function() {
        var crn_number = this.getParameter('clmcrn');
        var obj = {};
        try {
            var r = new sn_ws.RESTMessageV2('CLM Integration', 'Default GET');
            r.setStringParameterNoEscape('Apttus__FF_Agreement_Number__c', crn_number);

            var response = r.execute();
            var responseBody = response.getBody();
            var httpStatus = response.getStatusCode();
            if (httpStatus == 200) {

                JSON.parse(responseBody);  // Convert string to JSON

                var jsoneresString = JSON.stringify(responseBody);
                var jsonResponse = JSON.parse(jsoneresString);  // Convert string to JSON

                var startDate = jsonResponse.records[0].Apttus__Contract_Start_Date__c;
                var endDate = jsonResponse.records[0].Apttus__Contract_End_Date__c;

                obj['start'] = startDate.toString();
                obj['end'] = endDate.toString();

                var accountName = jsonResponse.records[0].Account_Name__c;

                var gr = new GlideRecord('core_company');
                gr.addEncodedQuery('nameSTARTSWITH' + accountName);
                gr.setLimit(1);
                gr.query()
                if (gr.next()) {
                    obj['company'] = gr.getValue("sys_id");
                }
                return JSON.stringify(obj);
            }
            return '';
        } catch (ex) {
            var message = ex.message;
            gs.error("Error in getCrnNum: " + message);
        }
    },

    type: 'clmCrmIntegration'
});
 
@sunil maddheshi have a look getting the same error
 

@Prathamesh Chav 

you didn't use the same script which I shared

try this

var clmCrmIntegration = Class.create();
clmCrmIntegration.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getCrnNum: function() {
        var crn_number = this.getParameter('clmcrn');
        var obj = {};
        try {
            var r = new sn_ws.RESTMessageV2('CLM Integration', 'Default GET');
            r.setStringParameterNoEscape('Apttus__FF_Agreement_Number__c', crn_number);

            var response = r.execute();
            var responseBody = response.getBody();
            var httpStatus = response.getStatusCode();
            if (httpStatus == 200) {


                var jsonResponse = JSON.parse(responseBody);  // Convert string to JSON

                var startDate = jsonResponse.records[0].Apttus__Contract_Start_Date__c;
                var endDate = jsonResponse.records[0].Apttus__Contract_End_Date__c;

                obj['start'] = startDate.toString();
                obj['end'] = endDate.toString();

                var accountName = jsonResponse.records[0].Account_Name__c;

                var gr = new GlideRecord('core_company');
                gr.addEncodedQuery('nameSTARTSWITH' + accountName);
                gr.setLimit(1);
                gr.query()
                if (gr.next()) {
                    obj['company'] = gr.getValue("sys_id");
                }
                return JSON.stringify(obj);
            }
            return '';
        } catch (ex) {
            var message = ex.message;
            gs.error("Error in getCrnNum: " + message);
        }
    },

    type: 'clmCrmIntegration'
});

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

Hi @Ankur Bawiskar ,

 

Tried with the same script you shared, but still giving me the same error

 

PrathameshChav_0-1740122981486.png

 

@Prathamesh Chav 

did you debug script include and see what came in log?

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