+ Start a Discussion
XIOXIO 

How to xecute apex trigger without having to edit/save?

Hello Developers,

The apex trigger below is basically a rollup count of the Contact Roles assigned to the Opportuniy. The trigger works great but only executes when I edit and save the Opportunity. I need the trigger to execute as soon as a Contact Role is created and assigned to update the Opportunity fields. Any assistance is greatly appreciated!
 
trigger RollUpContactRole on Opportunity (before update)
{

Map<Id,List<opportunityContactRole>> oppConRole = new Map<Id,List<opportunityContactRole>>();

Map<Id, Opportunity> oppty_con = new Map<Id, Opportunity>();

for (Integer i = 0; i < Trigger.new.size(); i++)
{
        oppty_con.put(Trigger.new[i].id,Trigger.new[i]);      
}

for (OpportunityContactRole oppcntctrle :[select OpportunityId,IsPrimary from OpportunityContactRole where OpportunityContactRole.OpportunityId in :oppty_con.keySet()])
{
    if(!oppConRole.containsKey(oppcntctrle.OpportunityId)){
        List<opportunityContactRole> temp = new List<opportunityContactRole>();
        temp.add(oppcntctrle);
        oppConRole.put(oppcntctrle.OpportunityId,temp);
    }else{
        List<opportunityContactRole> temp1 = new List<opportunityContactRole>();
        temp1 = oppConRole.get(oppcntctrle.OpportunityId);
        temp1.add(oppcntctrle);
        oppConRole.put(oppcntctrle.OpportunityId,temp1);
    }
}

for (Opportunity Oppty : system.trigger.new) 
{
    if(oppConRole.containsKey(Oppty.Id)){
        Oppty.Number_of_Contacts_Roles_Assigned__c = (oppConRole.get(Oppty.Id)).size();
        for(opportunityContactRole ocr : oppConRole.get(Oppty.Id)){
            if(ocr.IsPrimary){
                Oppty.Primary_Contact_Assigned__c =true;
                continue;
            }
        }
    }else{
        Oppty.Number_of_Contacts_Roles_Assigned__c = 0;
        Oppty.Primary_Contact_Assigned__c =false;
    }   
}

}

 
Best Answer chosen by XIO
Ashish KeshariAshish Keshari
Hi G Harmens,
The trigger you have written is running on Opportunity record with before update event. If you want to run on a Contract Role creation, should not you be writing the trigger on ContractRole itself with after insert event ? From the child  ContractRole, you could easily traverse to Parent opportunity field. Since I did not looked at the entire trigger - let me know if you have already though about it. thanks.

All Answers

Ashish KeshariAshish Keshari
Hi G Harmens,
The trigger you have written is running on Opportunity record with before update event. If you want to run on a Contract Role creation, should not you be writing the trigger on ContractRole itself with after insert event ? From the child  ContractRole, you could easily traverse to Parent opportunity field. Since I did not looked at the entire trigger - let me know if you have already though about it. thanks.
This was selected as the best answer
Amit Singh 1Amit Singh 1
Hello Hermans,

You can not write trigger on Opportunity Contact Role and your trigger is on Opportunity before Update Event. To execute ur trigger what you can do is Create a VF page and it's controller class using Opportunity as Standard COntroller and add that VF page to Opportunity Layout.

In Controller of VF page you can add the logic of checking if any contact Role has been added to opportunity OR not if Yes then update your Opportunity. This Update operation will be responsible for executing the trigger.

Hope this helps :)

Thanks,
Amit Singh