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
raz rraz r 

how to get Accounts with related contacts using Rest API callout and display in VF page

Hi All,

how to get Accounts with related contacts using Rest API callout and display in VF page .

Please help me ASAP.

Thanks .
GauravGargGauravGarg
When you want to display data in VF page, what is the use of Rest API? Can you elaborate your requirement. 
raz rraz r
Hi Gaurav,
The requirement is:
Create Webservice using apex REST.Response should returns accounts and contacts in the org. Group contacts by Account Name and disply account name with respective contacts in vf page. Also create a unmanaged package for this.

Please assist.

Thanks!
GauravGargGauravGarg
Hi Raz,

Create a Wrapper class like below
 
private class AccountContactWrapper
{	
	private String AccountName{get;set;}
	private List<ContactWrapper> childContacts{get;set;}
	
	public AccountContactWrapper(String aname)
	{
		AccountName = aName;
		childContacts = new List<ContactWrapper>();
	}
​}

private class ContactWrapper
{
	private String firstname {get;set;}
	private String lastname {set;set;}
	private string email {get;set;}
	
	public ContactWrapper(String fname, String lname, String emailAddress)
	{
		firstname = fname;
		lastname = lname;
		email = emailAddress;
	}
}

Parse JSON data into this wrapper class, and then display accordingly. 

Thanks,
Gaurav 
raz rraz r
I have created the Wrapper class as below:

public  class Jsonwrapper
{    
    public Integer totalSize;
    public boolean done;
    public List<Account> records;
    //public String AccountName{get;set;}
    public List<ContactWrapper> childContacts{get;set;}
    
    public Jsonwrapper(integer p_tot,boolean p_done,List<Account> p_rec)
    {
         totalSize=p_tot;
         done=p_done;
         records=p_rec;
        childContacts = new List<ContactWrapper>();
    }


public class ContactWrapper
{
    public Integer totalSize;
    public boolean done;
    public List<Contact>records;
    //public String firstname {get;set;}
    //public String lastname {set;set;}
    //public string email {get;set;}
    
    public ContactWrapper(integer p_tot,boolean p_done,List<Contact> p_rec)
    {
        totalSize=p_tot;
         done=p_done;
         records=p_rec;
        //firstname = fname;
        //lastname = lname;
        //email = emailAddress;
    }
}
}

the wrapper reult is showing only Account.
DEBUG|wrapperJsonwrapper:[AccountName=null, childContacts=null, done=true, records=(Account:{Id=00190000016V11FAAS, Name=birlasoft}), totalSize=1]

Please assist.
GauravGargGauravGarg
You need to add all the ChildContact records in wrapper. 

 
raz rraz r
Can you please assist me on this..
GauravGargGauravGarg
can you send me the code. I will update it in
raz rraz r
Apex class:
----------------------
public class AccountswithReplatedContacts {
    public list<Account>acc{get;set;}
    public String Accountname{get;set;}
    public String Response {get;set;}
    Public JSONwrapper wrapper{get;set;}
    
    public AccountswithReplatedContacts(){
        
        //getresposnsefromCallout();
    }
    
    public void getresposnsefromCallout(){
        //String search=''+Accountname+'';
       // system.debug('searchstring'+search);
String Endpoint='https://'+URL.getSalesforceBaseUrl().getHost()+'/services/data/v39.0/query/?q=SELECT +Id,Name ,(Select +id,name +from+contacts)+FROM + Account + where + name=\''+String.escapeSingleQuotes(Accountname)+'\'';
         System.debug('Endpoint'+Endpoint);
         HttpRequest req = new HttpRequest();
        req.setMethod('GET');
        string autho = 'Bearer '+userInfo.getSessionId();
        req.setHeader('Authorization', autho);
        req.setEndpoint(Endpoint);
         Http http = new Http();
        HTTPresponse res= http.send(req);
        response = res.getBody();
        system.debug('response'+response);
       wrapper= (JSONwrapper) System.JSON.deserialize(response, JSONwrapper.class);
        system.debug('wrapper'+wrapper);
        //wrapper = (JSONwrapper) JSON.deserializeStrict(response, JSONwrapper.class);
                }
 
    }

------------------
Wrapper class
---------------------------
public  class Jsonwrapper
{    
    public Integer totalSize;
    public boolean done;
    public List<Account> records;
    //public String AccountName{get;set;}
    public List<ContactWrapper> childContacts{get;set;}
    
    public Jsonwrapper(integer p_tot,boolean p_done,List<Account> p_rec)
    {
         totalSize=p_tot;
         done=p_done;
         records=p_rec;
        childContacts = new List<ContactWrapper>();
    }


public class ContactWrapper
{
    public Integer totalSize;
    public boolean done;
    public List<Contact>records;
    //public String firstname {get;set;}
    //public String lastname {set;set;}
    //public string email {get;set;}
    
    public ContactWrapper(integer p_tot,boolean p_done,List<Contact> p_rec)
    {
        totalSize=p_tot;
         done=p_done;
         records=p_rec;
        //firstname = fname;
        //lastname = lname;
        //email = emailAddress;
    }
}
}

Please help me..
GauravGargGauravGarg
In JSON Wrapper where are Contact records?
raz rraz r
that is records attribute in json result
 
raz rraz r
Hi Gaurav,

Did you find any solution for this..


 
GauravGargGauravGarg
Raz,

I am little confused over here. You are just passing List<Account> in controller, are contact's part of Account? 

If yes, then it will be travesed something like this
for(Contact c : Account.Contacts)
{
    //contact records
}

Can you check contact records available. Or if possible pass me the JSON file 
raz rraz r
Hi Gaurav,

Please fidn the below JSON response:

response{"totalSize":1,"done":true,"records":[{"attributes":{"type":"Account","url":"/services/data/v39.0/sobjects/Account/00190000016V11FAAS"},"Id":"00190000016V11FAAS","Name":"birlasoft","Contacts":{"totalSize":6,"done":true,"records":[{"attributes":{"type":"Contact","url":"/services/data/v39.0/sobjects/Contact/0039000002MYh46AAD"},"Id":"0039000002MYh46AAD","Name":"Krishna K"},{"attributes":{"type":"Contact","url":"/services/data/v39.0/sobjects/Contact/0039000002MYgxfAAD"},"Id":"0039000002MYgxfAAD","Name":"Krishna K"},{"attributes":{"type":"Contact","url":"/services/data/v39.0/sobjects/Contact/0039000002MYgXrAAL"},"Id":"0039000002MYgXrAAL","Name":"Riaz Test"},{"attributes":{"type":"Contact","url":"/services/data/v39.0/sobjects/Contact/0039000002MYhF4AAL"},"Id":"0039000002MYhF4AAL","Name":"Deepika HR"},{"attributes":{"type":"Contact","url":"/services/data/v39.0/sobjects/Contact/0039000002MYhHoAAL"},"Id":"0039000002MYhHoAAL","Name":"Romit Shilpe"},{"attributes":{"type":"Contact","url":"/services/data/v39.0/sobjects/Contact/0039000001AlZ2XAAV"},"Id":"0039000001AlZ2XAAV","Name":"Riazgood boy"}]}}]}
 
GauravGargGauravGarg
public class AccountswithReplatedContacts {
    public list<Account>acc{get;set;}
    public String Accountname{get;set;}
    public String Response {get;set;}
    Public JSONwrapper wrapper{get;set;}
    
    public AccountswithReplatedContacts(){
        
        //getresposnsefromCallout();
    }
    
    public void getresposnsefromCallout(){
        //String search=''+Accountname+'';
       // system.debug('searchstring'+search);
String Endpoint='https://'+URL.getSalesforceBaseUrl().getHost()+'/services/data/v39.0/query/?q=SELECT +Id,Name ,(Select +id,name +from+contacts)+FROM + Account + where + name=\''+String.escapeSingleQuotes(Accountname)+'\'';
         System.debug('Endpoint'+Endpoint);
         HttpRequest req = new HttpRequest();
        req.setMethod('GET');
        string autho = 'Bearer '+userInfo.getSessionId();
        req.setHeader('Authorization', autho);
        req.setEndpoint(Endpoint);
         Http http = new Http();
        HTTPresponse res= http.send(req);
        response = res.getBody();
        system.debug('response'+response);
       wrapper= (JSONwrapper) System.JSON.deserialize(response, JSONwrapper.class);
        system.debug('wrapper'+wrapper);
        //wrapper = (JSONwrapper) JSON.deserializeStrict(response, JSONwrapper.class);
                }
 
    }

------------------
Wrapper class
---------------------------
public  class Jsonwrapper
{    
    public Integer totalSize;
    public boolean done;
    List<AccountWrapper> accWrapper = new List<AccountWrapper>();
    public Jsonwrapper(integer p_tot,boolean p_done,List<Account> p_rec)
    {
         totalSize=p_tot;
         done=p_done;
		 for(Account acc : p_rec)
		 {
			AccountWrapper aw = new aw(acc.Name);
			List<ContactWrapper> conWrapper = new List<Contact>();
			for(Contact con : acc.Contacts)
			{
				ContactWrapper cw = new ContactWrapper(con.totalSize, con.done, con.records);
				conWrapper.add(cw);
			}
			aw.conList.addAll(conWrapper);
		 }
			accWrapper.addAll(aw);
		 }

	
	public class AccountWrapper
	{
		Public String name{get;set;}
		public List<ContactWrapper> conList{get;set;}
		
		public AccountWrapper(String n)
		{
			name = n;
			conList = new List<ContactWrapper>();
		}
	}

public class ContactWrapper
{
    public Integer totalSize;
    public boolean done;
    public contact record;
    
    public ContactWrapper(integer p_tot,boolean p_done,Contact p_rec)
    {
        totalSize=p_tot;
        done=p_done;
        records=p_rec;
    }
}
}

Try this. 
raz rraz r
I am trying to complie so many syntax issues are coming in Wrapper class.Could you please try in your trail org ..
raz rraz r
Gaurav,

Please help me on this..
GauravGargGauravGarg
What syntax are you facing, can we have a screen sharing session to solve this issue
raz rraz r
ok.

 
GauravGargGauravGarg
Contact me on skype: gaurav62990
raz rraz r
User-added image

Please see the attched screenshot 
GauravGargGauravGarg
got it, can you debug what in "Account.Contacts" and check which attritbute contains the records and other values. 
raz rraz r
Iam not getting the Debug statement from wrapper class.
raz rraz r
Can you please test it in your triail org ocne,
GauravGargGauravGarg
Write debug statement in Contacts for loop. 
// Add debug state for Account also 
system.debug('Accounts: '+ acc);

for (Contact con : Acc.Contacts)
{
   system.debug(con);
}
Use hit and try method to got your solutions. 
VIKAS SRIVASTVAVIKAS SRIVASTVA
can u please give me the correct and modified code?
Gaurav or Raj plz help me out.
VIKAS SRIVASTVAVIKAS SRIVASTVA
GauravGarg plz reply