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

Chaitanya ILCR
Kilo Patron

Hi @Prathamesh Chav ,

You have to parse the response body to be able to access the data as key value pairs

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) {
    responseBody = JSON.parse(responseBody)
    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);
}

you can try this

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Prathamesh Chav 

your code to query company should be within try block

were you able to parse and get the company name using this -> responseBody.records[0].Account_Name__c

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 ,

 

Still giving the same error after adding my code in try block

PrathameshChav_0-1740049388812.png

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();

       if(httpStatus == 200){
       
            var c = new GlideRecord("core_company");
            c.addEncodedQuery("name=" + responseBody.records[0].Account_Name__c + "^state=INDIA");
            c.query();
            if(c.next()){
                        responseBody.records[0].sysId = c.sys_id;      
                        }

            return JSON.stringify(responseBody);
        }  

}
catch(ex) {
 var message = ex.message;
}
       
Yes I am able to fetch account name but I want sys Id of company name to populate it to the client side for company name field on the form onchange action
 
PrathameshChav_1-1740049583326.png

 

client side

 

@Prathamesh Chav 

please share your complete script include, client script and sample response you are getting

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