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
Guru sfdcGuru sfdc 

BatchApex help

Hi All,
 
Can anyone please help me to create a batch apex using below trigger.
 
Thanks,
 
Guru sfdcGuru sfdc
Trigger:
Trigger AccountPartnerType on Account (after update){
    if(AccountPartnerTypecls.Aptcalc){
  AccountPartnerTypecls acpt = new AccountPartnerTypecls();
                acpt.AccountPartnerTypecalc(Trigger.new,trigger.newmap,trigger.oldmap);
                 }
}
Class:
public class AccountPartnerTypecls{
    public static Boolean Aptcalc = true;
    public static Boolean Aptcalc1 = false; 
    public static Boolean Aptcalc2 = false;  
    public void AccountPartnerTypecalc(List<Account> aclist,map<id,Account> newmap,map<id,Account> oldmap)
    {
        set<id> accidset = new set<id>(); 
        set<string> ulprntidset = new set<string>();   
        string dulprnt;
        map<string,List<Account>>  accmap = new  map<string,List<Account>>();
        system.debug('*********'+aclist.size());           
        for(Account acc: aclist)
        {
            accidset.add(acc.id);
            // if(acc.GE_Global_Ultimate_Reporting_Account__c!=Null)  //GE_Digital_Ultimate_Parent_ID__c
            // Ultimate_Parent_ID__c
            ulprntidset.add(acc.GE_Digital_Ultimate_Parent_ID__c);
            dulprnt= acc.GE_Digital_Ultimate_Parent_ID__c;
            system.debug('*********'+dulprnt);
        }
       
        system.debug('*********'+dulprnt); 
       
        List<Account> hiracclist = new List<Account>();
        List<Account> aclist1 = [select id, name,Partner_Status__c,Is_Partner__c,Account_Type__c,Tier_2__c,Parentid,GE_Digital_Ultimate_Parent_ID__c from Account where  GE_Digital_Ultimate_Parent_ID__c  =:dulprnt  AND Parentid!=null AND id NOT IN:accidset ];
       
        system.debug('*********'+aclist1.size()); 
       
        for(Account a : aclist1)
        {
            if(accmap.containskey(a.GE_Digital_Ultimate_Parent_ID__c))
            accmap.get(a.GE_Digital_Ultimate_Parent_ID__c).add(a);
            else
            {
                List<Account> alist = new List<Account>();
                alist.add(a);
                accmap.put(a.GE_Digital_Ultimate_Parent_ID__c,alist);
            }
        }
        for(Account acc: [select id, name,Partner_Status__c,Is_Partner__c,Account_Type__c,Parentid,Tier_2__c,GE_Digital_Ultimate_Parent_ID__c from Account where ID IN :accidset ])
        {
            string acctype='';
            if(accmap.get(acc.GE_Digital_Ultimate_Parent_ID__c)!=null)
            {
                /* if(accmap.get(acc.GE_Digital_Ultimate_Parent_ID__c).contains(acc)){
                accmap.get(acc.GE_Digital_Ultimate_Parent_ID__c).set(0, acc);
                accmap.get(acc.GE_Digital_Ultimate_Parent_ID__c).remove(0);
                }*/
                for(Account acchr : accmap.get(acc.GE_Digital_Ultimate_Parent_ID__c))
                {
                    //acchr.Is_Partner__c= acc.Is_Partner__c;
                    if(acc.Is_Partner__c)
                    acchr.Account_Type__c = 'Partner';
                    else
                    acchr.Account_Type__c= '';
                    hiracclist.add(acchr);
                }
                if(acc.Is_Partner__c)
                {
                    if(acc.parentid!=null)
                    Aptcalc1 =true;
                    acc.Account_Type__c= 'Partner';
                    hiracclist.add(acc);
                }
                else{
                    if(acc.parentid!=null)
                    Aptcalc2 =true;
                    acc.Account_Type__c= '';
                    hiracclist.add(acc);
                }
            }
            else{
                if(acc.Is_Partner__c)
                {
                    acc.Account_Type__c= 'Partner';
                    hiracclist.add(acc);
                }
                else{
                    acc.Account_Type__c= ' ';
                    hiracclist.add(acc);
                }
            }
        }      
        Account a=  [select id, name,Partner_Status__c,Is_Partner__c,Account_Type__c,Parentid,Tier_2__c,GE_Digital_Ultimate_Parent_ID__c from Account where ID  =:dulprnt];
        if(Aptcalc1)
        {
            a.Account_Type__c= 'Partner';
            hiracclist.add(a);
        }
        else if(Aptcalc2)
        {
            a.Account_Type__c= '';
            hiracclist.add(a);
        }
        Aptcalc = false;         
        update  hiracclist;
    }
}

Thanks
Amit Singh 1Amit Singh 1
Use below code for Batch class and use the Old Apex Class.
global class AccountPartnerTypeBatch implements Database.Batchable<Account>{
   global Database.QueryLocator start(Database.BatchableContext BC){
      // Modify query as per your requirement
      return Database.getQueryLocator('Select Id,Name From Account LIMIT 10');
   }

   global void execute(Database.BatchableContext BC, List<sObject> scope){
      if(AccountPartnerTypecls.Aptcalc){
	    AccountPartnerTypecls acpt = new AccountPartnerTypecls();
        acpt.AccountPartnerTypecalc(Trigger.new,trigger.newmap,trigger.oldmap);
      }
   }

   global void finish(Database.BatchableContext BC){
	  // You can write your other logic here
   }
}
Use SOQL query as per your requirement.
Hope this helps :)
Thanks!
Amit Singh
 
Guru sfdcGuru sfdc
Hi Amit,

Thanks for quick reply. Can you please help me to create complete class and trigger logic in one batch apex class I don't want to use two classes Please help me

​Thanks
 
Amit Singh 1Amit Singh 1
Use Below code.
global class AccountPartnerTypeBatch implements Database.Batchable<Account>{
   global Database.QueryLocator start(Database.BatchableContext BC){
      // Modify query as per your requirement
      return Database.getQueryLocator('Select Id,Name From Account LIMIT 10');
   }

   global void execute(Database.BatchableContext BC, List<sObject> scope){
          public static Boolean Aptcalc1 = false; 
          public static Boolean Aptcalc2 = false; 
		  set<id> accidset = new set<id>(); 
        set<string> ulprntidset = new set<string>();   
        string dulprnt;
        map<string,List<Account>>  accmap = new  map<string,List<Account>>();
        system.debug('*********'+aclist.size());           
        for(Account acc: aclist)
        {
            accidset.add(acc.id);
            // if(acc.GE_Global_Ultimate_Reporting_Account__c!=Null)  //GE_Digital_Ultimate_Parent_ID__c
            // Ultimate_Parent_ID__c
            ulprntidset.add(acc.GE_Digital_Ultimate_Parent_ID__c);
            dulprnt= acc.GE_Digital_Ultimate_Parent_ID__c;
            system.debug('*********'+dulprnt);
        }
       
        system.debug('*********'+dulprnt); 
       
        List<Account> hiracclist = new List<Account>();
        List<Account> aclist1 = [select id, name,Partner_Status__c,Is_Partner__c,Account_Type__c,Tier_2__c,Parentid,GE_Digital_Ultimate_Parent_ID__c from Account where  GE_Digital_Ultimate_Parent_ID__c  =:dulprnt  AND Parentid!=null AND id NOT IN:accidset ];
       
        system.debug('*********'+aclist1.size()); 
       
        for(Account a : aclist1)
        {
            if(accmap.containskey(a.GE_Digital_Ultimate_Parent_ID__c))
            accmap.get(a.GE_Digital_Ultimate_Parent_ID__c).add(a);
            else
            {
                List<Account> alist = new List<Account>();
                alist.add(a);
                accmap.put(a.GE_Digital_Ultimate_Parent_ID__c,alist);
            }
        }
        for(Account acc: [select id, name,Partner_Status__c,Is_Partner__c,Account_Type__c,Parentid,Tier_2__c,GE_Digital_Ultimate_Parent_ID__c from Account where ID IN :accidset ])
        {
            string acctype='';
            if(accmap.get(acc.GE_Digital_Ultimate_Parent_ID__c)!=null)
            {
                /* if(accmap.get(acc.GE_Digital_Ultimate_Parent_ID__c).contains(acc)){
                accmap.get(acc.GE_Digital_Ultimate_Parent_ID__c).set(0, acc);
                accmap.get(acc.GE_Digital_Ultimate_Parent_ID__c).remove(0);
                }*/
                for(Account acchr : accmap.get(acc.GE_Digital_Ultimate_Parent_ID__c))
                {
                    //acchr.Is_Partner__c= acc.Is_Partner__c;
                    if(acc.Is_Partner__c)
                    acchr.Account_Type__c = 'Partner';
                    else
                    acchr.Account_Type__c= '';
                    hiracclist.add(acchr);
                }
                if(acc.Is_Partner__c)
                {
                    if(acc.parentid!=null)
                    Aptcalc1 =true;
                    acc.Account_Type__c= 'Partner';
                    hiracclist.add(acc);
                }
                else{
                    if(acc.parentid!=null)
                    Aptcalc2 =true;
                    acc.Account_Type__c= '';
                    hiracclist.add(acc);
                }
            }
            else{
                if(acc.Is_Partner__c)
                {
                    acc.Account_Type__c= 'Partner';
                    hiracclist.add(acc);
                }
                else{
                    acc.Account_Type__c= ' ';
                    hiracclist.add(acc);
                }
            }
        }      
        Account a=  [select id, name,Partner_Status__c,Is_Partner__c,Account_Type__c,Parentid,Tier_2__c,GE_Digital_Ultimate_Parent_ID__c from Account where ID  =:dulprnt];
        if(Aptcalc1)
        {
            a.Account_Type__c= 'Partner';
            hiracclist.add(a);
        }
        else if(Aptcalc2)
        {
            a.Account_Type__c= '';
            hiracclist.add(a);
        }        
        update  hiracclist;
   }

   global void finish(Database.BatchableContext BC){
	  // You can write your other logic here
   }
}
Hope this helps :)
Thanks!
AMIT SINGH