You need to sign in to do that
Don't have an account?
QD93
Converting Apex Trigger to Apex Class
Hi ,
I would like to convert the below trigger to apex class , let me know how to do it. Currently, the trigger fires when an Opportunity is created on custom object A (company Summary), it create Opportunity Contact Roles relating the newly created opportunity to all the contacts related with the object A (company summary).
I would like to convert the below trigger to apex class , let me know how to do it. Currently, the trigger fires when an Opportunity is created on custom object A (company Summary), it create Opportunity Contact Roles relating the newly created opportunity to all the contacts related with the object A (company summary).
trigger createOpportunityContactRoles on Opportunity (after insert) { Set<Id> CompanySummaryIds = new Set<Id>(); List<OpportunityContactRole> ocrList = new List<OpportunityContactRole>(); Map<Id, List<Contact>> CompanySummaryContacts = new Map<Id, List<Contact>>(); for(Opportunity o: Trigger.New) { if(o.aduro__Company_Summary__c != Null) CompanySummaryIds.add(o.aduro__Company_Summary__c); } for(Contact con: [select id, aduro__Company__c from Contact where aduro__Company__c in: CompanySummaryIds]) { if(!CompanySummaryContacts.containsKey(con.aduro__Company__c)) CompanySummaryContacts.put(con.aduro__Company__c, new List<Contact>()); CompanySummaryContacts.get(con.Aduro__Company__c).add(con); } for(Opportunity opp: Trigger.New) { if(CompanySummaryContacts.containskey(opp.aduro__Company_Summary__c) && CompanySummaryContacts.get(opp.aduro__Company_Summary__c) != NULL) { Boolean isFirstContact = true; for(Contact c: CompanySummaryContacts.get(opp.aduro__Company_Summary__c)) { OpportunityContactRole ocr = new OpportunityContactRole(ContactId = c.Id, OpportunityId = opp.id, Role = 'Investor: Influencer'); if(isFirstContact) { ocr.IsPrimary = true; isFirstContact = false; } ocrList.add(ocr); } } } if(ocrList.size() > 0) insert ocrList; }
You can try the following code and let me know if you have any issue.
Note- I have not compiled the code ***************Trigger*************
--
Thanks,
Prashant