You need to sign in to do that
Don't have an account?
Mike @ 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?
I've done something like this many such times. Try using a normal while loop:
All Answers
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?
Maybe this would help
Interesting, I'll give it a try. Thanks
I've done something like this many such times. Try using a normal while loop:
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:
I actually did some debugging before, so if the picklist value is --None-- the underlying value is null.
Ok, then one of the above logic solutions should hopefully solve your problem. Let us know if it doesn't.
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 .
always run down a list if you are going to remove items:
for ( Integer i = objects.size - 1; i > index; --i )
{
objects.remove( i );
}