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
Paras JainParas 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);    
             }    
         }   
     }

}
SaranSaran
Hi,

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,
Paras JainParas Jain
hi,
yes 
numberDaysDue2 variable get changed everytime.
 
SaranSaran
Hi,

Can u explain the logic of your trigger so that it would be easy to identify. 
Paras JainParas Jain
On opportunity update, if this is open Opportunity then its close date will equals Account's "Next Potential Date(Custom date field)"
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)
SaranSaran
Hi,

I have just modified the insert update block can u please check if the below code solves your issue.
 
map<id,Opportunity> accOppMap = new map<id,opportunity>();
List<Account> accUpdateList =new List<Account>();
set<Id> Accountset = new set<Account>();

if(Trigger.isInsert || Trigger.isUpdate){
    for(Opportunity opp: Trigger.new){
        if(opp.Accountid!=null){
            Accountset.add(opp.Accountid);
        }                
    }

	for(Opportunity oppty : [select id, closeDate, AccountId from opportunity where isClosed = false order by CloseDate Desc])
	{
		if(!accOppMap.containsKey(oppty.AccountId))
		{
			accOppMap.put(oppty.AccountId , oppty);
		}
	}

	for(id accId : Accountset)
	{
		Account acc = new Account();
		acc.id - accId;
		if(accOppMap.containsKey(accId))
			acc.Next_Potential_Purchase_Date__c = accOppMap.get(accId).CloseDate;
		else
			acc.Next_Potential_Purchase_Date__c = null;
		accUpdateList.add(acc);
	}
	update accUpdateList;
}