• Petteri Koivunen
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
I'm trying to get OpportunityContactRoleId while creating new Opportunity to Contact.

If I'm using Salesforce Classic, the following example works fine.
But if I switch to Lightning, I got error "createupdate_opportunity_trigger: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject Trigger.createupdate_opportunity_trigger: line 7, column 1"

So, this means that OpportunityContactRole is not linked with Opportunity. Why this works if I use Classic? How do I get ContactId when using Lightning?
 
trigger createupdate_opportunity_trigger on Opportunity (after insert, after update) 
{
    for(Opportunity o:Trigger.new) 
    {
        if (o.id != null)
        {
            OpportunityContactRole contactRole = [select ContactId from OpportunityContactRole where IsPrimary = true and OpportunityId = :o.Id];
            if (contactRole != null)
            {
                System.debug('Found: ' + contactRole.ContactID);
            }
        }
    }
}

 
I'm trying to get OpportunityContactRoleId while creating new Opportunity to Contact.

If I'm using Salesforce Classic, the following example works fine.
But if I switch to Lightning, I got error "createupdate_opportunity_trigger: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject Trigger.createupdate_opportunity_trigger: line 7, column 1"

So, this means that OpportunityContactRole is not linked with Opportunity. Why this works if I use Classic? How do I get ContactId when using Lightning?
 
trigger createupdate_opportunity_trigger on Opportunity (after insert, after update) 
{
    for(Opportunity o:Trigger.new) 
    {
        if (o.id != null)
        {
            OpportunityContactRole contactRole = [select ContactId from OpportunityContactRole where IsPrimary = true and OpportunityId = :o.Id];
            if (contactRole != null)
            {
                System.debug('Found: ' + contactRole.ContactID);
            }
        }
    }
}