+ Start a Discussion
Sai ThejaSai Theja 

How to save post method json response in to salesforce Account Field

Hello,
I have post method to store salesforce data to external system. In response i'm getting Id from that system. how to save that id in salesforce Account field (Client_id__c).
below is my code:

Account a = [SELECT Id,Name, Client_City__c,Payroll_Contact__c, Client_Address__C,Client_Zip__c ,Primary_Contact__c,State_Code__c,Phone__c,State__c, Contact_Name__c, Phone,(select Name from contacts),(select Name from location__r)  from Account where id = '0012C00000LcvQn'] ;  
JSONGenerator gen = JSON.createGenerator(true);    
    gen.writeStartObject();      
    gen.writeStringField('name',a.name);
    gen.writeStringField('address_line_1',a.Client_Address__c);
    gen.writeStringField('city',a.Client_City__c);
    gen.writeStringField('state_code',a.State_Code__c );
    gen.writeStringField('zip_code',a.Client_Zip__c );
    gen.writeStringField('sf_id',a.Id);
    gen.writeFieldName('primary_contact_attributes');
    gen.writeStartObject();        
    gen.writeObjectField('name', a.contacts[0].Name);
    gen.writeObjectField('phone', a.Phone__c);  
    gen.writeEndObject();    
    gen.writeFieldName('payroll_contact_attributes');
    gen.writeStartObject();        
    gen.writeObjectField('name', a.Payroll_Contact__c);
    gen.writeObjectField('phone', a.Phone__c);  
    gen.writeEndObject();    

    gen.writeEndObject();    
    String jsonS = gen.getAsString();
System.debug('jsonMaterials'+jsonS);

Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api-qa.retrotax-aci.com/clients');
request.setMethod('POST');
request.setHeader('X-AUTH-TOKEN','xxxxxxxxxxxxxxxxxxxxxxxx');
request.setHeader('X-API-KEY', 'xxxxxxxxxxxxxxxxxxxxxxxxxx');
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('Client Created Successfully: ' +
        response.getStatusCode() + ' ' + response.getBody());
} else {
    System.debug('Unable to create Client: ' +
        response.getStatusCode() + ' ' + response.getBody());
   }


Response Screen Shot
User-added imageHere im getting Id:655, i need to store that id in salesforce.

 
Best Answer chosen by Sai Theja
Ahmed Ansari 13Ahmed Ansari 13

for this you can try like this

String jsonString = '{"Name":"ABC","AccountNumber":"1312321"}';

Account act = (Account) JSON.deserialize(jsonString, Account.class);

System.debug('Account:'+act.Name);

All Answers

Ahmed Ansari 13Ahmed Ansari 13

for this you can try like this

String jsonString = '{"Name":"ABC","AccountNumber":"1312321"}';

Account act = (Account) JSON.deserialize(jsonString, Account.class);

System.debug('Account:'+act.Name);

This was selected as the best answer
Lokesh KumarLokesh Kumar
Use this code snippet
public class Car {
    public String make;
    public String year;
}

public void parse() {        
    Car c = (Car)JSON.deserializeStrict(
        '{"make":"SFDC","year":"2020"}',
        Car.class);
    System.assertEquals(c.make, 'SFDC');
    System.assertEquals(c.year, '2020');
}