You need to sign in to do that
Don't have an account?
Vinothini Murugesh 23
Please help to solve this error in my class
Trigger:
trigger Account_AU on Account (before insert, before update, after insert, after update)
{
system.debug('START --------------'+AccountTriggers.toBeExecuted);
if(AccountTriggers.toBeExecuted){
if(Trigger.isBefore) {
if(Trigger.isInsert) {
AccountTriggers.setRecordType(Trigger.new, (new Map<Id, Account>()));
AccountTriggers.updateAccFields(Trigger.new,(new Map<Id, Account>()),true);
AccountTriggers.updateCustomerCategories(Trigger.new, null);
} else if(Trigger.isUpdate) {
//AccountTriggers.retainFieldsWhenAccountIsInactive(Trigger.newMap, Trigger.oldMap);
AccountTriggers.setRecordType(Trigger.new, Trigger.oldMap);
AccountTriggers.updateAccFields(Trigger.new, Trigger.oldMap,false);
}
} else if(Trigger.isAfter) {
if(Trigger.isInsert) {
AccountTriggers.FlagContactsForAccountSharing(Trigger.New, null);
if(!utility.IsAccountAfterInsertCalled){
utility.IsAccountAfterInsertCalled=true;
utility.IsAccountAfterUpdateCalled=true;
AccountLocationMappingtoStore.deepCloneAccountLocation(Trigger.new,trigger.oldMap,true);
AccountTriggers.deepCloneAccount(Trigger.new, (new Map<Id, Account>()));
AccountTriggers.CheckdeptAccount(Trigger.new);
}
} else if(Trigger.isUpdate) {
system.debug('inside update');
AccountTriggers.FlagContactsForAccountSharing(Trigger.New, Trigger.oldMap);
AccountTriggers.CheckdeptAccount(Trigger.new);
AccountTriggers.managingCountry_Update(Trigger.New, Trigger.oldMap);
if(!utility.IsAccountAfterupdateCalled){
utility.IsAccountAfterupdateCalled=true;
//To update the existing location.
AccountTriggers.deepCloneAccount(Trigger.new,Trigger.oldMap);
system.debug('entering inside');
AccountTriggers.updateCustomerCategories(Trigger.new, Trigger.oldMap);
/** To create/update store and location--B-160815**/
AccountLocationMappingtoStore.deepCloneAccountLocation(Trigger.new,trigger.oldMap,false);
}
}
} // end isAfter
}
}
class and Method:
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>>();
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);
//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.parentid != 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);
}
else{
accInserIdMap.put(acc.parentid,new List<Account>{acc});
}
}
else if(oldAcct != null && acc.parentid != 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);
}
else{
system.debug('insiode update1'+acc.parentid);
accInserIdMap.put(acc.parentid,new List<Account>{acc});
}
}
}
//This loop only for inserted records.
if(accInserIdMap.size() > 0){
system.debug(accInserIdMap+'accInserIdMap############');
List<Account> accList = [select Customer_Categories_for_MC__c,Customer_Category__c,Primary_Category_for_MC__c from Account where id in:accInserIdMap.keyset()];
system.debug(accList+'accList############');
Set<string> newCatSet = new Set<string>();
if(accList.size() > 0){
for(Account acc:accList){
for(Account accRefNew:accInserIdMap.get(acc.id)){
if(accRefNew.Customer_Categories_for_MC__c != null){
/******************changes on 5th******************/
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);
}
}
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);
//custCategory.charAt(custCategory.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;
//acc.Customer_Category__c = accRefNew.Customer_Category__c;
}
}
}
update accList;
system.debug('accList'+accList);
}
}
// The below loop will run for the update of the Account
if(!accIds.isEmpty()){
system.debug('inside account id'+accIds.size());
if(accIds.size()==1){
getAllChildAccountIds(accIds);
system.debug('accIds'+accIds);
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;
}
}
if(accUpdatenewList.size() > 0){
utility.IsAccountAfterupdateCalled=true;
update accUpdatenewList;
}
}
else{
Database.executeBatch(new Batch_CustomerCategoryUpdate(accIds), (Test.isRunningTest() ? accIds.size() : 1) );
}
}
/* catch(Exception ex){
System.debug('Cat Exception'+ex.getmessage());
} */
}
// Return list of all child Account Ids
public static void getAllChildAccountIds(set<Id> setAccId){
Set<Id> tempIdSet=new Set<Id>();
for(Account acc:[Select Id, name,Customer_Category__c, Synchronize__c,Customer_Categories_for_MC__c,inherit__c,Primary_Category_for_MC__c from Account where parentid in :setAccId]){
accUpdatenewList.add(acc);
tempIdSet.add(acc.id);
}
if(tempIdSet.size()>0){
setAccountIds.addAll(tempIdSet);
getAllChildAccountIds(tempIdSet);
}
}
public static void updateAccFields(List<Account> accountList, Map<Id, Account> oldAccts,boolean check){
Id locationRecordID = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Location').getRecordTypeId();
List<account> accUpdateList = new List<account>();
//Following else loop will only run for the update of Account
if(oldAccts != null&&check==false){
Map<string,Account> soldToAccMap = new Map<string,Account>();
Map<string,Account> soldToAccMap1 = new Map<string,Account>();
Map<string,Account> sapAccNumberMap = new Map<string,Account>();
for(Account acc:accountList){
if(acc.SAPAccountNum__c==null && acc.IsSoldTo__c==false && locationRecordID==acc.RecordTypeId&&( acc.CustomerDistributionTier__c != oldAccts.get(acc.id).CustomerDistributionTier__c || acc.Managing_Country__c != oldAccts.get(acc.id).Managing_Country__c ||acc.Lead_Category__c!=oldAccts.get(acc.id).Lead_Category__c||
acc.SalesOrg__c != oldAccts.get(acc.id).SalesOrg__c||acc.Account_Class__c != oldAccts.get(acc.id).Account_Class__c || acc.Channel_Class_Name__c != oldAccts.get(acc.id).Channel_Class_Name__c||acc.customer_Category__c!=oldAccts.get(acc.id).customer_Category__c)){
soldToAccMap.put(acc.id,acc);
soldToAccMap1.put(acc.Sold_to__c,acc);
if(acc.SAPAccountNum__c != null && acc.SAPAccountNum__c!=''){
sapAccNumberMap.put(acc.SAPAccountNum__c,acc);
}
}
}
if(soldToAccMap.size() > 0 ){
set<string> soldTo=soldToAccMap.keyset();
set<string> soldTo1=soldToAccMap1.keyset();
set<string> sapSet= new set<string>();
string query = 'select id,Sold_to__c,Customer_Category__c,Lead_Category__c,Sold_To_Number_Text__c,GBHGeo__c,GBHTerritory__c, CountryCode__c,SalesOrg__c,CustomerDistributionTier__c,SAPAccountNum__c,Channel_Class_Name__c,Managing_Country__c,Account_Class__c from account where ID in:soldTo1';
if(sapAccNumberMap.size() > 0){
sapSet=sapAccNumberMap.keyset();
//query = query+' or Sold_To_Number_Text__c in:sapSet';
}
for(Account accRef:(List<Account>)Database.query(query)){
if(accRef.Sold_To_Number_Text__c != '' && accRef.Sold_to__c == null){
Account acc = new Account(id = accRef.id);
acc.CustomerDistributionTier__c = accRef.CustomerDistributionTier__c;
acc.Channel_Class_Name__c = accRef.Channel_Class_Name__c;
acc.Managing_Country__c = accRef.Managing_Country__c;
acc.SalesOrg__c=accRef.SalesOrg__c;
acc.Account_Class__c = accRef.Account_Class__c;
acc.Lead_Category__c=accRef.Lead_Category__c;
acc.Customer_Category__c=accRef.Customer_Category__c;
// if(acc.Managing_Country__c == null){
// }
accUpdateList.add(acc);
}
}
}
}
if(accUpdateList.size() > 0){
utility.IsAccountAfterUpdateCalled=true;
Update accUpdateList;
}
}
trigger Account_AU on Account (before insert, before update, after insert, after update)
{
system.debug('START --------------'+AccountTriggers.toBeExecuted);
if(AccountTriggers.toBeExecuted){
if(Trigger.isBefore) {
if(Trigger.isInsert) {
AccountTriggers.setRecordType(Trigger.new, (new Map<Id, Account>()));
AccountTriggers.updateAccFields(Trigger.new,(new Map<Id, Account>()),true);
AccountTriggers.updateCustomerCategories(Trigger.new, null);
} else if(Trigger.isUpdate) {
//AccountTriggers.retainFieldsWhenAccountIsInactive(Trigger.newMap, Trigger.oldMap);
AccountTriggers.setRecordType(Trigger.new, Trigger.oldMap);
AccountTriggers.updateAccFields(Trigger.new, Trigger.oldMap,false);
}
} else if(Trigger.isAfter) {
if(Trigger.isInsert) {
AccountTriggers.FlagContactsForAccountSharing(Trigger.New, null);
if(!utility.IsAccountAfterInsertCalled){
utility.IsAccountAfterInsertCalled=true;
utility.IsAccountAfterUpdateCalled=true;
AccountLocationMappingtoStore.deepCloneAccountLocation(Trigger.new,trigger.oldMap,true);
AccountTriggers.deepCloneAccount(Trigger.new, (new Map<Id, Account>()));
AccountTriggers.CheckdeptAccount(Trigger.new);
}
} else if(Trigger.isUpdate) {
system.debug('inside update');
AccountTriggers.FlagContactsForAccountSharing(Trigger.New, Trigger.oldMap);
AccountTriggers.CheckdeptAccount(Trigger.new);
AccountTriggers.managingCountry_Update(Trigger.New, Trigger.oldMap);
if(!utility.IsAccountAfterupdateCalled){
utility.IsAccountAfterupdateCalled=true;
//To update the existing location.
AccountTriggers.deepCloneAccount(Trigger.new,Trigger.oldMap);
system.debug('entering inside');
AccountTriggers.updateCustomerCategories(Trigger.new, Trigger.oldMap);
/** To create/update store and location--B-160815**/
AccountLocationMappingtoStore.deepCloneAccountLocation(Trigger.new,trigger.oldMap,false);
}
}
} // end isAfter
}
}
class and Method:
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>>();
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);
//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.parentid != 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);
}
else{
accInserIdMap.put(acc.parentid,new List<Account>{acc});
}
}
else if(oldAcct != null && acc.parentid != 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);
}
else{
system.debug('insiode update1'+acc.parentid);
accInserIdMap.put(acc.parentid,new List<Account>{acc});
}
}
}
//This loop only for inserted records.
if(accInserIdMap.size() > 0){
system.debug(accInserIdMap+'accInserIdMap############');
List<Account> accList = [select Customer_Categories_for_MC__c,Customer_Category__c,Primary_Category_for_MC__c from Account where id in:accInserIdMap.keyset()];
system.debug(accList+'accList############');
Set<string> newCatSet = new Set<string>();
if(accList.size() > 0){
for(Account acc:accList){
for(Account accRefNew:accInserIdMap.get(acc.id)){
if(accRefNew.Customer_Categories_for_MC__c != null){
/******************changes on 5th******************/
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);
}
}
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);
//custCategory.charAt(custCategory.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;
//acc.Customer_Category__c = accRefNew.Customer_Category__c;
}
}
}
update accList;
system.debug('accList'+accList);
}
}
// The below loop will run for the update of the Account
if(!accIds.isEmpty()){
system.debug('inside account id'+accIds.size());
if(accIds.size()==1){
getAllChildAccountIds(accIds);
system.debug('accIds'+accIds);
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;
}
}
if(accUpdatenewList.size() > 0){
utility.IsAccountAfterupdateCalled=true;
update accUpdatenewList;
}
}
else{
Database.executeBatch(new Batch_CustomerCategoryUpdate(accIds), (Test.isRunningTest() ? accIds.size() : 1) );
}
}
/* catch(Exception ex){
System.debug('Cat Exception'+ex.getmessage());
} */
}
// Return list of all child Account Ids
public static void getAllChildAccountIds(set<Id> setAccId){
Set<Id> tempIdSet=new Set<Id>();
for(Account acc:[Select Id, name,Customer_Category__c, Synchronize__c,Customer_Categories_for_MC__c,inherit__c,Primary_Category_for_MC__c from Account where parentid in :setAccId]){
accUpdatenewList.add(acc);
tempIdSet.add(acc.id);
}
if(tempIdSet.size()>0){
setAccountIds.addAll(tempIdSet);
getAllChildAccountIds(tempIdSet);
}
}
public static void updateAccFields(List<Account> accountList, Map<Id, Account> oldAccts,boolean check){
Id locationRecordID = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Location').getRecordTypeId();
List<account> accUpdateList = new List<account>();
//Following else loop will only run for the update of Account
if(oldAccts != null&&check==false){
Map<string,Account> soldToAccMap = new Map<string,Account>();
Map<string,Account> soldToAccMap1 = new Map<string,Account>();
Map<string,Account> sapAccNumberMap = new Map<string,Account>();
for(Account acc:accountList){
if(acc.SAPAccountNum__c==null && acc.IsSoldTo__c==false && locationRecordID==acc.RecordTypeId&&( acc.CustomerDistributionTier__c != oldAccts.get(acc.id).CustomerDistributionTier__c || acc.Managing_Country__c != oldAccts.get(acc.id).Managing_Country__c ||acc.Lead_Category__c!=oldAccts.get(acc.id).Lead_Category__c||
acc.SalesOrg__c != oldAccts.get(acc.id).SalesOrg__c||acc.Account_Class__c != oldAccts.get(acc.id).Account_Class__c || acc.Channel_Class_Name__c != oldAccts.get(acc.id).Channel_Class_Name__c||acc.customer_Category__c!=oldAccts.get(acc.id).customer_Category__c)){
soldToAccMap.put(acc.id,acc);
soldToAccMap1.put(acc.Sold_to__c,acc);
if(acc.SAPAccountNum__c != null && acc.SAPAccountNum__c!=''){
sapAccNumberMap.put(acc.SAPAccountNum__c,acc);
}
}
}
if(soldToAccMap.size() > 0 ){
set<string> soldTo=soldToAccMap.keyset();
set<string> soldTo1=soldToAccMap1.keyset();
set<string> sapSet= new set<string>();
string query = 'select id,Sold_to__c,Customer_Category__c,Lead_Category__c,Sold_To_Number_Text__c,GBHGeo__c,GBHTerritory__c, CountryCode__c,SalesOrg__c,CustomerDistributionTier__c,SAPAccountNum__c,Channel_Class_Name__c,Managing_Country__c,Account_Class__c from account where ID in:soldTo1';
if(sapAccNumberMap.size() > 0){
sapSet=sapAccNumberMap.keyset();
//query = query+' or Sold_To_Number_Text__c in:sapSet';
}
for(Account accRef:(List<Account>)Database.query(query)){
if(accRef.Sold_To_Number_Text__c != '' && accRef.Sold_to__c == null){
Account acc = new Account(id = accRef.id);
acc.CustomerDistributionTier__c = accRef.CustomerDistributionTier__c;
acc.Channel_Class_Name__c = accRef.Channel_Class_Name__c;
acc.Managing_Country__c = accRef.Managing_Country__c;
acc.SalesOrg__c=accRef.SalesOrg__c;
acc.Account_Class__c = accRef.Account_Class__c;
acc.Lead_Category__c=accRef.Lead_Category__c;
acc.Customer_Category__c=accRef.Customer_Category__c;
// if(acc.Managing_Country__c == null){
// }
accUpdateList.add(acc);
}
}
}
}
if(accUpdateList.size() > 0){
utility.IsAccountAfterUpdateCalled=true;
Update accUpdateList;
}
}
Update failed. First exception on row 0 with id 001U000001Qffa9IAB; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Account_AU: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 001U000000Vm9lbIAB; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Account_AU: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 001U000000Vm9lbIAB; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 001U000000Vm9lb) is currently in trigger Account_AU, therefore it cannot recursively update itself: [] Class.AccountTriggers.updateAccFields: line 755, column 1 Trigger.Account_AU: line 22, column 1: [] Class.AccountTriggers.updateCustomerCategories: line 585, column 1 Trigger.Account_AU: line 54, column 1: []
https://help.salesforce.com/articleView?id=000133752&language=en_US&type=1
I am doing update in my beofre update functio which i am doing this in after update also.If i comment out the update operation which i am doing iti in beofore update , it is working fine.