0

I have made a trigger that populates one object from another, it only works for one item now. I'm stuck because I have two lists one populated and one not populated and I want to populate the second one with the first one using for loop. Here is what the trigger looks like. The for loop at the end is where i want to populate the listQS2 from the values from NSproductlist

/* if triggered from a batch upload there could be multiple items in the set**/

List<Quote> quoteTemp = Trigger.new;

System.debug('number of items in ID set ' + quoteTemp.size());
System.debug(quoteTemp[0].OpportunityId);


List<QS_Product_1__c> NSproductlist = new List<QS_Product_1__c>([SELECT ID,
                                                                        Name,  
                                                                        Price__c, 
                                                                        Quantity__c, 
                                                                        Sales_Price__c, 
                                                                        Opportunity_QS__c 
                                                                   From QS_Product_1__c  
                                                                  WHERE Opportunity_QS__C = :quoteTemp[0].OpportunityId] );
List<QS_Product_2__c> listQS2 = new List<QS_Product_2__c>();

System.debug('David' + NSproductlist.size());
System.debug('lineitem1: ' + NSproductlist[0] );


for(QS_Product_1__c temp : NSproductlist){

    QS_Product_2__c productQ2 = new QS_Product_2__c();
    productQ2.Name = NSproductlist[0].Name;
    productQ2.Price__c = NSproductlist[0].Price__c;
    productQ2.Quote_QS__c=quoteTemp[0].id;     
}

Merry christmas!!

Here is what I'm trying out now :

for(QS_Product_1__c temp : NSproductlist){
        listQS2[0].Name = temp.Name;
        listQS2[0].price__c = temp.price__c;
        listQS2[0].Quote_QS__c = temp.id;
      //  QS_Product_2__c productQ2 = new QS_Product_2__c();
      //  productQ2.Name = NSproductlist[index].Name;
      //  productQ2.Price__c = NSproductlist[index].Price__c;
      //  productQ2.Quote_QS__c=quoteTemp[index].id; 
      System.debug('index is :' + index);    
        //index ++;
    }
    insert listQS2; 
2
  • Your query for QS_Product_1__c filters on just the first item of Trigger.new. You will probably want to loop over Trigger.new first and get all of the opportunity ids from them, so you can query for all of them. Commented Dec 24, 2015 at 10:17
  • Yes you're right, but i want to concentrate on the bottom part of the code so i can populate the list with another already populated
    – user26057
    Commented Dec 24, 2015 at 10:21

1 Answer 1

1

your code is not completed but guessing This will solve your issue.

for(QS_Product_1__c temp : NSproductlist){

    QS_Product_2__c productQ2 = new QS_Product_2__c();
    productQ2.Name = temp.Name;
    productQ2.Price__c = temp.Price__c;
    productQ2.Quote_QS__c= temp.id;    
    //add  productQ2  into a list for other operation
}

Edit based on above code

List<QS_Product_2__c> listQS2 = new List<QS_Product_2__c>();

for(QS_Product_1__c temp : NSproductlist){

    QS_Product_2__c productQ2 = new QS_Product_2__c();
    productQ2.Name = temp.Name;
    productQ2.Price__c = temp.Price__c;
    productQ2.Quote_QS__c= temp.id;
    listQS2.add(productQ2); 
}
insert listQS2;
6
  • We're trying to avoid inserting individual records inside of the for loop. So we need to build an array of records and do a single insert outside of the loop
    – user26057
    Commented Dec 24, 2015 at 10:27
  • Yes @Greg correct.
    – Ratan Paul
    Commented Dec 24, 2015 at 10:27
  • I've made an edit with what I'm trying out now.
    – user26057
    Commented Dec 24, 2015 at 10:39
  • @Greg one question if NSproductlist contains 2 records then how many productQ2 record should create?
    – Ratan Paul
    Commented Dec 24, 2015 at 10:44
  • The same amount of records as in NS Product 2. and then stop when there is no more records
    – user26057
    Commented Dec 24, 2015 at 10:45

You must log in to answer this question.