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.