• madonaa max 9
  • NEWBIE
  • -2 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 4
    Replies
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.

 
Hello,
I am trying to do the following.  Have a button on teh parenet record that will render the child records in a datatable(editable)
What ever is selected I need to be able to create a new record from the rows slected.
I have a LWC and controller.  The LWC is wrapped in a aura compone tbut when selected it just has the header columns.
Should I try to  grab the child records in the .js?

Then how do I add a button to the LWS to create a new record from the rows slected?

Thank you for your help,
P
Hello All,
Can you update a field in LWC?  I want to append a child record field info with info from a parent record? So for example Contact.info = Contact.info + Account.info.  Is this possibe or does this need to be handle in a Apex class?
Thanks,
P  
Hi Guys. 
I have just begun the SuperBadge: Lightning Experience Specialist. I am unable to rename standard objects. Any thoughts? 
Cheers. 
Mark