function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mike @ BlackTabMike @ BlackTab 

Iterating Through A List And Removing Items

Hello,

 

I have some code that iterates through a list of Project_Role__c records and if the role__c field is null, remove it from the list.

 

Here is the code I have so far:

 

for(Integer j = 0; j < roles.size(); j++){
   if(roles.get(j).role__c == null){
        roles.remove(j);
   }
}

 For some reason the logic isn't working. 

 

*Note: Role__c is a picklist, should I be checking to see if Role__c == '' instead?

Best Answer chosen by Admin (Salesforce Developers) 
Damien_Damien_

I've done something like this many such times.  Try using a normal while loop:

 

Integer j = 0;
while (j < roles.size())
{
  if(roles.get(j).role__c == null)
  {
    roles.remove(j);
  }else
  {
    j++;
  }
}

 

All Answers

bob_buzzardbob_buzzard

You can't store blank values in the database, rather the platform stores a null, so I wouldn't expect that to be the issue.  Are you receiving an error, or are there elements remaining in the list with role__c equal to null?

SeAlVaSeAlVa

Maybe this would help

for(Integer j = 0; j < roles.size(); j++){
   if(roles.get(j).role__c == null){
        roles.remove(j);
        j--;
   }
}

 

Mike @ BlackTabMike @ BlackTab

Interesting, I'll give it a try. Thanks

Damien_Damien_

I've done something like this many such times.  Try using a normal while loop:

 

Integer j = 0;
while (j < roles.size())
{
  if(roles.get(j).role__c == null)
  {
    roles.remove(j);
  }else
  {
    j++;
  }
}

 

This was selected as the best answer
Damien_Damien_

My last reply would fix your logic error... but I know sometimes picklists have a sort of default value.  Could the value you are really be looking to compare be '-None-' or something similar instead?

 

So the comparison would be:

 

if(roles.get(j).role__c == null || roles.get(j).role__c == '-None-')//Or whatever it is your value might be
Mike @ BlackTabMike @ BlackTab

I actually did some debugging before, so if the picklist value is --None-- the underlying value is null.

Damien_Damien_

Ok, then one of the above logic solutions should hopefully solve your problem.  Let us know if it doesn't.

Adil_SFDCAdil_SFDC

Here is my query

 

orderHistListRec33 = [Select Name,Account__c,Order_Status__c, Case_Number__c, Product_Description__r.Is_Package__c, Product_Description__r.Name,CreatedDate
From Order_History__c order by createdDate dsc ];

 

I get 

 

 

List is 

 

OrderHistoryNumber (Name): 123    Product_Description__r.Name:Branding    CreatdeDate : 6/25/13

OrderHistoryNumber(Name) : 124    Product_Description__r.Name:CHAT    CreatdeDate : 6/25/13

OrderHistoryNumber (Name): 125    Product_Description__r.Name:Branding   CreatdeDate : 6/22/13

OrderHistoryNumber(Name) : 126     Product_Description__r.Name:CHAT    CreatdeDate : 6/22/13

 

I want to eleiminate duplicate Product_Description__r.Name and get most recent i.e. 

 

 

OrderHistoryNumber (Name): 123    Product_Description__r.Name:Branding    CreatdeDate : 6/25/13

OrderHistoryNumber(Name) : 124    Product_Description__r.Name:CHAT    CreatdeDate : 6/25/13

 

how do i modify my query .

dattaraj1.3941931834289702E12dattaraj1.3941931834289702E12
Thanks Damien . You have saved lot of my time.
Jur van Oerle 2Jur van Oerle 2

always run down a list if you are going to remove items: 

for ( Integer i = objects.size - 1; i > index; --i )

{

    objects.remove( i );

}