You need to sign in to do that
Don't have an account?

Trigger on Opportunity Line Item to insert Quote and Quote Line Item
Hi Friends,
I have a requirement where I need to create a Quote when a Opportunity Product is added.
When creating a Quote I need to copy contact details based on Opportunity Contact Role where Contact is Primary.
I am getting Contact Name but, got Stuck in getting a Contact Email, Contact MailingAddress and Contact Other Address. It is not getting populated in Quote.
Kindly review my code and let me know how can I get contact values.
Thanks in Advance.
I have a requirement where I need to create a Quote when a Opportunity Product is added.
When creating a Quote I need to copy contact details based on Opportunity Contact Role where Contact is Primary.
I am getting Contact Name but, got Stuck in getting a Contact Email, Contact MailingAddress and Contact Other Address. It is not getting populated in Quote.
/** Trigger on Opportunity Line Item to insert Quote and Quotelineitem **/ trigger quoteCreation on OpportunityLineItem (after insert) { if(Trigger.isAfter && Trigger.isInsert){ List<Quote> quoteList = new List<Quote>(); Set<ID> oppIds = new Set<ID>(); Set<Id> processIds = new Set<ID>(); for(OpportunityLineItem oppli : Trigger.New){ oppIds.add(oppli.Id); } List<OpportunityLineItem> opplList = new List<OpportunityLineItem>(); if(oppIds != null && oppIds.size() >0){ opplList = [SELECT Id, Opportunity.Name, Opportunity.AccountId, Opportunity.Account.PersonContactId, OpportunityId, Opportunity.Account.Name, Opportunity.Account.PersonEmail, Opportunity.Account.Salutation, Opportunity.Account.BillingCity, Opportunity.Account.BillingCountry, Opportunity.Account.BillingPostalCode, Opportunity.Account.BillingState, Opportunity.Account.ShippingCity, Opportunity.Account.ShippingCountry, Opportunity.Account.ShippingPostalCode, Opportunity.Account.ShippingState, Opportunity.Account.BillingStreet, Opportunity.Account.ShippingStreet, Opportunity.ContactId, Opportunity.Account.isPersonAccount FROM OpportunityLineItem WHERE Id IN:oppIds]; } if(opplList != null && opplList.size() >0 ){ for(OpportunityLineItem oppli : opplList) { Quote quo = new Quote(); quo.Name = 'Quote - ' + oppli.Opportunity.Name; quo.Status = 'Draft'; quo.OpportunityId = oppli.OpportunityId; // Getting Contact Details from Business Account. if(oppli.Opportunity.Account.isPersonAccount == False) { quo.ContactId = oppli.Opportunity.ContactId; quo.Email = oppli.Opportunity.Contact.Email; /* Need to get Contact Mailing Address and Other Address. */ } // Getting Contact Details from Person Account. else { quo.ContactId = oppli.Opportunity.Account.PersonContactId; quo.Email = oppli.Opportunity.Account.PersonEmail; quo.BillingName = oppli.Opportunity.Account.Name; quo.ShippingName = oppli.Opportunity.Account.Name; quo.BillingStreet = oppli.Opportunity.Account.BillingStreet; quo.BillingCity = oppli.Opportunity.Account.BillingCity; quo.BillingCountry = oppli.Opportunity.Account.BillingCountry; quo.BillingPostalCode = oppli.Opportunity.Account.BillingPostalCode; quo.BillingState = oppli.Opportunity.Account.BillingState; quo.ShippingStreet = oppli.Opportunity.Account.ShippingStreet; quo.ShippingCity = oppli.Opportunity.Account.ShippingCity; quo.ShippingCountry = oppli.Opportunity.Account.ShippingCountry; quo.ShippingPostalCode = oppli.Opportunity.Account.ShippingPostalCode; quo.ShippingState = oppli.Opportunity.Account.ShippingState; } if(!processIds.contains(quo.OpportunityId)) { quoteList.add(quo); processIds.add(quo.OpportunityID); } } } try { if(quoteList.size() > 0) { insert quoteList; } } catch(Exception e) { System.debug('The Following Exception has occurred: '+ e.getLinenumber() + ' : ' + e.getMessage()); } // Insert Quote Line Item set<Id> oppId = new Set<Id>(); set<Id> pcrId = new set<Id>(); List<QuoteLineItem> oliPist = new List<QuoteLineItem>(); if(quoteList != null && quoteList.size() >0){ for(Quote quo : quoteList) { if(quo.opportunityId != null) { oppId.add(quo.opportunityId); } } } Map<Id, List<OpportunityLineItem>> olitoOpp = new Map<Id, List<OpportunityLineItem>>(); if(oppId != null && oppId.size() >0){ for(OpportunityLineItem oli : [SELECT Id, Quantity, UnitPrice, PricebookEntryId, Product2Id, OpportunityId FROM OpportunityLineItem WHERE opportunityId IN : oppId]) { pcrId.add(oli.Product2Id); if(olitoOpp.containsKey(oli.opportunityId)) { olitoOpp.get(oli.opportunityId).add(oli); } else { olitoOpp.put(oli.opportunityId, new List<OpportunityLineItem> {oli}); } } } for(Quote quot : quoteList) { if(olitoOpp.containsKey(quot.opportunityId)) { for(OpportunityLineItem oli : olitoOpp.get(quot.opportunityId)){ if(oli != null) oliPist.add(new QuoteLineItem(QuoteId=quot.Id,Quantity = oli.Quantity,PricebookEntryId = oli.PricebookEntryId,UnitPrice = oli.UnitPrice,Product2Id = oli.Product2Id)); } } } if(!oliPist.isEmpty()) { insert oliPist; } } }
Kindly review my code and let me know how can I get contact values.
Thanks in Advance.
after that you have to make a query to contact to get name, email or other fields as you require: ( Please add the field you require)
finally, you have to change your logic here:
hope this will solve your problem. Let me know if you face any issues. If it helps you to solve your problem please mark it as the best answer.
All Answers
Hi,
In your query please add Opportunity.Contact.Email and then try to get oppli.Opportunity.Contact.Email. And you can do same for addresses as weill. If you are facing any error please post error as well.
If it helps please mark it as best answer, so other can take help from here.
Am getting the below error.
Kindly advice how to solve this error.
Can you tell me like what is the relationship between opportunity and contact in this problem or if you can explain like relationship between object is it default?
after that you have to make a query to contact to get name, email or other fields as you require: ( Please add the field you require)
finally, you have to change your logic here:
hope this will solve your problem. Let me know if you face any issues. If it helps you to solve your problem please mark it as the best answer.
This works fine.. Thanks for your assistance.
Read more: QuickBooks Support Phone Number
QuickBooks Support Phone Number
QuickBooks Support Phone Number