You need to sign in to do that
Don't have an account?
How to create Opportunity Contact Roles for all Opportunities without contact roles, with Accounts that have Contact(s)?
I have several Opportunities with no Contact Roles assigned. For the Opportunities with no Contact Roles, that are related to Accounts with Contacts, I need to create Contact Roles to match the related Account's Contacts. I am trying to accomplish this with an anonymous apex code block.
Here's what I have started, can anyone help me to make this work? Thank you.
Map<Id, Id> oppIdToAccIdMap = new Map<Id, Id>(); Map<Id, Id> conIdToAccIdMap = new Map<Id, Id>(); Set<Id> oppAccountIdSet = new Set<Id>(); List<OpportunityContactRole> ocrsToInsert = new List<OpportunityContactRole>(); // 1. Get all Opportunities that have no contact roles List<Opportunity> myOpps = [ SELECT Id, AccountId FROM Opportunity WHERE Id NOT IN (SELECT OpportunityId FROM OpportunityContactRole) ORDER BY Name ASC ]; for (Opportunity opp1 : myOpps) { oppAccountIdSet.add(opp1.AccountId); } // 2. Get all Contacts that aren't contact Roles, that share the same Account as Opportunities with no Contact Roles List<Contact> myCons = [ SELECT Id, Name, AccountId FROM Contact WHERE AccountId IN :oppAccountIdSet AND Id NOT IN (SELECT ContactId FROM OpportunityContactRole) ORDER BY Name ASC ]; System.debug('***** DEBUG: Contacts with same Account: ' + myCons); if (!myOpps.isEmpty() && !myCons.isEmpty()) { // 3. Lopp through Opportunities with no contact roles, store opp Id and account Id in a map for (Opportunity myOpp : myOpps) { if (myOpp.AccountId != null && myOpp.Id != null) { oppIdToAccIdMap.put(myOpp.Id, myOpp.AccountId); } } System.debug('***** DEBUG: Opportunity Id to Account Id: ' + oppIdToAccIdMap); // 4. Lopp through Contacts with no contact roles, store contact Id and Account Id in a map for (Contact myCon : myCons) { if (myCon.AccountId != null && myCon.Id != null) { conIdToAccIdMap.put(myCon.Id, myCon.AccountId); } } System.debug('***** DEBUG: Contact Id to Account Id: ' + conIdToAccIdMap); // Need to Create a new Opportunity Contact Role for each Opportunity, using Opportunity Id and Contact Id that share the same Account // If Account has more than one Contact, both contacts should be contact roles of the Account's Opportunity // If Account has more than one Opportunity, both Opportunities should have the Account's contact(s) as their contact roles }
I have done some modification in the code and you can find the updated code below:
Please take care of the syntax errors and let me know if you face any issues.
Thanks,
Abhishek Bansal.