You need to sign in to do that
Don't have an account?
abhishek 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());
}
}
}
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());
}
}
}
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.