Apex CPU Time Limit Exceeded error for DateMethods.Year()












1















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:



enter image description here



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>


enter image description here










share|improve this question





























    1















    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:



    enter image description here



    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>


    enter image description here










    share|improve this question



























      1












      1








      1








      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:



      enter image description here



      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>


      enter image description here










      share|improve this question
















      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:



      enter image description here



      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>


      enter image description here







      apex cpulimit






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 3 hours ago







      SunnyG

















      asked 5 hours ago









      SunnyGSunnyG

      265




      265






















          1 Answer
          1






          active

          oldest

          votes


















          3














          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.






          share|improve this answer
























          • Thank you @sfdcfox! I have updated the question because of limited char length here. Please look at the updated section.

            – SunnyG
            3 hours ago











          • @SunnyG you need to use LIKE if you want to use the % wildcard.

            – sfdcfox
            2 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
            2 hours ago











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          3














          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.






          share|improve this answer
























          • Thank you @sfdcfox! I have updated the question because of limited char length here. Please look at the updated section.

            – SunnyG
            3 hours ago











          • @SunnyG you need to use LIKE if you want to use the % wildcard.

            – sfdcfox
            2 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
            2 hours ago
















          3














          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.






          share|improve this answer
























          • Thank you @sfdcfox! I have updated the question because of limited char length here. Please look at the updated section.

            – SunnyG
            3 hours ago











          • @SunnyG you need to use LIKE if you want to use the % wildcard.

            – sfdcfox
            2 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
            2 hours ago














          3












          3








          3







          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 4 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
            3 hours ago











          • @SunnyG you need to use LIKE if you want to use the % wildcard.

            – sfdcfox
            2 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
            2 hours ago



















          • Thank you @sfdcfox! I have updated the question because of limited char length here. Please look at the updated section.

            – SunnyG
            3 hours ago











          • @SunnyG you need to use LIKE if you want to use the % wildcard.

            – sfdcfox
            2 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
            2 hours ago

















          Thank you @sfdcfox! I have updated the question because of limited char length here. Please look at the updated section.

          – SunnyG
          3 hours ago





          Thank you @sfdcfox! I have updated the question because of limited char length here. Please look at the updated section.

          – SunnyG
          3 hours ago













          @SunnyG you need to use LIKE if you want to use the % wildcard.

          – sfdcfox
          2 hours ago





          @SunnyG you need to use LIKE if you want to use the % wildcard.

          – sfdcfox
          2 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
          2 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
          2 hours ago


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          How to label and detect the document text images

          Vallis Paradisi

          Tabula Rosettana