You need to sign in to do that
Don't have an account?

Trigger Not Updating Opportunity Field
Hi,
My Trigger is to update the Oppty 'Next_Activity__c' Field with the Task Subject based on First Open Activity(ActivityDate). Now the below trigger is updating the field when the task is inserted but not when the task was updated or deleted.
Ex : If I created a task1 with subject ='xyz' and due date 20/1/2015
task2 with subject ='abc' and due date 21/1/2015
then the Next_Activity__c = 'xyz' but when I am deleting task1 it has to update the Next_Activity__c with 'abc' but it is not doing so. Can any one help me in finding the problem.
trigger UpdateOppNextActivtyTrigger on Task (after insert,after update) {
List<Id> idSet = new List<Id>();
for (Task att: Trigger.new) {
if ((att.WhatId + '').startsWith('006')) {
idSet.add(att.WhatId);
}
}
List<Opportunity> updateList = new List<Opportunity> ();
List<RecordType> rtList = [Select Id,Name from RecordType where SObjectType = 'Opportunity'and Name = 'van Wagenen' Limit 1];
for (opportunity o: [Select RecordTypeId,Next_Activity__c, (select id,RecordType.Name, subject from Tasks where RecordType.Name='van Wagenen' order by ActivityDate asc limit 1) from Opportunity where id in : idSet ]){
if (o.recordTypeId ==rtList[0].Id&&o.Next_Activity__c != o.tasks[0].subject&&o.tasks.size()>0) {
System.debug('Next Activity:'+o.Next_Activity__c);
System.debug('Task Subject Before Update:'+o.tasks[0].subject);
o.Next_Activity__c = o.tasks[0].subject;
System.debug('Next Activity:'+o.Next_Activity__c);
System.debug('Task Subject Before Update:'+o.tasks[0].subject);
updateList.add(o);
System.debug('Next Activity:'+o.Next_Activity__c);
System.debug('Task Subject Before Update:'+o.tasks[0].subject);
}
}
update updateList;
}
My Trigger is to update the Oppty 'Next_Activity__c' Field with the Task Subject based on First Open Activity(ActivityDate). Now the below trigger is updating the field when the task is inserted but not when the task was updated or deleted.
Ex : If I created a task1 with subject ='xyz' and due date 20/1/2015
task2 with subject ='abc' and due date 21/1/2015
then the Next_Activity__c = 'xyz' but when I am deleting task1 it has to update the Next_Activity__c with 'abc' but it is not doing so. Can any one help me in finding the problem.
trigger UpdateOppNextActivtyTrigger on Task (after insert,after update) {
List<Id> idSet = new List<Id>();
for (Task att: Trigger.new) {
if ((att.WhatId + '').startsWith('006')) {
idSet.add(att.WhatId);
}
}
List<Opportunity> updateList = new List<Opportunity> ();
List<RecordType> rtList = [Select Id,Name from RecordType where SObjectType = 'Opportunity'and Name = 'van Wagenen' Limit 1];
for (opportunity o: [Select RecordTypeId,Next_Activity__c, (select id,RecordType.Name, subject from Tasks where RecordType.Name='van Wagenen' order by ActivityDate asc limit 1) from Opportunity where id in : idSet ]){
if (o.recordTypeId ==rtList[0].Id&&o.Next_Activity__c != o.tasks[0].subject&&o.tasks.size()>0) {
System.debug('Next Activity:'+o.Next_Activity__c);
System.debug('Task Subject Before Update:'+o.tasks[0].subject);
o.Next_Activity__c = o.tasks[0].subject;
System.debug('Next Activity:'+o.Next_Activity__c);
System.debug('Task Subject Before Update:'+o.tasks[0].subject);
updateList.add(o);
System.debug('Next Activity:'+o.Next_Activity__c);
System.debug('Task Subject Before Update:'+o.tasks[0].subject);
}
}
update updateList;
}
Your trigger will be work on insert or update always not on delete to do this you need to make trigger ondelete also like following
trigger UpdateOppNextActivtyTrigger on Task (after insert,after update,after delete) {
.
.
.
.
}
Best Regards.
IQRAR AHMED
Senior .NET/Salesforce Developer
Still not happening.. even after keeping after delete also it is behaving in the same way
trigger UpdateOppEventNextActivityTrigger on Event (after insert,after update,after delete) {
Set<Id> oppIds = new Set<Id>();
List<Event> evts;
if (Trigger.isDelete) {
evts = Trigger.old;
} else {
evts = Trigger.new;
}
for (Event event: evts) {
oppIds.add(event.WhatId);
}
List<RecordType> rtList = [ select Name from RecordType where SObjectType = 'Opportunity' and Name = 'vG' Limit 1];
List<Opportunity> oppList = [select RecordTypeId,Next_Activity__c,Next_Activity_Date__c,(select Subject, ActivityDate from Events where isDeleted = false order by ActivityDate asc) from Opportunity where Id IN :oppIds and recordTypeId = :rtList.get(0).Id];
List<Opportunity> oppToUpdate = new List<Opportunity>();
if (!oppList.isEmpty()) {
for (Opportunity opp: oppList) {
if (!opp.Events.isEmpty() && opp.Events.get(0).Subject != null&&opp.Events.get(0).ActivityDate < opp.Next_Activity_Date__c) {
if (opp.Next_Activity__c != opp.Events.get(0).Subject) {
opp.Next_Activity__c = opp.Events.get(0).Subject;
oppToUpdate.add(opp);
}
}
}
}
if (!oppToUpdate.isEmpty()) {
update oppToUpdate;
}
}
----------------------------------------------------------------------------------------------------------------------------
trigger UpdateOppNextActivtyTrigger on Task (after insert,after update) {
List<Id> idSet = new List<Id>();
for (Task att: Trigger.new) {
if ((att.WhatId + '').startsWith('006')) {
idSet.add(att.WhatId);
}
}
List<RecordType> rtList = [Select Id,Name from RecordType where SObjectType = 'Opportunity'and Name = 'vG' Limit 1];
List<Opportunity> opptyList = [Select RecordTypeId,Next_Activity__c,Next_Activity_Date__c, (select id,RecordType.Name,Status, subject,ActivityDate from Tasks Where isClosed =false order by ActivityDate ASC)from Opportunity where id in : idSet and recordTypeId = :rtList.get(0).Id ];
List<Opportunity> updateList = new List<Opportunity> ();
if(!opptyList.isEmpty()){
for (opportunity opp: opptyList){
if (!opp.Tasks.isEmpty() &&opp.Tasks.get(0).Subject != null) {
if (opp.Next_Activity__c != opp.Tasks.get(0).Subject) {
opp.Next_Activity__c = opp.Tasks.get(0).Subject;
opp.Next_Activity_Date__c = opp.Tasks.get(0).ActivityDate;
updateList.add(opp);
}
}
}
}
if (!updateList.isEmpty()){
update updateList;
}
}