You need to sign in to do that
Don't have an account?
manoj6492
Apex code error
@RestResource(urlMapping='/MyAccount/*') global with sharing class wrapper { @HttpGet global static String MyAccount() { RestRequest req = RestContext.request; RestResponse res = RestContext.response; list<Account> acc = [Select name ,Id, (Select Id, lastname from Contacts)From Account limit 10]; list<Accountw> aw = new list<Accountw>(); list<ContactW> con= new list<ContactW> (); for(Account ac :acc) { for(contact c :ac.Contacts) { contactW c1 = new contactW(c.name,c.Email); con.add(c1); } Accountw a = new Accountw(ac.id,ac.name,con); aw.add(a); } return JSON.serialize(aw); } class Accountw { string Ids{get;set;} string Name{get;set;} list<contactW> con{get;set;} Accountw(String ids,string name,list<contactW> con1) { Ids=ids; Name=name; con= con1; } } class contactW { string cname{get;set;} string Email{get;set;} contactW(string name ,string Email) { cname = name; Email = Email; } } }
when i execut this code i get the following error:
- message: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Contact.Name Class.wrapper.MyAccount: line 17, column 1
- errorCode: APEX_ERROR
what changes should be done in order to overcome this error
Change like this
@RestResource(urlMapping='/MyAccount/*')
global with sharing class wrapper
{
@HttpGet
global static String MyAccount()
{
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
list<Account> acc = [Select name ,Id, (Select Id, name from Contacts)From Account limit 10];
list<Accountw> aw = new list<Accountw>();
list<ContactW> con= new list<ContactW> ();
for(Account ac :acc)
{
for(contact c :ac.Contacts)
{
contactW c1 = new contactW(c.name,c.Email);
con.add(c1);
}
Accountw a = new Accountw(ac.id,ac.name,con);
aw.add(a);
}
return JSON.serialize(aw);
}
class Accountw
{
string Ids{get;set;}
string Name{get;set;}
list<contactW> con{get;set;}
Accountw(String ids,string name,list<contactW> con1)
{
Ids=ids;
Name=name;
con= con1;
}
}
class contactW
{
string cname{get;set;}
string Email{get;set;}
contactW(string name ,string Email)
{
cname = name;
Email = Email;
}
}
}
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
Im getting the same output...no change in output
You missed the email as well
list<Account> acc = [Select name ,Id, (Select Id, name,email from Contacts)From Account limit 10];
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
i dint miss anything i have added email too
Please share the exact error which is coming now and in which line.
Thanks