2

I am trying to update the status field of the master record (Service Request) to close, when all the subsequent child activities status is closed.

Here is the code:

trigger TriggerSR_StatusUpdate on Activities__c (after update) {
  //Fetching all SR Ids in map
  List<Activities__c> SR_Ids1 = new List<Activities__c>(
    [SELECT ServiceRequest__r.id FROM Activities__c 
     where Status__c='Close' and id in :Trigger.newMap.keySet()]);

  Set<Id> SR_Ids = new Set<Id>();
  for(Activities__c act : SR_Ids1)
  {
     SR_Ids.add(act.ServiceRequest__c);
  }
  //fetching status of the activities for the corresponding Service Request
  Map<Id,ServiceRequest__c > SRMap = new Map<Id,ServiceRequest__c >(
    [SELECT id,(select Status__c from Activities__r) 
    from ServiceRequest__c where Id IN:SR_Ids]);

  List<ServiceRequest__c> SrStatus =SRMap.values();
}

I am getting an exception:

System.SObjectException: SObject row was retrieved via SOQL 
without querying the requested field

What does this mean?

1
  • 3
    This isn't your question, but in case you were interested, your query isn't actually necessary. Everything on that object is already loaded into trigger.new. Instead of doing a query you could just loop over that and collect an Activities__c that have a status__c = 'Closed' Mar 5 '13 at 3:40
8

You are accessing act.ServiceRequest__c in your for loop

for(Activities__c act : SR_Ids1)
{
   SR_Ids.add(act.ServiceRequest__c);
}

but didn't query for it first in your SOQL.

Change:

List<Activities__c> SR_Ids1= new List<Activities__c>([
    SELECT ServiceRequest__r.id 
    FROM Activities__c 
    where Status__c='Close' and id in :Trigger.newMap.keySet()]);

To include ServiceRequest__c.

List<Activities__c> SR_Ids1= new List<Activities__c>([
    SELECT ServiceRequest__r.id, ServiceRequest__c /* add that */
    FROM Activities__c 
    where Status__c='Close' and id in :Trigger.newMap.keySet()]);
0

How to reproduce the exception:

System.SObjectException: SObject row was retrieved via SOQL without 
querying the requested field: Opportunity.stuff__c

Code:

private static Opportunity reloadOpp(Opportunity opp) {
    Id oppId = opp.Id;
    return [Select Id From Opportunity Where Id = :oppId]; //stuff__c not selected here
}

static testmethod void testFoobar() {
    Opportunity opp = new Opportunity();        
    opp.Description = 'Carrier';
    opp.Type = 'Other';
    opp.Name = 'foobar';
    opp.StageName = 'PDP';

    try{
        insert opp;
    }
    catch(Exception e){
        System.debug(Logginglevel.Error, '' + e);
    }
    Opportunity opp2 = reloadOpp(opp);
    System.assert(opp2.stuff__c.equals('foobar'));    //so the error thrown here.
}

When you fetch the value opp2.stuff__c, the compiler notices that you never pulled that value from the object returned by the method reloadOpp. So you get the exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.