Skip to main content
Tweeted twitter.com/#!/StackSalesforce/status/357823999599837186
Code formatted
Source Link
Sergej Utko
  • 22.1k
  • 11
  • 60
  • 88

`@RestResource(urlMapping='/MyAccount/*') global with sharing class wrapper { @HttpGet global static String MyAccount() { RestRequest req = RestContext.request; RestResponse res = RestContext.response; list acc = [Select name ,Id, (Select Id, lastname from Contacts)From Account limit 10]; list aw = new list(); list con= new list (); for(Account ac :acc) {

@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 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

`@RestResource(urlMapping='/MyAccount/*') global with sharing class wrapper { @HttpGet global static String MyAccount() { RestRequest req = RestContext.request; RestResponse res = RestContext.response; list acc = [Select name ,Id, (Select Id, lastname from Contacts)From Account limit 10]; list aw = new list(); list con= new list (); 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

@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

Source Link
Manoj Chandran
  • 1.2k
  • 4
  • 26
  • 56

Apex code error while executing

`@RestResource(urlMapping='/MyAccount/*') global with sharing class wrapper { @HttpGet global static String MyAccount() { RestRequest req = RestContext.request; RestResponse res = RestContext.response; list acc = [Select name ,Id, (Select Id, lastname from Contacts)From Account limit 10]; list aw = new list(); list con= new list (); 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