[SalesForce] Unable to query Opportunity Contact Roles

I'm trying to get a list (and ultimately the ContactIds) of Contact Roles associated with an Opportunity in an after-update trigger, but the size of this list is always 0, even when there are Contact Roles in the Opportunity:

List<OpportunityContactRole> cRoles = [SELECT Id, Contactid FROM OpportunityContactRole WHERE OpportunityId = :opp.Id];

"opp" is the Opportunity that sets off the trigger. Am I doing anything wrong here?

Edit: this is all the code that precedes this line:

trigger OppStageChange on Opportunity (after update) {
    for (Opportunity opp : Trigger.new) {
        List<OpportunityContactRole> cRoles = [SELECT Id, Contactid FROM OpportunityContactRole WHERE OpportunityId = :opp.Id];

Best Answer

1) Can you test running this code in an asynchronous method called by the trigger? I seem to recall Opportunity Contact Roles not being populated til Workflows normally would (after after update triggers fire).

2) I would strongly recommend querying the OpportunityContactRoles using trigger.newMap.keySet() outside of the loop instead, otherwise the trigger is not going to be bulk-safe.

Related Topic