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

2 Answers 2

3

In your select statement you select the fields ID and lastName from Contact

Select name ,Id, (Select Id, lastname from Contacts)From Account

You then on line 17 go on to refer to the name and email fields from Contact. The error says "row was retrieved via SOQL without querying the requested field: Contact.Name"

You haven't retrieved the name or the email field in your SOQL query.

Your query should look more like

Select name ,Id, (Select Id, lastname, name, email from Contacts) From Account

The error message is actually pretty helpful!

6
  • thank you ....but the out put is not as expected :-( Commented Jul 18, 2013 at 9:32
  • In what way is the output not as expected? You mean that your error is resolved but your code doesn't do what you thought it was going to?
    – Doug B
    Commented Jul 18, 2013 at 9:41
  • ya the error is resolved but i dint get the expected output Commented Jul 18, 2013 at 9:43
  • @ManojChandran You need not serialize as JSON in last step.The platform automatically converts to JSON if method is HTTP annotated Commented Jul 18, 2013 at 9:44
  • @ManojChandran Return Type can be Account Wrapper Itself .You can return Account Wrapper List and the format will be JSON Commented Jul 18, 2013 at 9:45
0

I got the output for the code that I have mentioned in my question

The correct code is

@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  name, Email from Contacts)From Account  where ActiveStatus__c=true];
        list<Accountw> aw = new list<Accountw>();

        for(Account ac :acc)
        {
            system.debug('ac1');
            list<ContactW> con= new list<ContactW> ();
            system.debug('newlis:'+con);
            for(contact c :ac.Contacts)
            {   
                system.debug('contact>>'+c);
                contactW c1 = new contactW(c.name,c.email);
                con.add(c1);
            }
            Accountw a = new Accountw(ac.Id,ac.name,con);
            aw.add(a);
        }
        system.debug('the list :'+Json.serialize(aw));
        return Json.serialize(aw);
    }
    global class Accountw
    {
        string Ids{get;set;}
        string Name1{get;set;}
        list<contactW> con{get;set;}
        Accountw(String id,string name,list<contactW> con1)
        {
            Ids=id;
            Name1=name;
            con= con1;
        }
    }
    class contactW
    {
        string cname{get;set;}
        string Email1{get;set;}
        contactW(string name ,string  email)
        {
            cname = name;
            Email1 = email;
        }
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .