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;
QS_Product_1__c
filters on just the first item ofTrigger.new
. You will probably want to loop overTrigger.new
first and get all of the opportunity ids from them, so you can query for all of them.