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
Reshmi SmijuReshmi 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
Best Answer chosen by Reshmi Smiju
Ravi Dutt SharmaRavi Dutt Sharma
Hey Rashmi,

Try below controller, let me know if it works.
 
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);
        List<Account> accs = new List <Account>();
        for (Wrapper w: wrap_data ){
            Account acc = new Account();
            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;
        }
    }
}

 

All Answers

Ravi Dutt SharmaRavi Dutt Sharma
Hey Rashmi,

Try below controller, let me know if it works.
 
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);
        List<Account> accs = new List <Account>();
        for (Wrapper w: wrap_data ){
            Account acc = new Account();
            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;
        }
    }
}

 
This was selected as the best answer
Reshmi SmijuReshmi Smiju
Hi Ravi, Thanks for the Prompt response. Yep. Itwas silly mistake. I thought I have got mistake with some other part. Anyway Thank you. :)