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
pavan kumar 1864pavan kumar 1864 

Not able to update the batch class.

Everything is  fine but at the last im not getting to updtae,i'm missing out in Brackets ,can anyone please guide me.

global class corporatearrbatch implements Database.Batchable<sObject>{
    
    public string a;
    
    public  corporatearrbatch()
    {
        a= 'select id,name,Corporate_Arr__c,createddate from account where type =\'Customer - Direct\' and id = \'0015g00000Gba22AAB\'';
    }
    
    public Database.querylocator start(Database.BatchableContext bc)
    {
        return Database.getquerylocator(a);
    }
    public void execute(Database.BatchableContext bc,list<account>accountrecords)
    {
        system.debug(accountrecords);
        
        
        
        list<account> accountupdated = new list<account>();
        map<id,account> mapname =new map<id,account>();
        
        map<id,list<opportunity>> opportunitylistwithid = new map<id,list<opportunity>>();
        
        map<id,Datetime> opportunityidwithmaxdate = new map<id,Datetime>();
        
        list<opportunity>opportunityrecords = [select id,name,accountid,StageName,CloseDate,Incremental_ARR__c,Corporate_Incremental_ARR__c,Corporate_effective_date__c,Contract_end_date__c from opportunity where (stagename = 'Closed Won' or stagename ='Closed: won - S' or stagename ='Closed: won - Renewal') and accountid in : accountrecords];
        
        system.debug(opportunityrecords.size());
        system.debug(opportunityrecords);
        
        Datetime maxdatefromopportunity;
        
        for(opportunity opportunityrecord : opportunityrecords)
        {
            
            maxdatefromopportunity = Null ;
            
            list<opportunity>opportunitylist = opportunitylistwithid.get(opportunityrecord.accountid);
            
            if(opportunitylist == Null)
            {
                opportunitylist = new list<opportunity>();
            }
            
            opportunitylist.add(opportunityrecord);
            
            opportunitylistwithid.put(opportunityrecord.accountid,opportunitylist);
            system.debug(opportunitylistwithid.size());
            
            if(opportunityrecord.Corporate_effective_date__c == Null)
            {
                maxdatefromopportunity = opportunityrecord.CloseDate;
                system.debug(maxdatefromopportunity);
            }
            
            else if(opportunityrecord.CloseDate >= opportunityrecord.Corporate_effective_date__c)
            {
                maxdatefromopportunity = opportunityrecord.CloseDate;
                system.debug(maxdatefromopportunity);
            }
            
            else
            {
                maxdatefromopportunity = opportunityrecord.Corporate_effective_date__c;
                system.debug(maxdatefromopportunity);
            }
            
            opportunityidwithmaxdate.put(opportunityrecord.id,maxdatefromopportunity);
            system.debug(opportunityidwithmaxdate);
        }
        
        Decimal maxamount = 0.00;
        
        account acc = new account ();
        
        for(id accountids : opportunitylistwithid.keyset())
        {
            system.debug('accountids'+ accountids);
            
            list<opportunity> opportunitylist = opportunitylistwithid.get(accountids);
            
            system.debug('opportunitylistwith correspondentaccountid'+ opportunitylist);
            
            for(opportunity opportunitysinglerecord : opportunitylist)
            {
                system.debug('opportunitylist----------'+ opportunitysinglerecord);
                
                
                if(opportunitylist.size()>1 && opportunitysinglerecord.Incremental_ARR__c != null)
                {
                    system.debug(1);
                    system.debug(opportunitylist.size());
                    
                    if(date.today() >= opportunityidwithmaxdate.get(opportunitysinglerecord.id))
                    {
                        system.debug(2);
                        if(opportunitysinglerecord.Corporate_Incremental_ARR__c == Null)
                        {
                            system.debug(3);
                            
                            maxamount = maxamount + opportunitysinglerecord.Incremental_ARR__c;
                            acc.id = opportunitysinglerecord.accountid;
                            
                            system.debug('maxamount--------------'+ maxamount);
                            system.debug('accid-------------'+ opportunitysinglerecord);
                            
                            
                        }
                        else
                        {
                            system.debug(4);
                            maxamount= maxamount + opportunitysinglerecord.Corporate_Incremental_ARR__c;
                            acc.id = opportunitysinglerecord.accountid;
                            system.debug('maxamount--------------'+ maxamount);
                            system.debug('accid-------------'+ opportunitysinglerecord);
                        }
                    } 
                }
                
                else {
                    if(opportunitylist.size() == 1 && opportunitylist.size() != null )
                    {
                        system.debug(5);
                        if(opportunitysinglerecord.Incremental_ARR__c != null)
                        {
                            system.debug(6);
                            maxamount = opportunitysinglerecord.Incremental_ARR__c;
                            system.debug('maxamount--------------'+ maxamount);
                            
                        }
                        else
                        {
                            system.debug(7);
                            maxamount = 0;
                            system.debug('maxamount--------------'+ maxamount);
                        }
                    }}
                acc.id = opportunitysinglerecord.AccountId;
                system.debug(acc.id = opportunitysinglerecord.AccountId);
                
                if(maxamount != null)
                {
                    acc.Corporate_Arr__c = maxamount;
                    system.debug( acc.Corporate_Arr__c);
                    accountupdated.add(acc);
                     system.debug(acc);
                }
            }
            
            
        }
        
        
        if(accountupdated != null)
        {    mapname.putall(accountupdated);
         if(mapname!=null)
         {
             update mapname.values();
         }
         
        }
        
        system.debug(acc);
        system.debug(mapname);
    }
    public void finish(Database.BatchableContext bc)
    {
        
    }
}
 
PriyaPriya (Salesforce Developers) 

Hi Pavan,

What is the error you are getting ?

Regards,

Priya Ranjan

pavan kumar 1864pavan kumar 1864
Thanks for the reply,
The issue got solved by my senior but i was not able to get the changes that he did .like on the line 78 he had created a ACCOUNT INSTANCE.
but initially i had created ACCOUNT INSTANCE on line 75 and we were pushing the variable's value to the new     list<account> accountupdated = new list<account>();     which is in 19th line.

The problem here was the latest value was getting REPLACED when it was Iterating Over the next ELement,.
if i place the ACCOUNT INSTANCE outsidethe For LOOP. but if i place the ACCOUNT INSTANCE inside the FOR LOOP that is on the 78th line  . The new values, as well as old values, were getting retained  in the list<account> accountupdated = new list<account>();

Can you please explain why it was happening?




 global class corporatearrbatch implements Database.Batchable<sObject>{
    
    public string a;
    
    public  corporatearrbatch()
    {
        a= 'select id,name,Corporate_Arr__c,createddate from account where type =\'Customer - Direct\' and id in (select accountid from opportunity where iswon= true) order by createddate desc limit 3';
    }
    
    public Database.querylocator start(Database.BatchableContext bc)
    {
        return Database.getquerylocator(a);
    }
    public void execute(Database.BatchableContext bc,list<account>accountrecords)
    {
        system.debug(accountrecords);
        
        
        list<account> accountupdated = new list<account>();
        
        map<id,account> mapname =new map<id,account>();
        
        map<id,list<opportunity>> opportunitylistwithid = new map<id,list<opportunity>>();
        
        map<id,Datetime> opportunityidwithmaxdate = new map<id,Datetime>();
        
        list<opportunity>opportunityrecords = [select id,name,accountid,StageName,CloseDate,Incremental_ARR__c,Corporate_Incremental_ARR__c,Corporate_effective_date__c,Contract_end_date__c from opportunity where (stagename = 'Closed Won' or stagename ='Closed: won - S' or stagename ='Closed: won - Renewal') and accountid in : accountrecords];
        
        system.debug(opportunityrecords.size());
        system.debug(opportunityrecords);
        
        
        
        Datetime  maxdatefromopportunity = Null ;
        
        for(opportunity opportunityrecord : opportunityrecords)
        {
            list<opportunity>opportunitylist = opportunitylistwithid.get(opportunityrecord.accountid);
            
            if(opportunitylist == Null)
            {
                opportunitylist = new list<opportunity>();
            }
            
            opportunitylist.add(opportunityrecord);
            
            opportunitylistwithid.put(opportunityrecord.accountid,opportunitylist);
            system.debug(opportunitylistwithid.size());
            
            if(opportunityrecord.Corporate_effective_date__c == Null)
            {
                maxdatefromopportunity = opportunityrecord.CloseDate;
                system.debug(maxdatefromopportunity);
            }
            
            else if(opportunityrecord.CloseDate >= opportunityrecord.Corporate_effective_date__c)
            {
                maxdatefromopportunity = opportunityrecord.CloseDate;
                system.debug(maxdatefromopportunity);
            }
            
            else
            {
                maxdatefromopportunity = opportunityrecord.Corporate_effective_date__c;
                system.debug(maxdatefromopportunity);
            }
            
            opportunityidwithmaxdate.put(opportunityrecord.id,maxdatefromopportunity);
            system.debug(opportunityidwithmaxdate);
        }
        
        Decimal maxamount = 0.00;
        
        
        
        for(id accountids : opportunitylistwithid.keyset())
        {
            account acc = new account();
            
            
            system.debug('accountids'+ accountids);
            
            list<opportunity> opportunitylist = opportunitylistwithid.get(accountids);
            
            system.debug('opportunitylistwith correspondentaccountid'+ opportunitylist);
            
            for(opportunity opportunitysinglerecord : opportunitylist)
            {
                system.debug('opportunitylist----------'+ opportunitysinglerecord);
                
                
                if(opportunitylist.size()>1 && opportunitysinglerecord.Incremental_ARR__c != null)
                {
                    system.debug(1);
                    system.debug(opportunitylist.size());
                    
                    if(date.today() >= opportunityidwithmaxdate.get(opportunitysinglerecord.id))
                    {
                        system.debug(2);
                        if(opportunitysinglerecord.Corporate_Incremental_ARR__c == Null)
                        {
                            system.debug(3);
                            
                            maxamount = maxamount + opportunitysinglerecord.Incremental_ARR__c;
                            acc.id = opportunitysinglerecord.AccountId;
                            
                            system.debug('maxamount--------------'+ maxamount);
                            
                            
                            
                        }
                        else
                        {
                            system.debug(4);
                            maxamount= maxamount + opportunitysinglerecord.Corporate_Incremental_ARR__c;
                            acc.id = opportunitysinglerecord.AccountId;
                            system.debug('maxamount--------------'+ maxamount);
                            
                        }
                    } 
                }
                
                else {
                    if(opportunitylist.size() == 1 && opportunitylist.size() != null )
                    {
                        system.debug(5);
                        if(opportunitysinglerecord.Incremental_ARR__c != null)
                        {
                            system.debug(6);
                            maxamount = opportunitysinglerecord.Incremental_ARR__c;
                            system.debug('maxamount--------------'+ maxamount);
                            
                            
                        }
                        else
                        {
                            system.debug(7);
                            maxamount = 0;
                            system.debug('maxamount--------------'+ maxamount);
                            
                        }
                        acc.id = opportunitysinglerecord.AccountId;
                        system.debug(acc.id = opportunitysinglerecord.AccountId);
                        
                    }
                }
                
                
                
            }
            if(maxamount != null)
            {
                acc.Corporate_Arr__c = maxamount;
                system.debug( acc.Corporate_Arr__c);
                system.debug(acc);
                
            } 
            accountupdated.add(acc);  
            system.debug(accountupdated);
            system.debug(acc);
            
        }
        
        if(accountupdated != null)
        {    
            update accountupdated;
            
            system.debug(accountupdated); 
            
        }
        
    }
    public void finish(Database.BatchableContext bc)
    {
        
    }
}