2

My query for QS_Product_1__c filters on just the first item of Trigger.new. I need a loop to loop over Trigger.new first and get all of the opportunity ids from them, so I can query for all of them. Does anyone know how to do that I'm new to developing and I've tried a few things but none of them work.

trigger PopulateQuoteNSFields on Quote (after insert) {
 /** get the set of Quote object ids that caused the trigger
  * if triggered from UI then there will only be one item in the set
  * if triggered from a batch upload there could be multiple items in the set**/

 Set <Id> Ids = new Set<Id>();


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] );
    System.debug('lineitem2: ' + NSproductlist[1] );
    System.debug('lineitem3: ' + NSproductlist[2] );

    integer index = 0;


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= quoteTemp[0].Id;
    listQS2.add(productQ2); 

}
insert listQS2;
}

Here is what I've tried

for (QS_Product_1__c productQ1 : Trigger.new)
      {
      Ids.add(productQ1.Id);
      }
2
  • are you looking OpportunityId for quote object ? – Himanshu Dec 29 '15 at 10:13
  • Yes I want to bulkify the trigger so that If I want to make changes to multiple quotes they are all assigned to the right opportunity not all opportunities or one opportunity. – Greg Dec 29 '15 at 10:15
1

you can query all the opportunity in following way:

set<Id> oppIds = new set<Id>();
for(Quote q : Trigger.new)
{
   oppIds.add(q.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 in  :oppIds] );
2
  • It works fine and there is no problems with the code. I just have one question for the last part of the trigger. where is says productQ2.Quote_QS__c= quoteTemp[0].Id; How would I get the right Quote Id populated. – Greg Dec 29 '15 at 10:36
  • you need to first explain what you are actually looking for. you querying the data on QS_Product_1__c and you are creating QS_Product_2__c object record out of that? then you use only single quote record. please update this question with your object model from schema builder and what you want to achieve. – Himanshu Dec 29 '15 at 11:11
1

You can directly use the Trigger.newMap.keyset() for getting all the ids.

2
  • Thanks. How would you then assign it to the opportunity that is connected to the Quote. ? – Greg Dec 29 '15 at 10:13
  • if you want to collect all the opportunity ids that are binded with the Quote then you can use the above code shared by @Himanshu – SFDC Learner Dec 29 '15 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.