function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Frank van Meegen 13Frank van Meegen 13 

How to parse REST Post responce Id in JSON format to a Salesforce field

With the following class I have managed to succesfully create and post a JSON string from Salesforce to an external system:
 
public class WorkOrderAppPost {
  @future (callout=true)
    public static void postWorkOrder() {
Case c = [SELECT Id,
CaseNumber,
type,
WorkorderApp__Payment_Method__c,
account.name,
account.id,
account.ShippingStreet,
account.ShippingPostalCode,
account.ShippingCity,
account.BillingStreet,
account.BillingPostalCode,
account.BillingCity,
contact.id,
Contact.Name
 from Case Limit 1] ;  
    JSONGenerator gen = JSON.createGenerator(true);    
	gen.writeStartObject();      
	gen.writeStringField('WorkorderNo', c.Id);
	gen.writeStringField('ExternProjectNr',c.CaseNumber);
	gen.writeStringField('CustomerName',c.account.name);
	gen.writeStringField('CustomerDebtorNr',c.account.id);
	gen.writeStringField('CustomerStreet',c.account.ShippingStreet);
 	gen.writeStringField('CustomerZIP',c.account.ShippingPostalCode);
	gen.writeStringField('CustomerCity',c.account.ShippingCity);
	gen.writeStringField('CustomerContactPerson',c.Contact.Name);
	gen.writeStringField('CustomerNameInvoice',c.account.name);
	gen.writeStringField('CustomerDebtorNrInvoice',c.account.id);
	gen.writeStringField('CustomerStreetInvoice',c.account.BillingStreet);
	gen.writeStringField('CustomerZIPInvoice',c.account.BillingPostalCode);
    gen.writeStringField('CustomerCityInvoice',c.account.BillingCity);
	gen.writeStringField('CustomerContactPersonInvoice',c.Contact.Name);
	gen.writeStringField('TypeOfWork',c.type);
	gen.writeStringField('PaymentMethod',c.WorkorderApp__Payment_Method__c);        
	gen.writeEndObject();    
	String jsonS = gen.getAsString();
System.debug('jsonMaterials'+jsonS);

// Sending the http body with JSON 
        
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://www.externalsystem.com');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
// Set the body as a JSON object
request.setbody(jsonS);
HttpResponse response = http.send(request);
// Parse the JSON response
if (response.getStatusCode() != 200) {
    System.debug('The status code returned was not expected: ' +
        response.getStatusCode() + ' ' + response.getStatus());
} else {
    System.debug(response.getBody());

}

        
        
    }
}
The external system responds with the following code when succesfull:
 
{"code":200,"messages":[],"response":[{"workorder_no":"5000Y00000FtqLDQAZ","row_id":597665}]}

I would like to parse the row_id value (597665) into the case record that has been posted to the external system in the field "WorkorderApp__WorkOrder_Id__c" How can I achieve this goal?
Best Answer chosen by Frank van Meegen 13
Raj VakatiRaj Vakati
JSONParser parser = JSON.createParser(res.getBody());

        System.JSONToken token;
        String  rowId;

        parser.nextToken();
        while((token = parser.nextToken()) != null) {
            // Parse the object
            if ((token = parser.getCurrentToken()) != JSONToken.END_OBJECT) {
                text = parser.getText();
                if (token == JSONToken.FIELD_Name && text == 'row_id') {
                    token=parser.nextToken();
                    rowId = parser.getText();
                 }
				}
				}
				
				
				WorkorderApp__WorkOrder_Id__c = rowId ; 
				// Update WO ;