function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
HTANIRSHTANIRS 

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.
 
/** 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.

 
Best Answer chosen by HTANIRS
Jitendra Singh ShahiJitendra Singh Shahi
So here first, you have to fetch all the contact id from below query to a set. 
Set<String> contactIdSet = new Set<String>();
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];

for(OpportunityLineItem opLineItem: opplList){
   contactIdSet.add(opLineItem.ContactId);
}


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)

List<Contact> conList = [Select email, otherfield you require from contact];


finally, you have to change your logic here: 

if(oppli.Opportunity.Account.isPersonAccount == False) {
    quo.ContactId = oppli.Opportunity.ContactId;
    for(Contact con : conList){
        if(con.id == oppli.Opportunity.ContactId){
            quo.Email = con.Email;
            // add all other field from con object to quo object
        }
    }
}

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

Jitendra Singh ShahiJitendra Singh Shahi

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.

HTANIRSHTANIRS
Hi Jitendra,

Am getting the below error.

User-added image

Kindly advice how to solve this error.
Jitendra Singh ShahiJitendra Singh Shahi
sure,
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?
HTANIRSHTANIRS
I am getting contact details based on opportunity contact role.
Jitendra Singh ShahiJitendra Singh Shahi
So here first, you have to fetch all the contact id from below query to a set. 
Set<String> contactIdSet = new Set<String>();
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];

for(OpportunityLineItem opLineItem: opplList){
   contactIdSet.add(opLineItem.ContactId);
}


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)

List<Contact> conList = [Select email, otherfield you require from contact];


finally, you have to change your logic here: 

if(oppli.Opportunity.Account.isPersonAccount == False) {
    quo.ContactId = oppli.Opportunity.ContactId;
    for(Contact con : conList){
        if(con.id == oppli.Opportunity.ContactId){
            quo.Email = con.Email;
            // add all other field from con object to quo object
        }
    }
}

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 was selected as the best answer
HTANIRSHTANIRS
Hi Jitendra,

This works fine.. Thanks for your assistance.
madonaa max 9madonaa max 9
Get the world-class accounting platform support at QuickBooks Support Phone Number #1-833-57O-O8OO. QuickBooks Pro is known to be a world-class accounting platform that has also provided its customers with a range of accounting services. If you’re managing your company, you can use this QuickBooks Support Phone Number #1-833-57O-O8OO Accounting service to make your job simpler. For this, you should be able to manage all sorts of company dealings quickly.
Read more: QuickBooks Support Phone Number
QuickBooks Support Phone Number
QuickBooks Support Phone Number