You need to sign in to do that
Don't have an account?
Isha Saxena 9
I am writing batch which states that is in a Account Allocation rule object "account Manager" field is changed then for every matching value of "look up parameter field" on the object which matches an account then the account owner should be updated.
global class Batch_Account_Manager_change_update implements Database.Batchable<sobject>
{
List<Account_Allocation_Rules__c> acc_rule = new List<Account_Allocation_Rules__c>();
string rid = [SELECT id from RecordType where Name ='Tyro Customer'].Id;
string dynamicQuery = null;
boolean flag = false;
Integer num = 0;
string sg = null;
Set<ID> setAccRuleId = new Set<ID>();
List<Account> upacc = new List<Account>();
List<Account> acct = new List<Account>();
Set<string> paramlist = new Set<string>();
Map<String,account> accmap = new map<String,account>();
global Database.QueryLocator start(Database.BatchableContext bc)
{
return Database.getQueryLocator('Select Id,Name,RecordType.Id,Active__c,Account_Manager__c,Active_User__c,Industry__c,Integration_Product__c,Lookup_Parameters__c,Rule_Allocation_Name__c,Segment__c from Account_Allocation_Rules__c where LastModifiedDate = Today');
}
global void execute(Database.BatchableContext bc,List<Account_Allocation_Rules__c> sobj)
{
System.debug('Account Allocation Rule'+sobj);
for(Account_Allocation_Rules__c alc : sobj)
{
if(alc.Lookup_Parameters__c != null)
{
paramlist.add(alc.Lookup_Parameters__c);
}
}
acct = [Select Id,OwnerId,Segment__c,Account_Allocation_Parameters__c,Corporate_Group__c from Account where RecordType.Id =: rid and Corporate_Group__c = null and Key_Account__c = false and Merchant_Group_Member__c = 0 and Account_Allocation_Parameters__c in : paramlist];
//acct = Database.query(dynamicQuery);
if(acct.size()>0)
{
for (Account at : acct)
{
accmap.put(at.id, at);
}
}
System.debug('#######Account Map'+accmap);
System.debug('testing time');
for(account act : accmap.values())
{
for (Account_Allocation_Rules__c alc : sobj)
{
System.debug('########DrillDown');
if (act.Account_Allocation_Parameters__c == alc.Lookup_Parameters__c)
{
System.debug('########DrillDown111111');
system.debug(LoggingLevel.INFO, alc.Lookup_Parameters__c + ' exist');
System.debug('Account Owner Id####'+act.OwnerId);
System.debug('Allocation Account Manager'+alc.Account_Manager__c);
act.OwnerId = alc.Account_Manager__c ;
upacc.add(act);
}
else
{
system.debug(LoggingLevel.INFO, alc.Lookup_Parameters__c + ' does not exist');
}
}
}
System.debug('Updated Account List*******'+upacc);
}
global void finish(Database.BatchableContext bc)
{
update upacc;
}
}
{
List<Account_Allocation_Rules__c> acc_rule = new List<Account_Allocation_Rules__c>();
string rid = [SELECT id from RecordType where Name ='Tyro Customer'].Id;
string dynamicQuery = null;
boolean flag = false;
Integer num = 0;
string sg = null;
Set<ID> setAccRuleId = new Set<ID>();
List<Account> upacc = new List<Account>();
List<Account> acct = new List<Account>();
Set<string> paramlist = new Set<string>();
Map<String,account> accmap = new map<String,account>();
global Database.QueryLocator start(Database.BatchableContext bc)
{
return Database.getQueryLocator('Select Id,Name,RecordType.Id,Active__c,Account_Manager__c,Active_User__c,Industry__c,Integration_Product__c,Lookup_Parameters__c,Rule_Allocation_Name__c,Segment__c from Account_Allocation_Rules__c where LastModifiedDate = Today');
}
global void execute(Database.BatchableContext bc,List<Account_Allocation_Rules__c> sobj)
{
System.debug('Account Allocation Rule'+sobj);
for(Account_Allocation_Rules__c alc : sobj)
{
if(alc.Lookup_Parameters__c != null)
{
paramlist.add(alc.Lookup_Parameters__c);
}
}
acct = [Select Id,OwnerId,Segment__c,Account_Allocation_Parameters__c,Corporate_Group__c from Account where RecordType.Id =: rid and Corporate_Group__c = null and Key_Account__c = false and Merchant_Group_Member__c = 0 and Account_Allocation_Parameters__c in : paramlist];
//acct = Database.query(dynamicQuery);
if(acct.size()>0)
{
for (Account at : acct)
{
accmap.put(at.id, at);
}
}
System.debug('#######Account Map'+accmap);
System.debug('testing time');
for(account act : accmap.values())
{
for (Account_Allocation_Rules__c alc : sobj)
{
System.debug('########DrillDown');
if (act.Account_Allocation_Parameters__c == alc.Lookup_Parameters__c)
{
System.debug('########DrillDown111111');
system.debug(LoggingLevel.INFO, alc.Lookup_Parameters__c + ' exist');
System.debug('Account Owner Id####'+act.OwnerId);
System.debug('Allocation Account Manager'+alc.Account_Manager__c);
act.OwnerId = alc.Account_Manager__c ;
upacc.add(act);
}
else
{
system.debug(LoggingLevel.INFO, alc.Lookup_Parameters__c + ' does not exist');
}
}
}
System.debug('Updated Account List*******'+upacc);
}
global void finish(Database.BatchableContext bc)
{
update upacc;
}
}
use TODAY instead of Today in getquerylocator. and let me know if this work also while executing this batch you should have records in your or where lastmodified date = TODAY
Let me know if this works
Thanks,
Sukanya Banekar
let me know if this works.