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

sunil maddheshi
Tera Guru

@Prathamesh Chav 

Your script is modifying responseBody.records[0] directly, but responseBody is a string, not a JSON object. You need to parse it first before modifying it.

 

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();  // Response is a string
    var httpStatus = response.getStatusCode();
}
catch (ex) {
    var message = ex.message;
}

if (httpStatus == 200) {
    var jsonResponse = JSON.parse(responseBody);  // Convert string to JSON
    
    var compSys = new GlideRecord("core_company");
    compSys.addEncodedQuery("nameSTARTSWITH" + jsonResponse.records[0].Account_Name__c);
    compSys.query();
    
    var sys_Comp = "";
    if (compSys.next()) {
        sys_Comp = compSys.getValue("sys_id"); // Fetch sys_id correctly
    }

    jsonResponse.records[0].sys_idd = sys_Comp;  // Modify JSON object

    return JSON.stringify(jsonResponse);  // Convert JSON back to string
}

 

Please mark correct/helpful if this help you!

Hi @sunil maddheshi ,

 

I tried this approach but didn't work

What issue/error are you getting?