You need to sign in to do that
Don't have an account?
Reshmi Smiju
Issues while inserting Account with data coming from external site
Hi,
I am trying to insert accounts with data coming from an external site. Being a beginner in Integration, I was trying to do this. But got an error from VF page while inserting the account like "Error is in expression '{!makecall}' in component <apex:commandButton> in page ext_rest_callout_page: Class.EXT_REST_Callout_AccCreation.makecall: line 21, column 1"
Below is my code : CONTROLLER
public class EXT_REST_Callout_AccCreation{
public String output{get;set;}
public List<Wrapper> wrap_data {get;set;}
public void makecall(){
HttpRequest req = new HttpRequest();
req.setEndPoint('http://jsonplaceholder.typicode.com/posts');
req.setMethod('GET');
Http h = new Http();
HttpResponse res = h.send(req);
output = res.getbody();
wrap_data = (List<Wrapper>)JSON.deserialize(output,List<Wrapper>.class);
Account acc = new Account();
List<Account> accs = new List <Account>();
for (Wrapper w: wrap_data ){
acc.Name = w.Title;
acc.Description = w.Body;
accs.add(acc);
}
insert accs;
}
public class Wrapper{
public Account acc {get;set;}
public Integer UserId{get;set;}
public Integer Id{get;set;}
public String Title{get;set;}
public String Body{get;set;}
public Wrapper(Integer u, Integer i, String t, String b){
UserId = u;
Id = i;
Title = t;
Body = b;
}
}
}
The I tried to display the Data "wrap_data " in a VF Page, and it was coming fine. But after adding the code to insert the account with the JSON data, i got the error. Pls let me know the correct approach to do the same.
My VF Code
<apex:page controller="EXT_REST_Callout_AccCreation">
<apex:form >
<apex:pageBlock title="REST Callout Response Data">
<apex:commandButton action="{!makecall}" value="Show Response Data"/>
<apex:pageBlockTable value="{!wrap_data}" var="w">
<apex:column value="{!w.UserId}" headerValue="User ID"/>
<apex:column value="{!w.Id}" headerValue="ID"/>
<apex:column value="{!w.Title}" headerValue="Title"/>
<apex:column value="{!w.Body}" headerValue="Body"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Thank you,
Regards
I am trying to insert accounts with data coming from an external site. Being a beginner in Integration, I was trying to do this. But got an error from VF page while inserting the account like "Error is in expression '{!makecall}' in component <apex:commandButton> in page ext_rest_callout_page: Class.EXT_REST_Callout_AccCreation.makecall: line 21, column 1"
Below is my code : CONTROLLER
public class EXT_REST_Callout_AccCreation{
public String output{get;set;}
public List<Wrapper> wrap_data {get;set;}
public void makecall(){
HttpRequest req = new HttpRequest();
req.setEndPoint('http://jsonplaceholder.typicode.com/posts');
req.setMethod('GET');
Http h = new Http();
HttpResponse res = h.send(req);
output = res.getbody();
wrap_data = (List<Wrapper>)JSON.deserialize(output,List<Wrapper>.class);
Account acc = new Account();
List<Account> accs = new List <Account>();
for (Wrapper w: wrap_data ){
acc.Name = w.Title;
acc.Description = w.Body;
accs.add(acc);
}
insert accs;
}
public class Wrapper{
public Account acc {get;set;}
public Integer UserId{get;set;}
public Integer Id{get;set;}
public String Title{get;set;}
public String Body{get;set;}
public Wrapper(Integer u, Integer i, String t, String b){
UserId = u;
Id = i;
Title = t;
Body = b;
}
}
}
The I tried to display the Data "wrap_data " in a VF Page, and it was coming fine. But after adding the code to insert the account with the JSON data, i got the error. Pls let me know the correct approach to do the same.
My VF Code
<apex:page controller="EXT_REST_Callout_AccCreation">
<apex:form >
<apex:pageBlock title="REST Callout Response Data">
<apex:commandButton action="{!makecall}" value="Show Response Data"/>
<apex:pageBlockTable value="{!wrap_data}" var="w">
<apex:column value="{!w.UserId}" headerValue="User ID"/>
<apex:column value="{!w.Id}" headerValue="ID"/>
<apex:column value="{!w.Title}" headerValue="Title"/>
<apex:column value="{!w.Body}" headerValue="Body"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Thank you,
Regards
Try below controller, let me know if it works.
All Answers
Try below controller, let me know if it works.