1

I get the following error in my SalesForce app:

Error:Apex trigger UpdateOpportunity caused an unexpected exception, contact your administrator: UpdateOpportunity: execution of BeforeUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Opportunity.Unsolicited_Bid__c: Class.OpportunityMethods.updateOppCompetitor: line 11, column 1

I see there are a lot of similar questions, but I've tried their solutions and still can't get this query to work correctly.

Here is the trigger I have:

trigger UpdateOpportunity on Opportunity (before insert, before update){

    Opportunity[] oppQuery = [SELECT ID, RecordType.Name, StageName, Win_Reason__c FROM Opportunity WHERE ID IN :Trigger.new];  

    for(Opportunity opp : oppQuery){

        if(opp.RecordType.Name == 'SGI Opportunity' && opp.StageName != 'Lead 0%'){

            OpportunityMethods.updateOppCompetitor(opp);  
    }
}

And here's the class I use in the trigger that has Win_Reason__c, the field in question.

public Class OpportunityMethods {

public static void updateOppCompetitor(Opportunity opp){

    if(opp.Win_Reason__c == 'No Competition/Sole Source'){

    System.debug('THIS WORKED');

        opp.Competitor_s__c = 'No Competitor/Sole Source'; 
    }
    if(opp.Unsolicited_Bid__c == 'Yes'){


    System.debug('THIS WORKED');


        opp.Competitor_s__c = 'No Competitor/Sole Source';
    } 
}

}

Any clue what else I need to add to the SOQL query to make it run correctly?

Thanks in advance!

7

Please add the column Unsolicited_Bid__c to your oppQuery.

That is what the exception System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Opportunity.Unsolicited_Bid__ was trying to relay, that there was an attempt to get/set a field on an sobject from a query that originally did not query that field.

As an aside, do you have a specific reason for re-querying the opportunities inside the trigger? I believe you can reference and loop through Trigger.new and reference any of the opportunity fields and likely avoided this exception initially:

for(Opportunity opp : Trigger.new){
    if(opp.RecordType.Name == 'SGI Opportunity' && opp.StageName != 'Lead 0%'){
        OpportunityMethods.updateOppCompetitor(opp);  
    }
}
  • lol wow. I totally overlooked that. Thank you so much and sorry for the stupid question. :( – user3708392 Sep 8 '14 at 12:57
-1

You need to add Unsolicited_Bid__c to your SOQL query.

  • 2
    Brian -- although technically correct; we at SFSE strive to make our answers more than one liners. For example, you could have pasted in the corrected query and perhaps also explained how APEX uses soql fields differently depending on whether they are on the left side of an assignment or referenced in an expression – cropredy Sep 5 '14 at 22:27
  • 1
    Pretty silly to downvote a correct answer. – Brian Cline Sep 8 '14 at 14:41
  • 1
    @crop1645: May be you can strive to better your documentation on such and all issues. But I understand its easier to correct other people who are silly enough on trying to help rather than correcting oneself. – Asad Malik Jun 8 '15 at 19:23

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.