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
abhishek singh 497abhishek singh 497 

inserting data into salesforce receiving from third party (json)

Hello Team,
 I have json data of account and contact and want to insert the same into the salesforce, So I have made Restapi using wrapper class method.I am pasting JSON data, apex class.Please let me know what more things i need to change in my code.

Json data:-
{
  "accountList": [
    {
      "Name": "testPlC",
      "ActivationDate": "2011-10-04T16:58:54.858Z",
      "ContactItems": [
        {
          "FirstName": "PLCTest",
          "LastName": "plsignore",
          "Birthdate": "2011-10-04"
        },
        {
          "FirstName": "PLCTestv2",
          "LastName": "plsignorev2",
          "Birthdate": "2011-10-04"
        }
      ],
      "AccountNumber": "001"
    },
    {
      "Name": "testPlCV2",
      "ActivationDate": "2011-10-05T16:58:54.858Z",
      "ContactItems": [
        {
          "FirstName": "PLCTestv3",
          "LastName": "plsignore",
          "Birthdate": "2011-10-04"
        },
        {
          "FirstName": "PLCTestv4",
          "LastName": "plsignorev2",
          "Birthdate": "2011-10-04"
        }
      ],
      "AccountNumber": "002"
    }
  ]
}
Json class:-

public class JSON2Apex {
    public class AccountList{
        public String Name;
        public Date ActivationDate;
        public String AccountNumber;
        public List<ContactItems> ContactItems;   
    }
    public class ContactItems{
        public String FirstName;
        public String LastName;
        public String Birthdate;    
    }
    public List<AccountList> accountList;
    public static JSON2Apex parse(String json){
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }
}

RestApi class:-
@RestResource(urlMapping='/Account/*')
public class AcctContactInsert {
    public static void listAcctContactInsert(){
        HttpResponse httpResponse;
        try{
            String endpoint ='';
            Http http = new Http();
            HttpRequest httpRequest = new HttpRequest();
            httpRequest.setMethod('GET');
            httpRequest.setEndpoint(endpoint);
            httpResponse = http.send(httpRequest);
            if(httpResponse.getStatusCode() == 200){
                String strResponse = httpResponse.getBody();
                JSON2Apex jsonApex = JSON2Apex.parse(strResponse);
                for(JSON2Apex.AccountList acctList : jsonApex.accountList){
                    System.debug('AccountList:'+acctList.Name+':'+acctList.ContactItems);  
                }
            }
        }
        catch(Exception ex){
            System.debug('Status Code: ' + httpResponse.getStatusCode());
            System.debug('Status Response: ' + httpResponse.getStatus()); 
        }
    }  
}
 
Raj VakatiRaj Vakati
Use this code.. you have parse and insert account and contact 
 
public class JSON2Apex {
    public class AccountList{
        public String Name{get;set;}
        public Date ActivationDate{get;set;}
        public String AccountNumber{get;set;}
        public List<ContactItems> ContactItems{get;set;} 
    }
    public class ContactItems{
        public String FirstName{get;set;}
        public String LastName{get;set;}
        public String Birthdate{get;set;}    
    }
    public List<AccountList> accountList{get;set;}
    public static JSON2Apex parse(String json){
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }
}
 
@RestResource(urlMapping='/Account/*')
public class AcctContactInsert {
    public static void listAcctContactInsert(){
        HttpResponse httpResponse;
        try{
            String endpoint ='';
            Http http = new Http();
            HttpRequest httpRequest = new HttpRequest();
            httpRequest.setMethod('GET');
            httpRequest.setEndpoint(endpoint);
            httpResponse = http.send(httpRequest);
            if(httpResponse.getStatusCode() == 200){
                String strResponse = httpResponse.getBody();
                
				JSON2Apex jsonApex = JSON2Apex.parse(strResponse);
				
				
                for(JSON2Apex.AccountList acctList : jsonApex.accountList){
					
					Account acc = new Account();
					acc.Name = acctList.Name ; 
					acc.AccountNumber = acctList.AccountNumber ; 
					insert acc ; 
					
					List<JSON2Apex.AccountList.ContactItems> conLis =  acctList.ContactItems
			
						for(JSON2Apex.AccountList.ContactItems cnnn : conLis){
							Contact con = new COntact();
							con.FirstName = cnnn.FirstName ; 
							con.LastName = cnnn.LastName ; 
							con.AccountId = acc.Id ; 
							insert con ;
						}
			
                }
            }
        }
        catch(Exception ex){
            System.debug('Status Code: ' + httpResponse.getStatusCode());
            System.debug('Status Response: ' + httpResponse.getStatus()); 
        }
    }  
}

 
abhishek singh 497abhishek singh 497
Hello Raj,
Thanks for the quick response.
Few questions I need to ask:-

1) I have used JSON2APEX convertor which made my job easy.
2) so if without JSON2APEX have to implement and have to use the same wrapper concept is there any way we can do it.

Thanks & Regards,
Abhishek Singh.