function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
s_macs_mac 

trigger on events/tasks to update opportunity

trigger on events/tasks after insert and after update to update a field on opportunity,with the value of field in events/tasks..
SF AdminSF Admin
Hi,

I Hope below link help you!

populate the Opportunity Stage in a custom field on the Task object:https://developer.salesforce.com/forums/ForumsMain?id=906F000000091fKIAQ

Regards
Bhanu MaheshBhanu Mahesh
Hi s_mac,

Try the below code

NOTE; I have taken Test__c field should be updated on opportunity with test__c field on task

trigger test on task (after insert,after update) {
    Set<Id> oppIds = new Set<Id>();
    Map<Id,Opportunity> mapOpptnty = new Map<Id,Opportunity>();
    for (Task t : Trigger.new) {
            if (t.WhatId != null && string.valueof(t.WhatId).startsWith('006') ) {
                oppIds.add(t.WhatId);
            }
        }
        if(!oppIds.isEmpty()){
            for(Opportunity opp:[SELECT Id,test__c FROM Opportunity WHERE Id IN : oppIds]){
                mapOpptnty.put(opp.Id,opp);
            }
        }
        
        for (Task t : Trigger.new) {
            if (t.WhatId != null && string.valueof(t.WhatId).startsWith('006') && mapOpptnty != null && mapOpptnty.get(t.WhatId) != null) {
                Opportunity opp = mapOpptnty.get(t.WhatId);
                opp.test__c = t.test__c;
                mapOpptnty.put(opp.Id,opp);
            }
        }
    if(mapOpptnty.values().size() > 0){
        update mapOpptnty.values();
    }
}

For Event, modify the above code with same logic to work with Event

Regards,
Bhanu Mahesh
PratikPratik (Salesforce Developers) 
Hi s_mac,

Here is the sample code:
 
Trigger changeopp on Event(after update, after insert) {
    string eid;
    list<opportunity> oplist = new list<opportunity> ();
    list<opportunity> opToUpdate = new list<opportunity> ();
    for (event e: trigger.new) {
    
    eid= e.whatid;
    if(eid.startsWith('006')) {
    oplist = [select name, stagename from opportunity where id=: eid];
    
        }
    
    }
    
    for ( opportunity opp: oplist) {
    opp.stagename = 'Prospecting';
    opToUpdate.add(opp);
    }
    
    update opToUpdate;

}

Thanks,
Pratik
 
Andreas BruchwitzAndreas Bruchwitz
Hi Pratik,

how would the code look like if we wanna restrict the trigger to 2 previous stages?
IF an event was created while the Opportunity was in Stage 1 or Stage 2 -> Update Stage to Stage 3 else do nothing.

Thanks in advance.

Regards,
Andreas
Andreas BruchwitzAndreas Bruchwitz
I think I figured it out. Thanks for the code!

Trigger changeopp on Event(after update, after insert) {
    string eid;
    
    list<opportunity> oplist = new list<opportunity> ();
    list<opportunity> opToUpdate = new list<opportunity> ();
    
    for (event e: trigger.new) {
    eid= e.whatid;
    
    if(eid.startsWith('006')) {
    oplist = [select name, stagename from opportunity where id=: eid and (stagename= 'Contacted' or stagename= 'Start - go for it!')];
     
        }
     
    }
     
    for ( opportunity opp: oplist) {
    opp.stagename = 'Appointment scheduled';
    opToUpdate.add(opp);
    }
     
    update opToUpdate;
 
}