Our org was configured such that Opps can only be created during Lead Conversion or from a Contact related list. This ensures each Opp has a Contact Role associated.
SDRs create an Event when they have qualified a prospect (sometimes on a Lead, sometimes on a Contact) and set a meeting. If it's on a Lead, once the Lead is converted with an Opp created, the Event automatically associates with the Opp.
If the Event was created on a Contact which was qualified and an Opp is later created off the Contact, the Event doesn't associate with the Opp.
Here's my trigger for associating Events to an Opp from Contacts associated as Contact Roles on the Opp.
trigger AddMQLEventstToOpp on Opportunity (after update) {
for (Opportunity myOpp : Trigger.new) {
OpportunityContactRole contactRole = [select ContactId from OpportunityContactRole where OpportunityId = :myOpp.Id];
String cRoleId = contactRole.ContactId;
List<Event> mqlEventsToAdd = new List <Event>();
mqlEventsToAdd.add([select Id from Event where WhoId = :cRoleId and WhatId = null and Type = 'MQL']);
for (Event mqlEvent : mqlEventsToAdd){
mqlEvent.WhatId = myOpp.Id;
update mqlEvent;
}
}
}
I'm getting this error and not sure how to troubleshoot.
System.QueryException: List has no rows for assignment to SObject: External entry point