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
Bernie XBernie X 

Dynamic creation of custom object records...Is this a hack?

Here's a class method I've wriiten to dynamically create new custom records from two independet SOQL queries.  The code works without any hitches, but I can't understand why only r.Part__c=whatlist[0].id works inside the inner for loop.  No matter the size of whatlist, r.Part__c=whatlist[i].id generates an out of bounds error.  Only r.Part__c=whatlist[0].id allows for the intended iteration.

public list <system__c> GetQuoteData(string s){
    mylist =[select quoteid,quantity,discount,PricebookEntry.Product2Id,PricebookEntry.Productcode from quotelineitem where quoteid=:s
             and (/*not*/PricebookEntry.Productcode like '700-%')];
        for (integer v=0;v<mylist.size();v++){
          
     
            system__c r = new system__c();
            r.qty__c=mylist[v].quantity;
            r.discount__c=mylist[v].discount;
       
       
        
        whatlist=[select id,Product2id_map__c from part__c where Product2id_map__c=:mylist[v].PricebookEntry.Product2Id];
        for(integer i=0;i<whatlist.size();i++){ //system__c f = new system__c();
            r.Part__c=whatlist[0].id;
            system.debug('||||||||||||'+mylist.size());
            system.debug('||||||||||||'+whatlist[0]);
         
        }
            dumblist.add(r);
            system.debug('####'+dumblist[v].qty__c+'####'+dumblist[v].part__c+'####'+dumblist[v].discount__c);
           
        }
GlynAGlynA
Bernie,

You should use the for loop syntax for iterating over a collection:

for ( quotelineitem qli : mylist )
{
    ...
    r.qty__c = qli.quantity;
    ...
    for ( part__c thePart : whatlist )
    {
        r.Part__c = thePart.Id;
        ...


Also, I'm guessing that "whatlist" is declared in a part of the code you did not include in your post.  Is it declared as:

List<Part__c> whatlist;

???

You can get rid of the lists by putting the queries in the for loop syntax as well:

for ( quotelineitem qli :
    [   select  quoteid,quantity,discount,PricebookEntry.Product2Id,PricebookEntry.Productcode
        from    quotelineitem
        where   quoteid=:s and (/*not*/PricebookEntry.Productcode like '700-%')
    ]
    )
{
    ...
}

-Glyn
btarver1.389389794125792E12btarver1.389389794125792E12
Thanks Glyn for your reply.  I was reluctant to use the for(obj:list) syntax because of the iterator condition in the query of whatlist.

whatlist=[select id,Product2id_map__c from part__c where Product2id_map__c=:mylist[v].PricebookEntry.Product2Id];

and your're correct about the list declarations.  I have those outside the method, but within the class in an attempt to keep code organized.
GlynAGlynA
Bernie,

Did this solve your problem?  Or do you need more help?

If it helped solved the problem, please mark it as the "answer".  This will mark the thread as "solved".  Thanks!

-Glyn
Bernie XBernie X
Glyn,

Thanks for your reply; I took a few days off from development.  My original question was about the following syntax: r.Part__c=whatlist[0].id, where my thought was that I could iterrate through the rows, using [ ] as an index.  What I've learned since then is the circumstance of the L-value of a SOQL query that returns one record.  Hence, r.Part__c=whatlist[0].id refernces the id value of the first at only record from whatlst.

Here's my source on the L-values:  http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=langCon_apex_SOQL_single_row.htm|StartTopic=Content%2FlangCon_apex_SOQL_single_row.htm|SkinName=webhelp

Regards,

Bernard