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
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.
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;
}
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
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
Here is the sample code:
Thanks,
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