You need to sign in to do that
Don't have an account?
Apex Error On Class
The purpose of this class is to update a custom field on the Contact record of contacts associated with an opportunity via Opportunity Contact Role, when the Opportunity is a specific stage (Prospecting or Qualification).
Currently, when an Opportunity is created or updated I get an error:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger OpportunityAgentStatus caused an unexpected exception, contact your administrator: OpportunityAgentStatus: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Class.OpportunityAgentStageProspect.prospect: line 15, column 1
Obviously something is wrong with how I'm pulling contacts but I can't figure it out.
I'd appreciate the help.
Thanks.
public class OpportunityAgentStageProspect { public static void prospect(Opportunity[] agentopps) { for (Opportunity opp :agentopps){ Opportunity opp2 = [select ID, StageName from Opportunity where ID = :opp.Id]; if (opp2.StageName == 'Prospecting' || opp2.StageName == 'Qualification') { OpportunityContactRole[] contacts = [select ID from OpportunityContactRole where OpportunityID = :opp2.Id]; for (OpportunityContactRole con :contacts){ Contact c = [select ID, Agent_Status__c from Contact where ID = :con.Id]; c.Agent_Status__c = 'Prospect'; update c; } } } } }
Contact c = [select ID, Agent_Status__c from Contact where ID = :con.ContactID];
All Answers
Hello,
You're retrieving OpportunityContactRole, and than searching by the id of OpportunityContactRole in the Contact table, so abiouveisly it won't find any Contact.
I assume it should be something like:
In addition you have 2 SQL + 1 DML inside a loop.
It's not best practive, and likely to failed on limitation exception if you will update in bulk.
I made the changes but got another error:
Initial term of field expression must be a concrete SObject: LIST<OpportunityContactRole>, line# = 15, column# = 72, id = null, fullname = OpportunityAgentStageProspect
(2) filename = package.xml, result = SUCCESS, affect = changed, id = null, fullname = package.xml
Here is the udpated code:
Contact c = [select ID, Agent_Status__c from Contact where ID = :con.ContactID];
Awesome. Thanks much for the help.
I updated the code to handle bulk usage.
You are still using SQL inside the loop - for each Opportunity, you are queting the OpportunityContactRole and the Contact
try this
Thanks. I've been struggline with another class and failing in a bulk update, and this helps explain what I need to do very clearly.