You need to sign in to do that
Don't have an account?

Insert failed. First exception on row 0; first error: STRING_TOO_LONG, Error Description: data value too large: Account_AU: execution of AfterUpdate caused by: System.AsyncException: Database.executeBatch cannot be called from a batch start, batch execut
i am getting error whie calling Batch_CustomerCategoryUpdate: my account has 1707 in account hierarchy
public static void updateCustomerCategories(List<Account> accountList, Map<Id, Account> oldAccts) {
map<string,Account> parentMap = new map<string,Account>();
map<string,list<Account>> childMap = new map<string,list<Account>>();
List<Account> updateAccount = new List<Account>();
Account accRef=new Account();
// if(!shouldRun('UpdateCustomerCategories')) return;
Set<Id> accIds = new Set<Id>();
Map<String,List<Account>> accInserIdMap = new Map<String,List<Account>>();
Map<String,List<Account>> accInserIdMap1 = new Map<String,List<Account>>();
for(Account acc:accountList){
Account oldAcct = null;
if(oldAccts != null && oldAccts.get(acc.Id) != null) {
oldAcct = oldAccts.get(acc.Id);
}
if(oldAcct != null){
if(( oldAcct.Customer_Categories_for_MC__c != acc.Customer_Categories_for_MC__c )) {
parentMap.put(acc.id,acc);
accIds.add(acc.Id);
system.debug('accids'+accIds.size()+accIds);
//if it is only one parent then updated account will be assigned to this account reference
accRef = acc;
}
}
//It is for Inserted records
if(oldAcct == null && acc.IsSoldTo__c==true){
// system.debug('insiode insert'+acc.parentid);
if(accInserIdMap.containskey(acc.parentid)){ //need to check when it will work
accInserIdMap.get(acc.parentid).add(acc);
accInserIdMap1.get(acc.DSE__DS_Ultimate_Parent__c).add(acc);
}
else{
accInserIdMap.put(acc.parentid,new List<Account>{acc});
accInserIdMap1.put(acc.DSE__DS_Ultimate_Parent__c,new List<Account>{acc});
}
}
else if(oldAcct != null && acc.IsSoldTo__C==true){
// system.debug('insiode update'+acc.parentid);
if(accInserIdMap.containskey(acc.parentid)){ //need to check when it will work
accInserIdMap.get(acc.parentid).add(acc);
accInserIdMap1.get(acc.DSE__DS_Ultimate_Parent__c).add(acc);
}
else{
accInserIdMap.put(acc.parentid,new List<Account>{acc});
accInserIdMap1.put(acc.DSE__DS_Ultimate_Parent__c,new List<Account>{acc});
}
}
}
Set<id> ultimateid=new set<id>();
//This loop only for inserted records.
if(accInserIdMap.size() > 0){
List<Account> accList = [select DSE__DS_Ultimate_Parent__c,Customer_Categories_for_MC__c,Customer_Category__c,Primary_Category_for_MC__c from Account where id in:accInserIdMap.keyset()];
system.debug(accList.size()+'accList############');
Set<string> newCatSet = new Set<string>();
List<Account> accList1 = [select DSE__DS_Ultimate_Parent__c,Customer_Categories_for_MC__c,Customer_Category__c,Primary_Category_for_MC__c from Account where id in:accInserIdMap1.keyset()];
if(accList.size() > 0 ){
for(Account acc:accList){
for(Account accUltimate:accList1){
for(Account accRefNew:accInserIdMap.get(acc.id)){
if(accRefNew.Customer_Categories_for_MC__c != null){
if(accList.size() > 0){
if(accList[0].Customer_Category__c!=null && accList[0].Customer_Category__c.split(';').size()>0){
Pattern nonAlphanumeric = Pattern.compile('[^a-zA-Z;\'/ ]');
Matcher matcher = nonAlphanumeric.matcher(string.escapeSingleQuotes(accList[0].Customer_Category__c));
string st =matcher.replaceAll('').trim().replace('; ', ';');
for(String stCat: st.split(';')){
newCatSet.add(stCat);
}
}
}
if(accRefNew.Customer_Category__c!=null && accRefNew.Customer_Category__c.split(';').size()>0){
Pattern nonAlphanumeric = Pattern.compile('[^a-zA-Z;\'/ ]');
Matcher matcher = nonAlphanumeric.matcher(string.escapeSingleQuotes(accRefNew.Customer_Category__c));
string st =matcher.replaceAll('').trim().replace('; ', ';');
for(String stCat: st.split(';')){
newCatSet.add(stCat);
}
}
/******************changes on 5th******************/
/**************changes made now***********************/
Set<String> catSet = new Set<String>();
if(acc.Customer_Categories_for_MC__c!=null){
for(String stCat: acc.Customer_Categories_for_MC__c.split(';')){
catSet.add(stCat);
}
}
for(String stCat: accRefNew.Customer_Categories_for_MC__c.split(';')){
catSet.add(stCat);
}
String custCategory = '';
String custCategoryMC = '';
Integer i=0;
for(String catValue : newCatSet){
i++;
custCategory += i+' '+catValue+';';
}
for(String catValue : catSet){
custCategoryMC += catValue+';';
}
custCategory= custCategory.substring(0,custCategory.length()-1);
custCategoryMC= custCategoryMC.substring(0,custCategoryMC.length()-1);
/**************changes made now***********************/
acc.Customer_Categories_for_MC__c = custCategoryMC;
acc.Primary_Category_for_MC__c = accRefNew.Primary_Category_for_MC__c;
acc.Customer_Category__c = custCategory;
accUltimate.Customer_Categories_for_MC__c=custCategoryMC;
accUltimate.Primary_Category_for_MC__c = accRefNew.Primary_Category_for_MC__c;
accUltimate.Customer_Category__c = custCategory;
}
}
}
update accList;
update accList1;
}
}
}
// The below loop will run for the update of the Account
System.debug('*** accIds size: ' +accIds.size());
System.debug('*** accIds size: ' +accIds);
if(!accIds.isEmpty()){
System.debug('*** accIds size: ' +accIds);
if(accIds.size()==1){
System.debug('*** accIds size inside 1 size: ' +accIds.size()+accIds);
getAllChildAccountIds(accIds);
System.debug('*** accUpdatenewList size: ' +accUpdatenewList.size());
for(Account acc:accUpdatenewList){
if(accRef.Customer_Categories_for_MC__c != null){
acc.Customer_Categories_for_MC__c = accRef.Customer_Categories_for_MC__c;
// if(acc.inherit__c == true){
acc.Customer_Category__c = accRef.Customer_Category__c;
acc.Primary_Category_for_MC__c = accRef.Customer_Categories_for_MC__c.split(';')[0];
// }
acc.IgnoreValidation__c = true;
}
}
System.debug('*** accIds size: ' +accUpdatenewList);
if(accUpdatenewList.size() > 0){
utility.IsAccountAfterupdateCalled=true;
toBeExecuted = false;//***RG
System.debug('toBeExecuted: ' +toBeExecuted);
// update accUpdatenewList;
if(accUpdatenewList.size() < 100){ //determine the best number to use based on limits with enough buffer
system.debug('inside batch'+accUpdatenewList.size());
update accUpdatenewList;
}else{
if([SELECT count() FROM AsyncApexJob WHERE JobType='BatchApex' AND Status in ('Holding','Queued','Preparing','Processing')] < 100){
system.debug('inside batch'+accUpdatenewList.size());
categorySelectionBatch batch1 = new categorySelectionBatch(accUpdatenewList);
Id batch1Id = Database.executeBatch(batch1);
}else{
Account acct = new Account();
acct.addError('There are more than 100 batches of Apex code scheduled. Please try again after some time and if the issue repeats, log a case with LST Connect team');//Use a Label
}
}
}
}
else{
System.debug('*** accIds size: ' +accids.size());
System.debug('*** accIds size: ' +accids);
Database.executeBatch( new Batch_CustomerCategoryUpdate(accIds), (Test.isRunningTest() ? accIds.size() : accIds.size() ) );
}
}
}
public static void updateCustomerCategories(List<Account> accountList, Map<Id, Account> oldAccts) {
map<string,Account> parentMap = new map<string,Account>();
map<string,list<Account>> childMap = new map<string,list<Account>>();
List<Account> updateAccount = new List<Account>();
Account accRef=new Account();
// if(!shouldRun('UpdateCustomerCategories')) return;
Set<Id> accIds = new Set<Id>();
Map<String,List<Account>> accInserIdMap = new Map<String,List<Account>>();
Map<String,List<Account>> accInserIdMap1 = new Map<String,List<Account>>();
for(Account acc:accountList){
Account oldAcct = null;
if(oldAccts != null && oldAccts.get(acc.Id) != null) {
oldAcct = oldAccts.get(acc.Id);
}
if(oldAcct != null){
if(( oldAcct.Customer_Categories_for_MC__c != acc.Customer_Categories_for_MC__c )) {
parentMap.put(acc.id,acc);
accIds.add(acc.Id);
system.debug('accids'+accIds.size()+accIds);
//if it is only one parent then updated account will be assigned to this account reference
accRef = acc;
}
}
//It is for Inserted records
if(oldAcct == null && acc.IsSoldTo__c==true){
// system.debug('insiode insert'+acc.parentid);
if(accInserIdMap.containskey(acc.parentid)){ //need to check when it will work
accInserIdMap.get(acc.parentid).add(acc);
accInserIdMap1.get(acc.DSE__DS_Ultimate_Parent__c).add(acc);
}
else{
accInserIdMap.put(acc.parentid,new List<Account>{acc});
accInserIdMap1.put(acc.DSE__DS_Ultimate_Parent__c,new List<Account>{acc});
}
}
else if(oldAcct != null && acc.IsSoldTo__C==true){
// system.debug('insiode update'+acc.parentid);
if(accInserIdMap.containskey(acc.parentid)){ //need to check when it will work
accInserIdMap.get(acc.parentid).add(acc);
accInserIdMap1.get(acc.DSE__DS_Ultimate_Parent__c).add(acc);
}
else{
accInserIdMap.put(acc.parentid,new List<Account>{acc});
accInserIdMap1.put(acc.DSE__DS_Ultimate_Parent__c,new List<Account>{acc});
}
}
}
Set<id> ultimateid=new set<id>();
//This loop only for inserted records.
if(accInserIdMap.size() > 0){
List<Account> accList = [select DSE__DS_Ultimate_Parent__c,Customer_Categories_for_MC__c,Customer_Category__c,Primary_Category_for_MC__c from Account where id in:accInserIdMap.keyset()];
system.debug(accList.size()+'accList############');
Set<string> newCatSet = new Set<string>();
List<Account> accList1 = [select DSE__DS_Ultimate_Parent__c,Customer_Categories_for_MC__c,Customer_Category__c,Primary_Category_for_MC__c from Account where id in:accInserIdMap1.keyset()];
if(accList.size() > 0 ){
for(Account acc:accList){
for(Account accUltimate:accList1){
for(Account accRefNew:accInserIdMap.get(acc.id)){
if(accRefNew.Customer_Categories_for_MC__c != null){
if(accList.size() > 0){
if(accList[0].Customer_Category__c!=null && accList[0].Customer_Category__c.split(';').size()>0){
Pattern nonAlphanumeric = Pattern.compile('[^a-zA-Z;\'/ ]');
Matcher matcher = nonAlphanumeric.matcher(string.escapeSingleQuotes(accList[0].Customer_Category__c));
string st =matcher.replaceAll('').trim().replace('; ', ';');
for(String stCat: st.split(';')){
newCatSet.add(stCat);
}
}
}
if(accRefNew.Customer_Category__c!=null && accRefNew.Customer_Category__c.split(';').size()>0){
Pattern nonAlphanumeric = Pattern.compile('[^a-zA-Z;\'/ ]');
Matcher matcher = nonAlphanumeric.matcher(string.escapeSingleQuotes(accRefNew.Customer_Category__c));
string st =matcher.replaceAll('').trim().replace('; ', ';');
for(String stCat: st.split(';')){
newCatSet.add(stCat);
}
}
/******************changes on 5th******************/
/**************changes made now***********************/
Set<String> catSet = new Set<String>();
if(acc.Customer_Categories_for_MC__c!=null){
for(String stCat: acc.Customer_Categories_for_MC__c.split(';')){
catSet.add(stCat);
}
}
for(String stCat: accRefNew.Customer_Categories_for_MC__c.split(';')){
catSet.add(stCat);
}
String custCategory = '';
String custCategoryMC = '';
Integer i=0;
for(String catValue : newCatSet){
i++;
custCategory += i+' '+catValue+';';
}
for(String catValue : catSet){
custCategoryMC += catValue+';';
}
custCategory= custCategory.substring(0,custCategory.length()-1);
custCategoryMC= custCategoryMC.substring(0,custCategoryMC.length()-1);
/**************changes made now***********************/
acc.Customer_Categories_for_MC__c = custCategoryMC;
acc.Primary_Category_for_MC__c = accRefNew.Primary_Category_for_MC__c;
acc.Customer_Category__c = custCategory;
accUltimate.Customer_Categories_for_MC__c=custCategoryMC;
accUltimate.Primary_Category_for_MC__c = accRefNew.Primary_Category_for_MC__c;
accUltimate.Customer_Category__c = custCategory;
}
}
}
update accList;
update accList1;
}
}
}
// The below loop will run for the update of the Account
System.debug('*** accIds size: ' +accIds.size());
System.debug('*** accIds size: ' +accIds);
if(!accIds.isEmpty()){
System.debug('*** accIds size: ' +accIds);
if(accIds.size()==1){
System.debug('*** accIds size inside 1 size: ' +accIds.size()+accIds);
getAllChildAccountIds(accIds);
System.debug('*** accUpdatenewList size: ' +accUpdatenewList.size());
for(Account acc:accUpdatenewList){
if(accRef.Customer_Categories_for_MC__c != null){
acc.Customer_Categories_for_MC__c = accRef.Customer_Categories_for_MC__c;
// if(acc.inherit__c == true){
acc.Customer_Category__c = accRef.Customer_Category__c;
acc.Primary_Category_for_MC__c = accRef.Customer_Categories_for_MC__c.split(';')[0];
// }
acc.IgnoreValidation__c = true;
}
}
System.debug('*** accIds size: ' +accUpdatenewList);
if(accUpdatenewList.size() > 0){
utility.IsAccountAfterupdateCalled=true;
toBeExecuted = false;//***RG
System.debug('toBeExecuted: ' +toBeExecuted);
// update accUpdatenewList;
if(accUpdatenewList.size() < 100){ //determine the best number to use based on limits with enough buffer
system.debug('inside batch'+accUpdatenewList.size());
update accUpdatenewList;
}else{
if([SELECT count() FROM AsyncApexJob WHERE JobType='BatchApex' AND Status in ('Holding','Queued','Preparing','Processing')] < 100){
system.debug('inside batch'+accUpdatenewList.size());
categorySelectionBatch batch1 = new categorySelectionBatch(accUpdatenewList);
Id batch1Id = Database.executeBatch(batch1);
}else{
Account acct = new Account();
acct.addError('There are more than 100 batches of Apex code scheduled. Please try again after some time and if the issue repeats, log a case with LST Connect team');//Use a Label
}
}
}
}
else{
System.debug('*** accIds size: ' +accids.size());
System.debug('*** accIds size: ' +accids);
Database.executeBatch( new Batch_CustomerCategoryUpdate(accIds), (Test.isRunningTest() ? accIds.size() : accIds.size() ) );
}
}
}