Hot answers tagged

14 votes
Accepted

Apex - Getting list of dates in month - CPU timeout

The addDays method does not modify the value but instead returns the modified date. So you need: currentDay = currentDay.addDays(1); Your current code hits the CPU limit because currentDay never ...
Keith C's user avatar
  • 136k
11 votes
Accepted

Why does my Lightning Component go into an infinite loop and fail to save?

The reason for the cause of the recursion is your apex method and the lightning component helper method have same name "save" I would change the apex method to different name than save save : ...
Mohith Shrivastava's user avatar
10 votes

Help for fixing "expecting a semi-colon, found ')'" Driving me bonkers!

Replace = with :. for(Payment_received__c record : [SELECT ... FROM Payment_received__c] See Loops.
Adrian Larson's user avatar
  • 150k
10 votes
Accepted

Traverse through List<List <Opportunity>> (multi-dimension)

You're close to the solution. The thing that you're getting caught on is that the outer loop isn't iterating over a single Opportunity, but rather a List<Opportunity> // Outer loop on a List&...
Derek F's user avatar
  • 60.9k
8 votes
Accepted

How To Group Records By Value In A Specific Field?

You can use a List as a Value for a Map. Map<Integer, ABC__c[]> codeToABC = new Map<Integer, ABC__c[]>(); for(ABC__c record: abcRecords) { if(codeToABC.containsKey(record.Code__c)) { ...
sfdcfox's user avatar
  • 487k
8 votes
Accepted

Loop through ApexPages.CurrentPage().getParameters()

try like this.. ApexPages.CurrentPage().getParameters() return a map . you have to use like below code. for(String param: ApexPages.CurrentPage().getParameters().keyset()) { System.debug('===key==...
Ratan Paul's user avatar
  • 22.6k
8 votes
Accepted

For loop only loops 5 times

You are removing items while iterating over them. When you get to 5 you have removed 5 items and this the size is only 5. When you get to six it breaks. It is not advisable in general to remove ...
Eric's user avatar
  • 54.1k
8 votes
Accepted

Can you iterate over object properties in LWC?

No, you cannot. However, it is pretty easy to do so with a getter: get objectProperties() { return Object.entries(this.recordObject).map(([key,value])=>({ key, value })); } ... for:each={...
sfdcfox's user avatar
  • 487k
7 votes

Many unnested loops cause 'Apex CPU time limit exceeded'

Your loop: for(Integer i = 0; i < bulkDataValue; i+2){ never terminates hence you hit the CPU limit. You probably meant something like: for(Integer i = 0; i < bulkDataValue; i += 2){
Keith C's user avatar
  • 136k
7 votes
Accepted

For loop within for loop

You should never do DML statements (such as insert) inside loops, this will easily get you off limits. Create a List outside the loop, iterate over your trigger in the first loop, create new records ...
Mauricio Oliveira's user avatar
7 votes
Accepted

For Loop within For Loop.How to avoid such situation?

There's no problem with this for loop configuration because it doesn't cause multiplicative execution. The reason you may have heard about avoiding nested for loops is that some people try a naive ...
sfdcfox's user avatar
  • 487k
7 votes
Accepted

Nested for loop comparing two different lists and update

Using a Map to collect the values you want to update is one useful technique: existingRenewalCardsRequests = new Map<Id, Card_Request__c>(); ... ...
Keith C's user avatar
  • 136k
7 votes
Accepted

Concat Loop Results to determine if contains value

If you need to append values to a variable from within a loop, you'll need to remember to include the variable itself during concatenation. Otherwise, you'll simply be "re-setting" the variable after ...
Mark G's user avatar
  • 1,545
6 votes
Accepted

Execution of AfterInsert caused by: System.StringException: Invalid id: () - where have I gone wrong?

When you perform a comparison between a String and an Id (e.g. ==, !=, or <>), then the other argument is "promoted" to an Id value. All Id values must be 15 or 18 characters long, and must contain ...
sfdcfox's user avatar
  • 487k
6 votes
Accepted

Declaring variables inside loop in apex

The differences: Scope: if you define name outside the loop, the name viable's scope will be accessible outside the loop. This is the major difference. Heap size: defining name inside the loop will ...
Lance Shi's user avatar
  • 13k
6 votes

Loop Support in AMPscript

My hacky stupid workaround for While loops in AMPscript: %%[ FOR @i=1 to 2 DO /*Execute your code here*/ IF NOT @condition THEN @i = Subtract(@i, 1) ENDIF NEXT @i ]%% ...
ncv's user avatar
  • 418
6 votes
Accepted

Test Class: Unable to Cover For Loop

You trigger is fired whenever you update an opportunity and it queries all Contracts linked to these opportunities. What you do in your test class is: Insert opportunities Update opportunities(...
Novarg's user avatar
  • 5,256
6 votes

Counting each record's Nth degree of mutual or directional edges in a (many to many) graphical network of < 50,000 rows/nodes

I created a similar data structure. It's basically just missing the lookup from friend to person. Also for the sake of simplicity, I'm using the name as an identifier, but it would be the same ...
martin's user avatar
  • 12.6k
6 votes
Accepted

Loop through comma separated values in Visual Flow

Sure you can. Roughly the steps would go like this. Create a screen element with a long text area input field. Link it to a decision element to see if the field is empty. If it's not (shouldn't be ...
Kasper's user avatar
  • 2,892
6 votes
Accepted

Invalid loop variable type expected

Map<String, List<Account>>.values() has a return type of List<List<Account>>. You'll have to do an outer loop to "unroll" this data structure, if you want to iterate over all ...
David Reed's user avatar
  • 92.6k
6 votes
Accepted

Why is my flag not working on maximum trigger depth exceeded

You're seeing recursion because your recursion guard is in the wrong place. When this gets executed: AankoopDetailTriggerHandler.isBeforeInsert(trigger.new); if(...
David Reed's user avatar
  • 92.6k
5 votes

How To Group Records By Value In A Specific Field?

This is a pretty common pattern. So common it is worthwhile (and pretty straightforward) to write a utility to do so. Here is an example signature: public class GroupBy { public static Map<...
Adrian Larson's user avatar
  • 150k
5 votes
Accepted

Looping Through A Map

So, this snippet should do it. (I am being more verbose than I need to, for clarity's sake) List<User> usersWithDupes = <your list here> Set<User> usersUnique = new Set<User>(...
Sebastian Kessel's user avatar
5 votes
Accepted

Compile Error: Loop must iterate over a collection type: Map<Id,List<Event>>

Since you have declared Map<Id, List<Event>> events = new Map<Id, List<Event>>(); So, to loop through the List of events you need to use events.values() for (List<Event>...
Santanu Boral's user avatar
5 votes
Accepted

Put common values of two sets within one of them without using loop

There is a standard Set method, called retainAll. That method remove all elements in original set, except from set in parameter (save in current set intersection of the two sets) and returns true if ...
kurunve's user avatar
  • 4,289
5 votes
Accepted

The Looping through a Static Resource List is Not Obeying Conditions

This is always true because it is an assignment whose resulting value is true: if(ca.Incorrect__c = true){ This: if(ca.Incorrect__c){ or this: if(ca.Incorrect__c == true){ should work.
Keith C's user avatar
  • 136k
5 votes
Accepted

How to move my SOQL query outside for loop?

Bad What we mean when we say do not have a query in a for loop is this: for (Object o : objects) { list<Object2> = [select Id from Object2where RelatedId__c = :o.Id]; } This is bad ...
gNerb's user avatar
  • 9,471
5 votes

Converting list into Integer

Given that you are storing a list of numbers in a delimited string field, "24; 35; 67", you have exactly two options. One is iteration (a loop); the other is recursion. You do not have the option of ...
David Reed's user avatar
  • 92.6k
5 votes

Expressions in for:each index value in LWC?

Solution 1 - Use plain old html If you want to render a list in numerical order, starting at any number of your choice, you could use <ol> (ordered list) instead of <ul> (unordered list). ...
akiradev's user avatar
  • 851
5 votes
Accepted

LWC JavaScript: Can't Add a Property to Objects While Looping through an Array of Those Objects

Presuming that this came from a server call, you need to copy the objects entirely: let tempFieldWrapperArray = this.fieldSetWrapper.fieldWrappers.map(element => ({...element})); Or: let ...
sfdcfox's user avatar
  • 487k

Only top scored, non community-wiki answers of a minimum length are eligible