You need to sign in to do that
Don't have an account?
Paras Jain
Not workinging Correctly on bulk update by Data Loader
trigger updateAccountNextPotPurDate on Opportunity(after insert, after update) {
Set < id > Accountset = new Set < id > ();
List<Account>actListupdate=new List<Account>();
List<Account>actsList=new List<Account>();
public Integer numberDaysDue1;
public Integer numberDaysDue2 = 90000;
if(Trigger.isInsert || Trigger.isUpdate){
for(Opportunity opp: Trigger.new){
if(opp.Accountid!=null){
Accountset.add(opp.Accountid);
}
}
actsList=[select id, Next_Potential_Purchase_Date__c,
(select id, AccountId, StageName, CloseDate, Isclosed
from Opportunities
where Isclosed=FALSE Order By CloseDate Desc)
from Account
where id in : Accountset];
for(Account actlst : actsList){
if(actlst.Opportunities.size()>0){
for (Opportunity opp: actlst .Opportunities) {
numberDaysDue1 = math.abs(Date.Today().daysBetween(opp.CloseDate));
if (numberDaysDue2 > numberDaysDue1) {
numberDaysDue2 = math.abs(numberDaysDue1);
actlst.Next_Potential_Purchase_Date__c = opp.Closedate;
}
}
}
else{
actlst.Next_Potential_Purchase_Date__c =null ;
}
actListupdate.add(actlst );
}
if(actListupdate.size()>0){ update actListupdate;}
}
if(Trigger.isDelete){
for(Opportunity opptniy : Trigger.old){
if(opptniy.Accountid!=null){
Accountset.add(opptniy.Accountid);
}
}
}
}
Set < id > Accountset = new Set < id > ();
List<Account>actListupdate=new List<Account>();
List<Account>actsList=new List<Account>();
public Integer numberDaysDue1;
public Integer numberDaysDue2 = 90000;
if(Trigger.isInsert || Trigger.isUpdate){
for(Opportunity opp: Trigger.new){
if(opp.Accountid!=null){
Accountset.add(opp.Accountid);
}
}
actsList=[select id, Next_Potential_Purchase_Date__c,
(select id, AccountId, StageName, CloseDate, Isclosed
from Opportunities
where Isclosed=FALSE Order By CloseDate Desc)
from Account
where id in : Accountset];
for(Account actlst : actsList){
if(actlst.Opportunities.size()>0){
for (Opportunity opp: actlst .Opportunities) {
numberDaysDue1 = math.abs(Date.Today().daysBetween(opp.CloseDate));
if (numberDaysDue2 > numberDaysDue1) {
numberDaysDue2 = math.abs(numberDaysDue1);
actlst.Next_Potential_Purchase_Date__c = opp.Closedate;
}
}
}
else{
actlst.Next_Potential_Purchase_Date__c =null ;
}
actListupdate.add(actlst );
}
if(actListupdate.size()>0){ update actListupdate;}
}
if(Trigger.isDelete){
for(Opportunity opptniy : Trigger.old){
if(opptniy.Accountid!=null){
Accountset.add(opptniy.Accountid);
}
}
}
}
I think you numberDaysDue2 variable get changed everytime.
Is this your functionlity? If not please decare the numberDaysDue2 variable inside the for loop. Hope by declaring the numberDaysDue2 inside the for loop ill solve the issue.
Thanks,
yes
numberDaysDue2 variable get changed everytime.
Can u explain the logic of your trigger so that it would be easy to identify.
suppose if account has 4 opportunity, then close date of any open opportunity nearest to date.today() will be the Account's "Next Potential Date(Custom date field)
I have just modified the insert update block can u please check if the below code solves your issue.