1

I have been coming to this site with apex questions for a while, and appreciate the people who try and post answers here and help people!

I am trying to make a trigger on the Opportunity object that will create an Order, and use information from the child objects Quote and Product on that opportunity.

The functionality to create the order is there, but I am attempting to query any "Quote" with the Id of the opportunity in the trigger, and it is not bringing anything back to attempt to use as my order information.

I am just looking for a push in the right direction. Here is my code:

List<Order> ordertoinsert = new List<Order>();

for (Opportunity opp : Trigger.new) {
   Opportunity oldOpp = Trigger.oldMap.get(opp.Id);


    // Create Order record only when the Stage is Updated to "PO in Hand"


    if (opp.StageName <> oldOpp.stagename && opp.Stagename == 'PO in Hand') {

       List<Quote> existingQuote = [SELECT opportunityid, name from Quote WHERE opportunityid =:opp.ID LIMIT 1];

       if (existingQuote.isEmpty()) {
       } else {
       Quote infoQuote;
       infoQuote = existingQuote[0];
       system.debug(infoQuote.Id);
       }

        Order ord = new Order();
        ord.Name = opp.Name;
        ord.Opportunity__c   = opp.id;
        ord.Accountid= opp.AccountID;
        ord.Status = 'Draft';
        ord.Po_Number__c = '55555555';
        ord.EffectiveDate = Date.today();
        ord.Purchaser_Name__c = 'test';
        ord.Purchaser_Phone_Number__c = '412-555-5555';
        ord.Ship_date__c = Date.today();
        ordertoinsert.add(ord); // For Bulk processing of the Records.
    } //end if
} // End of For

// Inserting the New Order Records if these records exist
if ( !ordertoinsert.isEmpty()) {
    insert ordertoinsert;
} else {

}

Sorry to ask such a beginner question, but I just can't seem to search the right keywords that show me how to accomplish this. All I was trying to do was write the ID of the quote attached to the product to the system.debug, but could never get it to return anything. I need to be able to have the Quote and Product info of the opportunity available to me.

1 Answer 1

1

To state the obvious, you'll want to make sure that there is a quote for the opportunity involved. I don't see any reason why your code shouldn't get you a quote, provided your trigger is acting on an opportunity with a quote attached to it.

But really you should be querying for the Quote before you iterate over Trigger.new and collecting quotes into a map. Something like this:

Map<Id,List<Quote>> oppQuoteMap = new Map<Id,List<Quote>>();
for (List<quote> daQuote : [select id, opportunityid, name from quote where opportunityid in Trigger.new){
  if (!oppQuoteMap.keyset().contains(daQuote.opportunityid){
    oppQuoteMap.put(daQuote.id, new List<Quote>());
  }
    oppQuoteMap.get(daQuote.opportunityid).add(daQuote);
}

You can then use oppQuoteMap inside of your for loop to get any quotes related to your opportunity.

Just rememberif you query or call DML inside of a loop in Apex, you are risking governor limit exceptions. Hope you find your problem.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .