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
rashrash 

How to fetch a record using for loop

Here is the sample code ;

#  P will hold the Price code at sequence number i

#   len is a Integer variable which has the value 7;
#  m1 is the Model number wanted and is present in Price code sequenced 3

for(Integer i=1;i<=len;i++)
{
P = [Select Price_Code__c FROM Cpt_Price_Code__c WHERE Cust_Price_Type__c = :'MONP' and Sequence_Number__c  = : i].Price_Code__c ;
Price=[select Model_Number__c, New_Price__c from Std_Price_list__c where Model_Number__c  like: m1 and Price_Code__c like : P];
break;}

First fetch the price code of the sequence number" i "( 1,2,3,4,5,6,7) then go to Object STD price list and search for the model Number in that respective price code

if found then get the New Price associated with that model Number and break;

If Not Found continue;


PROBLEM: it is doing the process only once , and coming back with no values as the result is in 3., no iteration is happening

P.S..if i give direct values as i=3   
for(Integer i=3;i<=3;i++) then the code is working perfectly .as the result is in 3.

Pls Help how to go to every list and search and fetch the value.

TCAdminTCAdmin
Hello rash,

I am not completely familiar with Apex yet but I believe you are looking for something like:
Code:
for(Cpt_Price_Code__c p: [Select Price_Code__c FROM Cpt_Price_Code__c WHERE Cust_Price_Type__c = :'MONP' and Sequence_Number__c  = : i].Price_Code__c)

 The code in the Apex guide shows:
Code:
for (variable : [soql_query]) {
    code_block
}

I hope this helps.



Message Edited by TCAdmin on 06-12-2008 11:47 AM