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
Steve_WSteve_W 

i have an Sobject List now how do i compare one of the field values to a string?

I have a list in my trigger 

 

List <Trial__c> Tr = [Select id, Quote__c, Trial_Sub_Status__c from Trial__c where Quote__c in (Select QuoteId from QuoteLineItem )];

 

I want to run some code if the string 'Approved' exists in any of the records returned in my List definition

 

if(Tr[0].Trial_Sub_Status__c =='Approved') works for a single value but how can i iterate through each of the values for this specific field? I only anticipate 1-3 records at the most.

 

been searching for a while now and can't seem to figure out the right words to search with.

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
@anilbathula@@anilbathula@
Hi Steve_W

you can iterate the list with a for loop:

List <Trial__c> Tr = [Select id, Quote__c, Trial_Sub_Status__c from Trial__c where Quote__c in (Select QuoteId from QuoteLineItem )];

for(Trial__c T : tr){
if(T.Trial_Sub_Status__c =='Approved'){
// Do what u want..
}

}

All Answers

@anilbathula@@anilbathula@
Hi Steve_W

you can iterate the list with a for loop:

List <Trial__c> Tr = [Select id, Quote__c, Trial_Sub_Status__c from Trial__c where Quote__c in (Select QuoteId from QuoteLineItem )];

for(Trial__c T : tr){
if(T.Trial_Sub_Status__c =='Approved'){
// Do what u want..
}

}
This was selected as the best answer
Steve_WSteve_W

Thanks Anil,

 

This is exactly what i needed. I wasn't able to crack if by myself so i ended up revisiting my requirements and realized that i only need to identify at least one Trial__c record with an Approved status so i updated my SOQL query with that additional filter effectively reducing my List Tr and removing the need to iterate through it on my if condition.

 

List <Trial__c> Tr = [Select id, Quote__c, Trial_Sub_Status__c from Trial__c where Quote__c in (Select QuoteId from QuoteLineItem) and Trial_Sub_Status__c = 'Approved' ];	
	
	if(Tr.size()>0){
	//System.debug('Sub Status '+Tr[0].Trial_Sub_Status__c);
         do stuff here.