You need to sign in to do that
Don't have an account?
Matt Metros
Not sure what is going on here. I literally am filtering from the beginning to make sure that there won't be any null values.
Test giving me System.QueryException: List has no rows for assignment to SObject Error
trigger UpdateOpportunityContacts on Opportunity (after insert, before update) { for(Opportunity opp: Trigger.new){ List<Id> contactIds = new List<Id>(); for(OpportunityContactRole oppContactId : [SELECT ContactId FROM OpportunityContactRole WHERE OpportunityId =:opp.Id]){ contactIds.add(oppContactId.id); } if(contactIds.size()>0){ List<Contact> contacts = new List<Contact>(); for(Id contactId: contactIds ){ Contact contact = [SELECT Outreach_Status__c FROM Contact WHERE id =:contactId]; if(contact != null){ contacts.add(contact); } } List<Contact> updatedContacts = new List<Contact>(); for(Contact contact: Contacts){ if(opp.StageName == 'Closed Won'){ //move the contacts outreach status to either closed won or lost contact.Outreach_Status__c = 'Client'; } else if (opp.StageName == 'Closed Lost'){ contact.Outreach_Status__c = 'Closed Lost'; } else { contact.Outreach_Status__c = 'Opportunity'; } updatedContacts.add(contact); } insert updatedContacts; } } }
@isTest private class UpdateOpportunityContactsTest { @isTest static void test_method_one() { // Create New Opp Opportunity opp = new Opportunity(); opp.name = 'Test'; opp.accountId = '0010v00000KYsWoAAL'; opp.stageName = 'Demo Completed - DM'; opp.CloseDate = date.newinstance(2019, 3, 10); opp.Role_Persona__c = '-'; opp.ERP_Sofware__c = 'Microsoft Dynamics'; opp.Existing_Systems__c = '-'; opp.DM_Involvement_in_Purchasing__c = '-'; opp.NextStep = '-'; insert opp; // Create New Account Account a = new Account(); a.name = 'Test'; a.industry = 'Accounting'; a.of_Locations__c = 10; // create new contact Contact con = new Contact(); con.lastName = 'Test'; con.accountId = a.id; con.title = 'Test'; con.email = 'test@test.com'; insert con; //Create oppcontactrole OpportunityContactRole oppContactRole = new OpportunityContactRole(); oppcontactRole.OpportunityId = opp.id; oppContactRole.ContactId = con.id; oppContactRole.isPrimary = true; insert oppcontactRole; opp.stageName = 'Piloting'; update opp; system.debug(con.Outreach_Status__c); } }
Not sure what is going on here. I literally am filtering from the beginning to make sure that there won't be any null values.
Raj Vakati
QQ .. I guess you need before update trigger ??? How come an OpportunityContactRole is associated to opportunity on insert ???
Matt Metros
When you create an opportunity from a contact, that contact is inserted into the OpportunityContactRole