Apex CPU Time Limit Exceeded error for DateMethods.Year()apex cpu time limit exceeded error in salesforceAPEX...
Cat is tipping over bed-side lamps during the night
Coworker asking me to not bring cakes due to self control issue. What should I do?
How big is a framed opening for a door relative to the finished door opening width?
Website seeing my Facebook data?
How to not let the Identify spell spoil everything?
Single-row INSERT...SELECT much slower than separate SELECT
How vim overwrites readonly mode?
Boss asked me to sign a resignation paper without a date on it along with my new contract
Can we "borrow" our answers to populate our own websites?
Can a player sacrifice a creature after declaring that creature as blocker while taking lethal damage?
Why do all the books in Game of Thrones library have their covers facing the back of the shelf?
Are the positive and negative planes inner or outer planes in the Great Wheel cosmology model?
What to do with threats of blacklisting?
Can you determine if focus is sharp without diopter adjustment if your sight is imperfect?
Stuck on a Geometry Puzzle
Memory usage: #define vs. static const for uint8_t
What is a good reason for every spaceship to carry a weapon on board?
Is `Object` a function in javascript?
Non-Cancer terminal illness that can affect young (age 10-13) girls?
What makes papers publishable in top-tier journals?
Is there a verb that means to inject with poison?
What species should be used for storage of human minds?
Converting very wide logos to square formats
Do authors have to be politically correct in article-writing?
Apex CPU Time Limit Exceeded error for DateMethods.Year()
apex cpu time limit exceeded error in salesforceAPEX CPU Time limit exceeded error. Any suggestions?solution for apex cpu time limit exceeded exceptionTrigger Error: System.LimitException: Apex CPU time limit exceededNeed help with the error : : Apex CPU time limit exceededSystem.LimitException: Apex CPU time limit exceeded Error on AccountApex CPU time limit exceededCPU Time Limit Exceeded for managed packageApex CPU time limit exceeded for custom CommissionUpdate triggerApex Cpu Limit Exceeded Error in trigger
I am getting Apex CPU time limit exceeded even though the listhistoricalList is small (has 14 Records in it).
String soql = 'SELECT Id, Name, SoM_Business_Location_Status__c, SoM_Shipping_Address__c, SoM_ShippingAddress2__c, SoM_Shipping_CityStateZip__c, ' +
'Primary_Contact_Signing_Authority__r.Name, DBA__c, SoM_County__c, Current_LGU_Account__c, SoM_LARA_Business_ID__c ' +
',(SELECT Id FROM Complaint_Violations__r LIMIT 1) ' +
'FROM Account ';
String whereClause = '';
if (accountIds.size() > 0) {
whereClause += 'WHERE Id IN :accountIds ';
} else {
whereClause += 'WHERE Id!=null ';
}
// Query and populate the list
licensees = Database.query(soql + whereClause + soqlFilter + 'LIMIT 10001 ');
// Get record count and provide warning message if necessary
if (licensees.size() == 10001) {
licensees.remove(licensees.size() - 1);
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Warning,
'Warning! More than 10,000 records found.' +
' All records are not displayed. Please consider using filter' +
' to limit the number of records.'));
}
// Prepare account set to get historical sales list for the licensees
Set<Id> accountSet = new Set<Id> ();
for (Account acc : licensees) {
accountSet.add(acc.Id);
accountPreviousYearSales.put(acc.Id, 0);
accountNextYearSales.put(acc.Id, 0);
}
// Query and populate account sales map
List<Historical_Sales_Records__c> historicalList = [SELECT Id, Account__c, Sales_Amount__c, Date__c, Year__c
FROM Historical_Sales_Records__c
WHERE Account__c = :accountSet];
for (Historical_Sales_Records__c s : historicalList) {
if (s.Date__c != null) {
if (Date.valueOf(s.Date__c).year() == System.today().year() - 1) {
accountPreviousYearSales.put(s.Account__c, accountPreviousYearSales.get(s.Account__c) + s.Sales_Amount__c);
} else if (Date.valueOf(s.Date__c).year() == System.today().year()) {
accountNextYearSales.put(s.Account__c, accountNextYearSales.get(s.Account__c) + s.Sales_Amount__c);
}
}
}
getting Apex CPU Time limit Exceeded at line: if (Date.valueOf(s.Date__c).year() == System.today().year() - 1)
Please see this image:
UPDATED SECTION
Made these changes:
Set<Id> accountSet = new Set<Id> ();
for (Account acc : licensees) {
accountSet.add(acc.Id);
accountPreviousYearSales.put(acc.Id, 0);
accountNextYearSales.put(acc.Id, 0);
}
Integer previousYear = Date.today().year()-1;
Integer currentyear = Date.Today().year();
string previousyearstring = '%'+string.valueof(previousYear)+'%';
string currentyearstring = '%'+string.valueof(currentyear)+'%';
AggregateResult[] data = [
SELECT Account__c AccountId, Year__c year, SUM(Sales_Amount__c) sum
FROM Historical_Sales_Records__c
WHERE Account__c = :accountSet AND (Year__c = :previousyearstring OR Year__c = :currentyearstring) GROUP BY Account__c, Year__c
];
for(AggregateResult item: data) {
String year = (String)item.get('year');
Decimal sum = (Decimal)item.get('sum');
Id accId = (Id)item.get('AccountId');
(currentyearstring == year? accountNextYearSales: accountPreviousYearSales).put(accId, sum);
}
Now, i am not getting this value in VF page:
<apex:repeat value="{!licensees}" var="acc">
<tr>
<td>
<a href="#" onclick="showDetail('{!acc.Id}'),setScrollTo('#details')">{!acc.SoM_LARA_Business_ID__c}</a>
</td>
<td>{!acc.Name}</td>
<td>{!acc.DBA__c}</td>
<td>{!acc.Current_LGU_Account__c}</td>
<td>{!acc.SoM_County__c}</td>
<td>
<apex:outputtext value="{!acc.SoM_Shipping_Address__c} {!acc.SoM_ShippingAddress2__c} {!acc.SoM_Shipping_CityStateZip__c}" escape="false" />
</td>
<td>{!acc.SoM_Business_Location_Status__c}</td>
<td>
<a href="#" onclick="showDetail('{!acc.Id}'),setScrollTo('#violations')">{!if(acc.Complaint_Violations__r.size>0,'Y','N')}</a>
</td>
<td>${!accountNextYearSales[acc.Id]}</td>
<td>${!accountPreviousYearSales[acc.Id]}</td>
</tr>
</apex:repeat>
apex cpulimit
add a comment |
I am getting Apex CPU time limit exceeded even though the listhistoricalList is small (has 14 Records in it).
String soql = 'SELECT Id, Name, SoM_Business_Location_Status__c, SoM_Shipping_Address__c, SoM_ShippingAddress2__c, SoM_Shipping_CityStateZip__c, ' +
'Primary_Contact_Signing_Authority__r.Name, DBA__c, SoM_County__c, Current_LGU_Account__c, SoM_LARA_Business_ID__c ' +
',(SELECT Id FROM Complaint_Violations__r LIMIT 1) ' +
'FROM Account ';
String whereClause = '';
if (accountIds.size() > 0) {
whereClause += 'WHERE Id IN :accountIds ';
} else {
whereClause += 'WHERE Id!=null ';
}
// Query and populate the list
licensees = Database.query(soql + whereClause + soqlFilter + 'LIMIT 10001 ');
// Get record count and provide warning message if necessary
if (licensees.size() == 10001) {
licensees.remove(licensees.size() - 1);
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Warning,
'Warning! More than 10,000 records found.' +
' All records are not displayed. Please consider using filter' +
' to limit the number of records.'));
}
// Prepare account set to get historical sales list for the licensees
Set<Id> accountSet = new Set<Id> ();
for (Account acc : licensees) {
accountSet.add(acc.Id);
accountPreviousYearSales.put(acc.Id, 0);
accountNextYearSales.put(acc.Id, 0);
}
// Query and populate account sales map
List<Historical_Sales_Records__c> historicalList = [SELECT Id, Account__c, Sales_Amount__c, Date__c, Year__c
FROM Historical_Sales_Records__c
WHERE Account__c = :accountSet];
for (Historical_Sales_Records__c s : historicalList) {
if (s.Date__c != null) {
if (Date.valueOf(s.Date__c).year() == System.today().year() - 1) {
accountPreviousYearSales.put(s.Account__c, accountPreviousYearSales.get(s.Account__c) + s.Sales_Amount__c);
} else if (Date.valueOf(s.Date__c).year() == System.today().year()) {
accountNextYearSales.put(s.Account__c, accountNextYearSales.get(s.Account__c) + s.Sales_Amount__c);
}
}
}
getting Apex CPU Time limit Exceeded at line: if (Date.valueOf(s.Date__c).year() == System.today().year() - 1)
Please see this image:
UPDATED SECTION
Made these changes:
Set<Id> accountSet = new Set<Id> ();
for (Account acc : licensees) {
accountSet.add(acc.Id);
accountPreviousYearSales.put(acc.Id, 0);
accountNextYearSales.put(acc.Id, 0);
}
Integer previousYear = Date.today().year()-1;
Integer currentyear = Date.Today().year();
string previousyearstring = '%'+string.valueof(previousYear)+'%';
string currentyearstring = '%'+string.valueof(currentyear)+'%';
AggregateResult[] data = [
SELECT Account__c AccountId, Year__c year, SUM(Sales_Amount__c) sum
FROM Historical_Sales_Records__c
WHERE Account__c = :accountSet AND (Year__c = :previousyearstring OR Year__c = :currentyearstring) GROUP BY Account__c, Year__c
];
for(AggregateResult item: data) {
String year = (String)item.get('year');
Decimal sum = (Decimal)item.get('sum');
Id accId = (Id)item.get('AccountId');
(currentyearstring == year? accountNextYearSales: accountPreviousYearSales).put(accId, sum);
}
Now, i am not getting this value in VF page:
<apex:repeat value="{!licensees}" var="acc">
<tr>
<td>
<a href="#" onclick="showDetail('{!acc.Id}'),setScrollTo('#details')">{!acc.SoM_LARA_Business_ID__c}</a>
</td>
<td>{!acc.Name}</td>
<td>{!acc.DBA__c}</td>
<td>{!acc.Current_LGU_Account__c}</td>
<td>{!acc.SoM_County__c}</td>
<td>
<apex:outputtext value="{!acc.SoM_Shipping_Address__c} {!acc.SoM_ShippingAddress2__c} {!acc.SoM_Shipping_CityStateZip__c}" escape="false" />
</td>
<td>{!acc.SoM_Business_Location_Status__c}</td>
<td>
<a href="#" onclick="showDetail('{!acc.Id}'),setScrollTo('#violations')">{!if(acc.Complaint_Violations__r.size>0,'Y','N')}</a>
</td>
<td>${!accountNextYearSales[acc.Id]}</td>
<td>${!accountPreviousYearSales[acc.Id]}</td>
</tr>
</apex:repeat>
apex cpulimit
add a comment |
I am getting Apex CPU time limit exceeded even though the listhistoricalList is small (has 14 Records in it).
String soql = 'SELECT Id, Name, SoM_Business_Location_Status__c, SoM_Shipping_Address__c, SoM_ShippingAddress2__c, SoM_Shipping_CityStateZip__c, ' +
'Primary_Contact_Signing_Authority__r.Name, DBA__c, SoM_County__c, Current_LGU_Account__c, SoM_LARA_Business_ID__c ' +
',(SELECT Id FROM Complaint_Violations__r LIMIT 1) ' +
'FROM Account ';
String whereClause = '';
if (accountIds.size() > 0) {
whereClause += 'WHERE Id IN :accountIds ';
} else {
whereClause += 'WHERE Id!=null ';
}
// Query and populate the list
licensees = Database.query(soql + whereClause + soqlFilter + 'LIMIT 10001 ');
// Get record count and provide warning message if necessary
if (licensees.size() == 10001) {
licensees.remove(licensees.size() - 1);
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Warning,
'Warning! More than 10,000 records found.' +
' All records are not displayed. Please consider using filter' +
' to limit the number of records.'));
}
// Prepare account set to get historical sales list for the licensees
Set<Id> accountSet = new Set<Id> ();
for (Account acc : licensees) {
accountSet.add(acc.Id);
accountPreviousYearSales.put(acc.Id, 0);
accountNextYearSales.put(acc.Id, 0);
}
// Query and populate account sales map
List<Historical_Sales_Records__c> historicalList = [SELECT Id, Account__c, Sales_Amount__c, Date__c, Year__c
FROM Historical_Sales_Records__c
WHERE Account__c = :accountSet];
for (Historical_Sales_Records__c s : historicalList) {
if (s.Date__c != null) {
if (Date.valueOf(s.Date__c).year() == System.today().year() - 1) {
accountPreviousYearSales.put(s.Account__c, accountPreviousYearSales.get(s.Account__c) + s.Sales_Amount__c);
} else if (Date.valueOf(s.Date__c).year() == System.today().year()) {
accountNextYearSales.put(s.Account__c, accountNextYearSales.get(s.Account__c) + s.Sales_Amount__c);
}
}
}
getting Apex CPU Time limit Exceeded at line: if (Date.valueOf(s.Date__c).year() == System.today().year() - 1)
Please see this image:
UPDATED SECTION
Made these changes:
Set<Id> accountSet = new Set<Id> ();
for (Account acc : licensees) {
accountSet.add(acc.Id);
accountPreviousYearSales.put(acc.Id, 0);
accountNextYearSales.put(acc.Id, 0);
}
Integer previousYear = Date.today().year()-1;
Integer currentyear = Date.Today().year();
string previousyearstring = '%'+string.valueof(previousYear)+'%';
string currentyearstring = '%'+string.valueof(currentyear)+'%';
AggregateResult[] data = [
SELECT Account__c AccountId, Year__c year, SUM(Sales_Amount__c) sum
FROM Historical_Sales_Records__c
WHERE Account__c = :accountSet AND (Year__c = :previousyearstring OR Year__c = :currentyearstring) GROUP BY Account__c, Year__c
];
for(AggregateResult item: data) {
String year = (String)item.get('year');
Decimal sum = (Decimal)item.get('sum');
Id accId = (Id)item.get('AccountId');
(currentyearstring == year? accountNextYearSales: accountPreviousYearSales).put(accId, sum);
}
Now, i am not getting this value in VF page:
<apex:repeat value="{!licensees}" var="acc">
<tr>
<td>
<a href="#" onclick="showDetail('{!acc.Id}'),setScrollTo('#details')">{!acc.SoM_LARA_Business_ID__c}</a>
</td>
<td>{!acc.Name}</td>
<td>{!acc.DBA__c}</td>
<td>{!acc.Current_LGU_Account__c}</td>
<td>{!acc.SoM_County__c}</td>
<td>
<apex:outputtext value="{!acc.SoM_Shipping_Address__c} {!acc.SoM_ShippingAddress2__c} {!acc.SoM_Shipping_CityStateZip__c}" escape="false" />
</td>
<td>{!acc.SoM_Business_Location_Status__c}</td>
<td>
<a href="#" onclick="showDetail('{!acc.Id}'),setScrollTo('#violations')">{!if(acc.Complaint_Violations__r.size>0,'Y','N')}</a>
</td>
<td>${!accountNextYearSales[acc.Id]}</td>
<td>${!accountPreviousYearSales[acc.Id]}</td>
</tr>
</apex:repeat>
apex cpulimit
I am getting Apex CPU time limit exceeded even though the listhistoricalList is small (has 14 Records in it).
String soql = 'SELECT Id, Name, SoM_Business_Location_Status__c, SoM_Shipping_Address__c, SoM_ShippingAddress2__c, SoM_Shipping_CityStateZip__c, ' +
'Primary_Contact_Signing_Authority__r.Name, DBA__c, SoM_County__c, Current_LGU_Account__c, SoM_LARA_Business_ID__c ' +
',(SELECT Id FROM Complaint_Violations__r LIMIT 1) ' +
'FROM Account ';
String whereClause = '';
if (accountIds.size() > 0) {
whereClause += 'WHERE Id IN :accountIds ';
} else {
whereClause += 'WHERE Id!=null ';
}
// Query and populate the list
licensees = Database.query(soql + whereClause + soqlFilter + 'LIMIT 10001 ');
// Get record count and provide warning message if necessary
if (licensees.size() == 10001) {
licensees.remove(licensees.size() - 1);
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Warning,
'Warning! More than 10,000 records found.' +
' All records are not displayed. Please consider using filter' +
' to limit the number of records.'));
}
// Prepare account set to get historical sales list for the licensees
Set<Id> accountSet = new Set<Id> ();
for (Account acc : licensees) {
accountSet.add(acc.Id);
accountPreviousYearSales.put(acc.Id, 0);
accountNextYearSales.put(acc.Id, 0);
}
// Query and populate account sales map
List<Historical_Sales_Records__c> historicalList = [SELECT Id, Account__c, Sales_Amount__c, Date__c, Year__c
FROM Historical_Sales_Records__c
WHERE Account__c = :accountSet];
for (Historical_Sales_Records__c s : historicalList) {
if (s.Date__c != null) {
if (Date.valueOf(s.Date__c).year() == System.today().year() - 1) {
accountPreviousYearSales.put(s.Account__c, accountPreviousYearSales.get(s.Account__c) + s.Sales_Amount__c);
} else if (Date.valueOf(s.Date__c).year() == System.today().year()) {
accountNextYearSales.put(s.Account__c, accountNextYearSales.get(s.Account__c) + s.Sales_Amount__c);
}
}
}
getting Apex CPU Time limit Exceeded at line: if (Date.valueOf(s.Date__c).year() == System.today().year() - 1)
Please see this image:
UPDATED SECTION
Made these changes:
Set<Id> accountSet = new Set<Id> ();
for (Account acc : licensees) {
accountSet.add(acc.Id);
accountPreviousYearSales.put(acc.Id, 0);
accountNextYearSales.put(acc.Id, 0);
}
Integer previousYear = Date.today().year()-1;
Integer currentyear = Date.Today().year();
string previousyearstring = '%'+string.valueof(previousYear)+'%';
string currentyearstring = '%'+string.valueof(currentyear)+'%';
AggregateResult[] data = [
SELECT Account__c AccountId, Year__c year, SUM(Sales_Amount__c) sum
FROM Historical_Sales_Records__c
WHERE Account__c = :accountSet AND (Year__c = :previousyearstring OR Year__c = :currentyearstring) GROUP BY Account__c, Year__c
];
for(AggregateResult item: data) {
String year = (String)item.get('year');
Decimal sum = (Decimal)item.get('sum');
Id accId = (Id)item.get('AccountId');
(currentyearstring == year? accountNextYearSales: accountPreviousYearSales).put(accId, sum);
}
Now, i am not getting this value in VF page:
<apex:repeat value="{!licensees}" var="acc">
<tr>
<td>
<a href="#" onclick="showDetail('{!acc.Id}'),setScrollTo('#details')">{!acc.SoM_LARA_Business_ID__c}</a>
</td>
<td>{!acc.Name}</td>
<td>{!acc.DBA__c}</td>
<td>{!acc.Current_LGU_Account__c}</td>
<td>{!acc.SoM_County__c}</td>
<td>
<apex:outputtext value="{!acc.SoM_Shipping_Address__c} {!acc.SoM_ShippingAddress2__c} {!acc.SoM_Shipping_CityStateZip__c}" escape="false" />
</td>
<td>{!acc.SoM_Business_Location_Status__c}</td>
<td>
<a href="#" onclick="showDetail('{!acc.Id}'),setScrollTo('#violations')">{!if(acc.Complaint_Violations__r.size>0,'Y','N')}</a>
</td>
<td>${!accountNextYearSales[acc.Id]}</td>
<td>${!accountPreviousYearSales[acc.Id]}</td>
</tr>
</apex:repeat>
apex cpulimit
apex cpulimit
edited 6 hours ago
SunnyG
asked 8 hours ago
SunnyGSunnyG
285
285
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
today()
was called 5082 times, meaning there were at least 2541 records in the list. That whole mess could be avoided if you used an aggregate result query:
AggregateResult[] data = [
SELECT Account__c AccountId, CALENDAR_YEAR(Date__c) year, SUM(Sales_Amount__c) sum
FROM Historical_Sales_Records__c
WHERE Account__c = :accountSet AND Date__c >= LAST_YEAR AND Date__c > NEXT_YEAR
GROUP BY Account__c, CALENDAR_YEAR(Date___c)
];
Decimal thisYear = Date.today().year();
for(AggregateResult item: data) {
Decimal year = (Decimal)item.get('year'), sum = (Decimal)item.get('sum');
Id accId = (Id)item.get('AccountId');
(thisYear == year? accountNextYearSales: accountPreviousYearSales).put(accId, sum);
}
This is also a case where moving the filters in to the query drastically reduces the number of rows returned, increasing performance.
Thank you @sfdcfox! I have updated the question because of limited char length here. Please look at the updated section.
– SunnyG
6 hours ago
@SunnyG you need to useLIKE
if you want to use the%
wildcard.
– sfdcfox
6 hours ago
Ohhhhh! I didn't realize i still had % there. I am getting the values now. I will check if the time limit error is gone. Thanks
– SunnyG
6 hours ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "459"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f251636%2fapex-cpu-time-limit-exceeded-error-for-datemethods-year%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
today()
was called 5082 times, meaning there were at least 2541 records in the list. That whole mess could be avoided if you used an aggregate result query:
AggregateResult[] data = [
SELECT Account__c AccountId, CALENDAR_YEAR(Date__c) year, SUM(Sales_Amount__c) sum
FROM Historical_Sales_Records__c
WHERE Account__c = :accountSet AND Date__c >= LAST_YEAR AND Date__c > NEXT_YEAR
GROUP BY Account__c, CALENDAR_YEAR(Date___c)
];
Decimal thisYear = Date.today().year();
for(AggregateResult item: data) {
Decimal year = (Decimal)item.get('year'), sum = (Decimal)item.get('sum');
Id accId = (Id)item.get('AccountId');
(thisYear == year? accountNextYearSales: accountPreviousYearSales).put(accId, sum);
}
This is also a case where moving the filters in to the query drastically reduces the number of rows returned, increasing performance.
Thank you @sfdcfox! I have updated the question because of limited char length here. Please look at the updated section.
– SunnyG
6 hours ago
@SunnyG you need to useLIKE
if you want to use the%
wildcard.
– sfdcfox
6 hours ago
Ohhhhh! I didn't realize i still had % there. I am getting the values now. I will check if the time limit error is gone. Thanks
– SunnyG
6 hours ago
add a comment |
today()
was called 5082 times, meaning there were at least 2541 records in the list. That whole mess could be avoided if you used an aggregate result query:
AggregateResult[] data = [
SELECT Account__c AccountId, CALENDAR_YEAR(Date__c) year, SUM(Sales_Amount__c) sum
FROM Historical_Sales_Records__c
WHERE Account__c = :accountSet AND Date__c >= LAST_YEAR AND Date__c > NEXT_YEAR
GROUP BY Account__c, CALENDAR_YEAR(Date___c)
];
Decimal thisYear = Date.today().year();
for(AggregateResult item: data) {
Decimal year = (Decimal)item.get('year'), sum = (Decimal)item.get('sum');
Id accId = (Id)item.get('AccountId');
(thisYear == year? accountNextYearSales: accountPreviousYearSales).put(accId, sum);
}
This is also a case where moving the filters in to the query drastically reduces the number of rows returned, increasing performance.
Thank you @sfdcfox! I have updated the question because of limited char length here. Please look at the updated section.
– SunnyG
6 hours ago
@SunnyG you need to useLIKE
if you want to use the%
wildcard.
– sfdcfox
6 hours ago
Ohhhhh! I didn't realize i still had % there. I am getting the values now. I will check if the time limit error is gone. Thanks
– SunnyG
6 hours ago
add a comment |
today()
was called 5082 times, meaning there were at least 2541 records in the list. That whole mess could be avoided if you used an aggregate result query:
AggregateResult[] data = [
SELECT Account__c AccountId, CALENDAR_YEAR(Date__c) year, SUM(Sales_Amount__c) sum
FROM Historical_Sales_Records__c
WHERE Account__c = :accountSet AND Date__c >= LAST_YEAR AND Date__c > NEXT_YEAR
GROUP BY Account__c, CALENDAR_YEAR(Date___c)
];
Decimal thisYear = Date.today().year();
for(AggregateResult item: data) {
Decimal year = (Decimal)item.get('year'), sum = (Decimal)item.get('sum');
Id accId = (Id)item.get('AccountId');
(thisYear == year? accountNextYearSales: accountPreviousYearSales).put(accId, sum);
}
This is also a case where moving the filters in to the query drastically reduces the number of rows returned, increasing performance.
today()
was called 5082 times, meaning there were at least 2541 records in the list. That whole mess could be avoided if you used an aggregate result query:
AggregateResult[] data = [
SELECT Account__c AccountId, CALENDAR_YEAR(Date__c) year, SUM(Sales_Amount__c) sum
FROM Historical_Sales_Records__c
WHERE Account__c = :accountSet AND Date__c >= LAST_YEAR AND Date__c > NEXT_YEAR
GROUP BY Account__c, CALENDAR_YEAR(Date___c)
];
Decimal thisYear = Date.today().year();
for(AggregateResult item: data) {
Decimal year = (Decimal)item.get('year'), sum = (Decimal)item.get('sum');
Id accId = (Id)item.get('AccountId');
(thisYear == year? accountNextYearSales: accountPreviousYearSales).put(accId, sum);
}
This is also a case where moving the filters in to the query drastically reduces the number of rows returned, increasing performance.
answered 7 hours ago
sfdcfoxsfdcfox
256k11201442
256k11201442
Thank you @sfdcfox! I have updated the question because of limited char length here. Please look at the updated section.
– SunnyG
6 hours ago
@SunnyG you need to useLIKE
if you want to use the%
wildcard.
– sfdcfox
6 hours ago
Ohhhhh! I didn't realize i still had % there. I am getting the values now. I will check if the time limit error is gone. Thanks
– SunnyG
6 hours ago
add a comment |
Thank you @sfdcfox! I have updated the question because of limited char length here. Please look at the updated section.
– SunnyG
6 hours ago
@SunnyG you need to useLIKE
if you want to use the%
wildcard.
– sfdcfox
6 hours ago
Ohhhhh! I didn't realize i still had % there. I am getting the values now. I will check if the time limit error is gone. Thanks
– SunnyG
6 hours ago
Thank you @sfdcfox! I have updated the question because of limited char length here. Please look at the updated section.
– SunnyG
6 hours ago
Thank you @sfdcfox! I have updated the question because of limited char length here. Please look at the updated section.
– SunnyG
6 hours ago
@SunnyG you need to use
LIKE
if you want to use the %
wildcard.– sfdcfox
6 hours ago
@SunnyG you need to use
LIKE
if you want to use the %
wildcard.– sfdcfox
6 hours ago
Ohhhhh! I didn't realize i still had % there. I am getting the values now. I will check if the time limit error is gone. Thanks
– SunnyG
6 hours ago
Ohhhhh! I didn't realize i still had % there. I am getting the values now. I will check if the time limit error is gone. Thanks
– SunnyG
6 hours ago
add a comment |
Thanks for contributing an answer to Salesforce Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f251636%2fapex-cpu-time-limit-exceeded-error-for-datemethods-year%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown