- Vinothini Murugesh 23
- NEWBIE
- 10 Points
- Member since 2018
-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
4Questions
-
7Replies
Apex trigger/class is throwing error from managed package .How to fix this?
i am getting error from managed package apex trigger. So i am uanble to save my record. How to stop the trigger form managed package
Regards,
Vinothini
- Vinothini Murugesh 23
- January 09, 2018
- Like
- 0
Please help to solve this error in my class
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;
}
}
- Vinothini Murugesh 23
- January 08, 2018
- Like
- 0
apex cpu time limt exceeded error for my code.any one please help me correct it.
trigger UpdateRelIntConIds5 on Contract_Group_Association__c (after delete, after insert, after update) {
Set<Id> accounts = new Set<Id>();
Map<id,id> contractgroupid=new Map<id,id>();
Set<Id> cons = new Set<Id>();
Set<Id> contracts = new Set<Id>();
system.debug('old'+Trigger.old );
if (Trigger.old != null) {
system.debug('inside update');
for (Contract_Group_Association__c c : Trigger.old) {
if (c.Internal_Contract_Group__c != null)
cons.add(c.Internal_Contract_Group__c);
}
}
if (Trigger.new != null) {
system.debug('inside insert');
for (Contract_Group_Association__c c : Trigger.new) {
if (c.Internal_Contract_Group__c != null)
cons.add(c.Internal_Contract_Group__c);
}
}
if (cons.size()> 0){
List<Customer_Groups_Association__c> conGroup = [select Internal_Contact_Group__c,Internal_Contract_Group__c from Customer_Groups_Association__c where Internal_Contract_Group__c in :cons];
system.debug('conGroup'+conGroup.size());
for (Customer_Groups_Association__c cx : conGroup) {
accounts.add(cx.Internal_Contact_Group__c);
contractgroupid.put(cx.id,cx.Internal_Contract_Group__c);
system.debug('cx.Internal_Contact_Group__c: '+cx.Internal_Contact_Group__c);
}
}
system.debug('Groups'+accounts);
UpdateRelatedInternalContractIds ur = new UpdateRelatedInternalContractIds(accounts,contractgroupid);
ur.go();
}
Class:
global class UpdateRelatedInternalContractIds {
public Set<Id> accounts {get; set;}
public Map<id,id> contractgroupid {get; set;}
public UpdateRelatedInternalContractIds(Set<Id> accounts,map<id,id> contractgroupid) {
this.accounts = accounts;
this.contractgroupid= contractgroupid;
}
public void go() {
Set<Id> contractGroups = new Set<Id>();
Set<String> t=new Set<String>();
Map<String, Set<String>> customers = new Map<String, Set<String>>();
if (accounts == null)
return;
List<Customer_Groups_Association__c> clist = [select id, Internal_Contract_Group__c, Internal_Contact_Group__c from Customer_Groups_Association__c where Internal_Contact_Group__c in :accounts];
if (clist != null) {
for (Customer_Groups_Association__c c : clist) {
contractGroups.add(c.Internal_Contract_Group__c);
t = customers.get(c.Internal_Contact_Group__c);
if (t == null) {
t = new Set<String>();
customers.put(c.Internal_Contact_Group__c, t);
}
t.add(c.Internal_Contract_Group__c);
}
}
Map<String, Set<String>> contractIds = new Map<String, Set<String>>();
List<Contract_Group_Association__c> dlist = [select id, Internal_Contract__r.Internal_Contract_ID__c, Internal_Contract_Group__c from Contract_Group_Association__c where Internal_Contract_Group__c in :contractGroups];
if (dlist != null) {
for (Contract_Group_Association__c d : dlist) {
t = contractIds.get(d.Internal_Contract_Group__c);
if (t == null) {
t = new Set<String>();
contractIds.put(d.Internal_Contract_Group__c, t);
}
t.add(d.Internal_Contract__r.Internal_Contract_ID__c);
}
}
List<Account> customerList = [select id, Related_Internal_Contract_IDs__c from Account where id in :accounts];
for (Account c : customerList) {
String result = '';
if (customers.containsKey(c.id)) {
system.debug('customer@@' + c);
for (String s : customers.get(c.id)) {
if (!contractIds.containsKey(s))
continue;
for (String x : contractIds.get(s)) {
if (result != '')
result += ',';
result += x;
}
}
}
c.Related_Internal_Contract_IDs__c = result;
}
update customerList;
}
public void goNew() {
List<Contract_Group_Association__c> dlist = [select id, Internal_Contract__r.Internal_Contract_ID__c, Internal_Contract_Group__c from Contract_Group_Association__c where Internal_Contract_Group__c in (select Internal_Contract_Group__c from Customer_Groups_Association__c where Internal_Contact_Group__c in :accounts)];
Map<Id,String> relatedIds = new Map<Id,String>();
if(!dlist.isEmpty()){
for(Contract_Group_Association__c conAss : dlist){
//conAss.Internal_Contract__r.Internal_Contract_ID__c;
}
}
}
}
- Vinothini Murugesh 23
- January 04, 2018
- Like
- 0
CPU Time Limit exceeded error for my code.please help me to solve this
trigger UpdateRelIntConIds5 on Contract_Group_Association__c (after delete, after insert, after update) {
Set<Id> accounts = new Set<Id>();
Map<id,id> contractgroupid=new Map<id,id>();
Set<Id> cons = new Set<Id>();
Set<Id> contracts = new Set<Id>();
system.debug('old'+Trigger.old );
if (Trigger.old != null) {
system.debug('inside update');
for (Contract_Group_Association__c c : Trigger.old) {
if (c.Internal_Contract_Group__c != null)
cons.add(c.Internal_Contract_Group__c);
}
}
if (Trigger.new != null) {
system.debug('inside insert');
for (Contract_Group_Association__c c : Trigger.new) {
if (c.Internal_Contract_Group__c != null)
cons.add(c.Internal_Contract_Group__c);
}
}
if (cons.size()> 0){
List<Customer_Groups_Association__c> conGroup = [select Internal_Contact_Group__c,Internal_Contract_Group__c from Customer_Groups_Association__c where Internal_Contract_Group__c in :cons];
system.debug('conGroup'+conGroup.size());
for (Customer_Groups_Association__c cx : conGroup) {
accounts.add(cx.Internal_Contact_Group__c);
contractgroupid.put(cx.id,cx.Internal_Contract_Group__c);
system.debug('cx.Internal_Contact_Group__c: '+cx.Internal_Contact_Group__c);
}
}
system.debug('Groups'+accounts);
UpdateRelatedInternalContractIds ur = new UpdateRelatedInternalContractIds(accounts,contractgroupid);
ur.go();
}
apex class:
global class UpdateRelatedInternalContractIds {
public Set<Id> accounts {get; set;}
public Map<id,id> contractgroupid {get; set;}
public UpdateRelatedInternalContractIds(Set<Id> accounts,map<id,id> contractgroupid) {
this.accounts = accounts;
this.contractgroupid= contractgroupid;
}
public void go() {
Set<Id> contractGroups = new Set<Id>();
Map<String, Set<String>> customers = new Map<String, Set<String>>();
if (accounts == null)
return;
List<Customer_Groups_Association__c> clist = [select id, Internal_Contract_Group__c, Internal_Contact_Group__c from Customer_Groups_Association__c where Internal_Contact_Group__c in :accounts];
if (clist != null) {
for (Customer_Groups_Association__c c : clist) {
contractGroups.add(c.Internal_Contract_Group__c);
Set<String> t = customers.get(c.Internal_Contact_Group__c);
if (t == null) {
t = new Set<String>();
customers.put(c.Internal_Contact_Group__c, t);
}
t.add(c.Internal_Contract_Group__c);
}
}
Map<String, Set<String>> contractIds = new Map<String, Set<String>>();
List<Contract_Group_Association__c> dlist = [select id, Internal_Contract__r.Internal_Contract_ID__c, Internal_Contract_Group__c from Contract_Group_Association__c where Internal_Contract_Group__c in :contractGroups];
if (dlist != null) {
for (Contract_Group_Association__c d : dlist) {
Set<String> t = contractIds.get(d.Internal_Contract_Group__c);
if (t == null) {
t = new Set<String>();
contractIds.put(d.Internal_Contract_Group__c, t);
}
t.add(d.Internal_Contract__r.Internal_Contract_ID__c);
}
}
List<Account> customerList = [select id, Related_Internal_Contract_IDs__c from Account where id in :accounts];
for (Account c : customerList) {
String result = '';
if (customers.containsKey(c.id)) {
system.debug('customer@@' + c);
for (String s : customers.get(c.id)) {
if (!contractIds.containsKey(s))
continue;
for (String x : contractIds.get(s)) {
if (result != '')
result += ',';
result += x;
}
}
}
c.Related_Internal_Contract_IDs__c = result;
}
update customerList;
}
- Vinothini Murugesh 23
- January 04, 2018
- Like
- 0
Apex trigger/class is throwing error from managed package .How to fix this?
i am getting error from managed package apex trigger. So i am uanble to save my record. How to stop the trigger form managed package
Regards,
Vinothini
- Vinothini Murugesh 23
- January 09, 2018
- Like
- 0
Please help to solve this error in my class
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;
}
}
- Vinothini Murugesh 23
- January 08, 2018
- Like
- 0
apex cpu time limt exceeded error for my code.any one please help me correct it.
trigger UpdateRelIntConIds5 on Contract_Group_Association__c (after delete, after insert, after update) {
Set<Id> accounts = new Set<Id>();
Map<id,id> contractgroupid=new Map<id,id>();
Set<Id> cons = new Set<Id>();
Set<Id> contracts = new Set<Id>();
system.debug('old'+Trigger.old );
if (Trigger.old != null) {
system.debug('inside update');
for (Contract_Group_Association__c c : Trigger.old) {
if (c.Internal_Contract_Group__c != null)
cons.add(c.Internal_Contract_Group__c);
}
}
if (Trigger.new != null) {
system.debug('inside insert');
for (Contract_Group_Association__c c : Trigger.new) {
if (c.Internal_Contract_Group__c != null)
cons.add(c.Internal_Contract_Group__c);
}
}
if (cons.size()> 0){
List<Customer_Groups_Association__c> conGroup = [select Internal_Contact_Group__c,Internal_Contract_Group__c from Customer_Groups_Association__c where Internal_Contract_Group__c in :cons];
system.debug('conGroup'+conGroup.size());
for (Customer_Groups_Association__c cx : conGroup) {
accounts.add(cx.Internal_Contact_Group__c);
contractgroupid.put(cx.id,cx.Internal_Contract_Group__c);
system.debug('cx.Internal_Contact_Group__c: '+cx.Internal_Contact_Group__c);
}
}
system.debug('Groups'+accounts);
UpdateRelatedInternalContractIds ur = new UpdateRelatedInternalContractIds(accounts,contractgroupid);
ur.go();
}
Class:
global class UpdateRelatedInternalContractIds {
public Set<Id> accounts {get; set;}
public Map<id,id> contractgroupid {get; set;}
public UpdateRelatedInternalContractIds(Set<Id> accounts,map<id,id> contractgroupid) {
this.accounts = accounts;
this.contractgroupid= contractgroupid;
}
public void go() {
Set<Id> contractGroups = new Set<Id>();
Set<String> t=new Set<String>();
Map<String, Set<String>> customers = new Map<String, Set<String>>();
if (accounts == null)
return;
List<Customer_Groups_Association__c> clist = [select id, Internal_Contract_Group__c, Internal_Contact_Group__c from Customer_Groups_Association__c where Internal_Contact_Group__c in :accounts];
if (clist != null) {
for (Customer_Groups_Association__c c : clist) {
contractGroups.add(c.Internal_Contract_Group__c);
t = customers.get(c.Internal_Contact_Group__c);
if (t == null) {
t = new Set<String>();
customers.put(c.Internal_Contact_Group__c, t);
}
t.add(c.Internal_Contract_Group__c);
}
}
Map<String, Set<String>> contractIds = new Map<String, Set<String>>();
List<Contract_Group_Association__c> dlist = [select id, Internal_Contract__r.Internal_Contract_ID__c, Internal_Contract_Group__c from Contract_Group_Association__c where Internal_Contract_Group__c in :contractGroups];
if (dlist != null) {
for (Contract_Group_Association__c d : dlist) {
t = contractIds.get(d.Internal_Contract_Group__c);
if (t == null) {
t = new Set<String>();
contractIds.put(d.Internal_Contract_Group__c, t);
}
t.add(d.Internal_Contract__r.Internal_Contract_ID__c);
}
}
List<Account> customerList = [select id, Related_Internal_Contract_IDs__c from Account where id in :accounts];
for (Account c : customerList) {
String result = '';
if (customers.containsKey(c.id)) {
system.debug('customer@@' + c);
for (String s : customers.get(c.id)) {
if (!contractIds.containsKey(s))
continue;
for (String x : contractIds.get(s)) {
if (result != '')
result += ',';
result += x;
}
}
}
c.Related_Internal_Contract_IDs__c = result;
}
update customerList;
}
public void goNew() {
List<Contract_Group_Association__c> dlist = [select id, Internal_Contract__r.Internal_Contract_ID__c, Internal_Contract_Group__c from Contract_Group_Association__c where Internal_Contract_Group__c in (select Internal_Contract_Group__c from Customer_Groups_Association__c where Internal_Contact_Group__c in :accounts)];
Map<Id,String> relatedIds = new Map<Id,String>();
if(!dlist.isEmpty()){
for(Contract_Group_Association__c conAss : dlist){
//conAss.Internal_Contract__r.Internal_Contract_ID__c;
}
}
}
}
- Vinothini Murugesh 23
- January 04, 2018
- Like
- 0
How to prevent "Apex CPU Time Limit Exceeded"?
I am building a trigger to automatically associate a custom Account child object with a custom Opportunity child object. My trigger works fine and tests perfectly, except when I went to upload a bunch of records to the new object, I received the following error on every item:
System.LimitException: Apex CPU time limit exceeded
I found a helpful article here:
http://salesforce.stackexchange.com/questions/22223/how-to-code-more-efficient-to-avoid-apex-cpu-time-limit-exceeded
But I'm still not sure what I'm doing wrong. Does anyone have any ideas?
Here is the trigger code:
<pre>
trigger associateOppChildtoAccChild on OppChild__c (before insert, before update) {
Set<Id> OppSet = new Set<Id>();
for(OppChild__c OppChild :Trigger.new){
OppSet.add(OppChild.Opportunity__c);
}
Map<ID,ID> OPP_ACC = new Map<ID,ID>();
Set<Id> AccSet = new Set<Id>();
for(Opportunity OPP:[SELECT Id, AccountId from Opportunity where Id in :OppSet]){
OPP_ACC.put(OPP.Id, OPP.AccountId);
AccSet.add(OPP.AccountId);
}
Map<ID,ID> ACC_OppChild = new Map<ID,ID>();
for(OppChild__c OppChild :Trigger.new){
for(ID OPP :OPP_ACC.keyset()){
if(OPP == OppChild.Opportunity__c){
ACC_OppChild.put(OPP_ACC.get(OPP), OppChild.Id);
}
}
}
List<AccChild__c> AccChildList = [SELECT Id, Account__c, Type__c, Annual_Spend__c, Number_of_Shipment_Transactions__c FROM AccChild__c WHERE Account__c in :AccSet];
for(OppChild__c OppChild :Trigger.new){
for(AccChild__c m: AccChildList){
if(m.Type__c == OppChild.AccChild_Type__c){
for(ID ACC :ACC_OppChild.keyset()){
if(ACC == m.Account__c){
OppChild.AccChild__c = m.Id;
System.debug('OppChild AccChild = ' + OppChild.AccChild__c);
OppChild.Annual_Spend__c = m.Annual_Spend__c;
OppChild.Number_of_Shipment_Transactions__c = m.Number_of_Shipment_Transactions__c;
} else {
OppChild.adderror(OppChild.AccChild_Type__c + ' AccChild must exist on Account to add OppChild');
}
}
}
}
}
}
</pre>
- ilewi121
- February 06, 2014
- Like
- 1
System.LimitException: Apex CPU time limit exceeded
Hello,
Requirement: To update four picklist field values with dependencies. As the numbers of picklist values are high it’s difficult to manage the Field Dependencies manually.
Product Family
Product Line
Product Module
Product Version
Design Approach: So we planned to go for customization. We used four custom settings to store the picklist values. Then update the picklist fields values using Metadata API. Below are the design of four custom settings.
Product Family
Family1
Family2
Family3
Product Line
Family1 - Line1
Family2 - Line2
Family3 - Line3
Product Module
Line1 - Module1
Line1 - Module2
Line2 - Module2
Product Version
Module1 - Version1
Module2 - Version1
Module2 - Version2
Problem: Though the above design works for less volume of data, I am getting “System.LimitException: Apex CPU time limit exceeded” error when the number of picklist values is high (i.e. more than 1000).
I have generated a Metadata Service class from the MetadataAPI WSDL.
I need a map of following format to pass into "UpdateMetadata" method(in Metadata Service class).
Map<String, List<String>> OR Map<DependantValue, List<ControllingValues>>
To get the data in above format from custom setting, I am using below code.
Set<String> setDependantValue = new Set<String>();
for(Product_Line__c objPicklistValues : lstProduct_Line){
setDependantValue.add(objPicklistValues.Product_Line_Name__c);
}
Map<String, List<String>> mapDependantControllingValue = new Map<String, List<String>>();
for(String objDepVal : setDependantValue){
List<String> lstControllingValues = new List<String>();
for(Product_Line__c objPicklistValues : lstProduct_Line){
if(objDepVal == objPicklistValues.Product_Line_Name__c){
lstControllingValues.add(objPicklistValues.Product_Family_Name__c);
}
}
mapDependantControllingValue.put(objDepVal, lstControllingValues);
}
I think the error is coming due to FOR inside FOR loop.
Is there any other way to optimize the code or any solution?
Thank you
Swagat
- Swagat.Tushar
- October 24, 2013
- Like
- 0