+ Start a Discussion
QD93QD93 

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). 
 
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;
}

 
Prashant Pandey07Prashant Pandey07
Hello,

You can try the following code and let me know if you have any issue.

Note- I have not compiled the code
Public class oppHelper{
  public static void insertoppmethod(list<Opportunity> opps){

 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: opps) {
        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: opps) {
        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;

}
}
***************Trigger*************
 
trigger oppInsertTrg on Opportunity  (after insert) { 
    if (trigger.isAfter && trigger.isInsert) {
        oppHelper.insertoppmethod(trigger.new);

    }

--
Thanks,
Prashant