@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);//17 th line
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 execute 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