You need to sign in to do that
Don't have an account?
raz 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 .
how to get Accounts with related contacts using Rest API callout and display in VF page .
Please help me ASAP.
Thanks .
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!
Create a Wrapper class like below
Parse JSON data into this wrapper class, and then display accordingly.
Thanks,
Gaurav
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.
----------------------
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..
Did you find any solution for this..
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
Can you check contact records available. Or if possible pass me the JSON file
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"}]}}]}
Try this.
Please help me on this..
Please see the attched screenshot
Gaurav or Raj plz help me out.