You need to sign in to do that
Don't have an account?
SFDC Learning 6
CPU time exceed error in trigger
trigger PreviousGLSalesHandling on financialData__c (before insert) {
list<financialData__c> saleList = new list<financialData__c>();
list<string> saleListacc = new list<string>();
for(financialData__c s:trigger.new) {
saleList.add(s);
saleListacc.add(s.account__c);
}
list<financialData__c> sllist = [select id,Year__c,Month__c,Account__c,GL_Sales__c, Land_Comm__c from financialData__c where account__C in:saleListacc];
for(financialData__c st: saleList) {
for(financialData__c sm: sllist){
if(st.Year__c!=null && st.Month__c!=null && st.Account__c!=null && sm.Year__c!=null && sm.Month__c!=null && sm.Account__c!=null ){
String yr = String.valueof(st.Year__c-1);
String mnth = String.valueof(st.Month__c);
integer monthhh;
if(mnth=='jan'){monthhh=1;}
else if(mnth=='feb'){monthhh=2;}
else if(mnth=='mar'){monthhh=3;}
else if(mnth=='apr'){monthhh=4;}
else if(mnth=='may'){monthhh=5;}
else if(mnth=='jun'){monthhh=6;}
else if(mnth=='jul'){monthhh=7;}
else if(mnth=='aug'){monthhh=8;}
else if(mnth=='sep'){monthhh=9;}
else if(mnth=='oct'){monthhh=10;}
else if(mnth=='nov'){monthhh=11;}
else if(mnth=='dec'){monthhh=12;}
String acc = st.Account__c;
Date dd = Date.newInstance(integer.valueof(st.year__c),monthhh, 1);
Integer numberOfDays = Date.daysInMonth(dd.year(), dd.month());
Date lastDayOfMonth = Date.newInstance(dd.year(), dd.month(), numberOfDays);
system.debug('sssssssssss'+numberOfDays );
st.Start_Day_of_Month__c = '1';
st.End_Day_of_Month__c = string.valueof(numberOfDays) ;
if(yr.contains(String.valueof(sm.Year__c)) && mnth.contains(String.valueof(sm.Month__c)) && acc.contains(sm.Account__c)){
st.Previous_year_GL_Sales__c = sm.GL_Sales__c;
//st.Previous_Year_Comm__c = sm.Land_Comm__c;
}
}
}
}
}
I am getting errro of CPU time exceed. Please help me! Thanks in advance
list<financialData__c> saleList = new list<financialData__c>();
list<string> saleListacc = new list<string>();
for(financialData__c s:trigger.new) {
saleList.add(s);
saleListacc.add(s.account__c);
}
list<financialData__c> sllist = [select id,Year__c,Month__c,Account__c,GL_Sales__c, Land_Comm__c from financialData__c where account__C in:saleListacc];
for(financialData__c st: saleList) {
for(financialData__c sm: sllist){
if(st.Year__c!=null && st.Month__c!=null && st.Account__c!=null && sm.Year__c!=null && sm.Month__c!=null && sm.Account__c!=null ){
String yr = String.valueof(st.Year__c-1);
String mnth = String.valueof(st.Month__c);
integer monthhh;
if(mnth=='jan'){monthhh=1;}
else if(mnth=='feb'){monthhh=2;}
else if(mnth=='mar'){monthhh=3;}
else if(mnth=='apr'){monthhh=4;}
else if(mnth=='may'){monthhh=5;}
else if(mnth=='jun'){monthhh=6;}
else if(mnth=='jul'){monthhh=7;}
else if(mnth=='aug'){monthhh=8;}
else if(mnth=='sep'){monthhh=9;}
else if(mnth=='oct'){monthhh=10;}
else if(mnth=='nov'){monthhh=11;}
else if(mnth=='dec'){monthhh=12;}
String acc = st.Account__c;
Date dd = Date.newInstance(integer.valueof(st.year__c),monthhh, 1);
Integer numberOfDays = Date.daysInMonth(dd.year(), dd.month());
Date lastDayOfMonth = Date.newInstance(dd.year(), dd.month(), numberOfDays);
system.debug('sssssssssss'+numberOfDays );
st.Start_Day_of_Month__c = '1';
st.End_Day_of_Month__c = string.valueof(numberOfDays) ;
if(yr.contains(String.valueof(sm.Year__c)) && mnth.contains(String.valueof(sm.Month__c)) && acc.contains(sm.Account__c)){
st.Previous_year_GL_Sales__c = sm.GL_Sales__c;
//st.Previous_Year_Comm__c = sm.Land_Comm__c;
}
}
}
}
}
I am getting errro of CPU time exceed. Please help me! Thanks in advance
Error is due to more number of execution.
Looks like, you want to populate Previous Year GL Sale for the same account.
You can populate the Map<Account, with Previous Year Sale>
and then you can iterate over, trigger.new and populate Previous Year GL Sale's Field value from populated Map.
Thanks
I tried it but its not working
trigger PreviousGLSalesHandlingFinal on financialData__c (after insert) {
map<id,financialData__c> salemap=new map<id,financialData__c>();
list<financialData__c> saleList = new list<financialData__c>();
for(financialData__c s:trigger.new) {
salemap.put(s.id,s);
}
list<financialData__c> sllist = [select id,Year__c,Month__c,Account__c,GL_Sales__c, Land_Comm__c from financialData__c];
for(financialData__c ss:sllist){
if(ss.Year__c!=null && ss.Month__c!=null && ss.Account__c!=null){
String yr = String.valueof(ss.Year__c-1);
String mnth = String.valueof(ss.Month__c);
integer monthhh;
if(mnth=='jan'){monthhh=1;}
else if(mnth=='feb'){monthhh=2;}
else if(mnth=='mar'){monthhh=3;}
else if(mnth=='apr'){monthhh=4;}
else if(mnth=='may'){monthhh=5;}
else if(mnth=='jun'){monthhh=6;}
else if(mnth=='jul'){monthhh=7;}
else if(mnth=='aug'){monthhh=8;}
else if(mnth=='sep'){monthhh=9;}
else if(mnth=='oct'){monthhh=10;}
else if(mnth=='nov'){monthhh=11;}
else if(mnth=='dec'){monthhh=12;}
String acc = ss.Account__c;
if (salemap.containsKey(yr) && salemap.containsKey(mnth) && salemap.containsKey(acc)) {
system.debug('ssssssssssssssss'+salemap.get(yr)+salemap.get(mnth));
} }
}
}
public Class checkRecursive{
private static boolean run = true;
public static boolean runOnce(){
if(run){
run=false;
return true;
}
else{
return run;
}
}
}
trigger PreviousGLSalesHandlingFinal on financialData__c (before insert) {
if(checkRecursive.runOnce())
{
map<id,financialData__c> salemap=new map<id,financialData__c>();
List<FinancialData__c> listOfFilteredData = new List<FinancialData__c>();
List<Id> listOfFilteredAccount = new List<Id>();
list<financialData__c> saleList = new list<financialData__c>();
for(financialData__c s:trigger.new) {
if(s.Account__c !=NULL && s.Year__C !=NULL && s.Month__c!=NULL)
{
listOfFilteredData.add(s);
listOfFilteredAccount.add(s.Account__c);
}
}
list<financialData__c> sllist = [select id,Year__c,Month__c,Account__c,GL_Sales__c, Land_Comm__c from financialData__c where Account__c in:listOfFilteredAccount Order by CreatedDate Asc];
for(financialData__c ss:sllist){
salemap.put(ss.Account__c,ss);
}
for(FinancialData__c ss:listOfFilteredAccount)
if(ss.Year__c!=null && ss.Month__c!=null && ss.Account__c!=null){
String yr = String.valueof(ss.Year__c-1);
String mnth = String.valueof(ss.Month__c);
integer monthhh;
if(mnth=='jan'){monthhh=1;}
else if(mnth=='feb'){monthhh=2;}
else if(mnth=='mar'){monthhh=3;}
else if(mnth=='apr'){monthhh=4;}
else if(mnth=='may'){monthhh=5;}
else if(mnth=='jun'){monthhh=6;}
else if(mnth=='jul'){monthhh=7;}
else if(mnth=='aug'){monthhh=8;}
else if(mnth=='sep'){monthhh=9;}
else if(mnth=='oct'){monthhh=10;}
else if(mnth=='nov'){monthhh=11;}
else if(mnth=='dec'){monthhh=12;}
String acc = ss.Account__c;
if (salemap.containsKey(acc) && salemap.get(acc)!=NULL && salemap.get(acc).Year__c == yr && salemap.get(acc).Month__c ==mnth && salemap.get(acc).GL_Sales__c!=NULL) {
ss.Previous_year_GL_Sales__c = salemap.get(acc).GL_Sales__c;
} }
}
}
}