• Paul Kim 24
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
Hey everyone,

New to APEX so I apologize if my question doesn't have enough information to start to accurately portray my question.

The problem I'm trying to solve is that on Opportunities, we're currently using Products. I'd like for the Opportunity Stage Name of "Closed Pending" to 1. create a Quote and then 2. then take all of the Opportunity Products and clone them onto the Quote as Quote Line Items - keeping in mind that the Products are the same for both the Quote and the Opportunity. Here's my code:

This is to create the Quote at the point of Stage = Closed Pending (isPending is a formula checkbox for when Stage is Closed Pending, so may be redundant?)
trigger CreateQuote on Opportunity (after update) {
    
    List<Opportunity> o = new List <Opportunity>();
    o = [SELECT ID FROM Opportunity WHERE StageName = 'Closed Pending'];
    
    for (Opportunity opp: Trigger.new) {
        if(opp.isPending__c ==true){
        Quote newQuote = new Quote();
        
        newQuote.Name = opp.Name + ' Quote';
        newQuote.OpportunityId = opp.Id;
        
        insert newQuote;
    }
    }   
}
Here's the second trigger where I attempt to clone the Opportunity Products:
trigger QuoteLineItem on Quote (after insert) {
    
    Quote q=trigger.new[0];
    
    Opportunity oppID=[SELECT id FROM Opportunity WHERE id=:q.OpportunityId /*AND StageName = 'Closed Pending'*/];
    
    List<OpportunityLineItem> opplines=[SELECT id, PricebookEntryId, Product2ID, Quantity, UnitPrice, ARR__c 
                                        FROM OpportunityLineItem 
                                        WHERE OpportunityId=:oppID.id];
    
    for(OpportunityLineItem oppline:opplines){
        
        QuoteLineItem qli = new QuoteLineItem();
        qli.QuoteId = q.id; 
        qli.Product2Id = oppline.PriceBookEntry.Product2Id;
        qli.Quantity = oppline.Quantity;
        qli.ARR__c = oppline.ARR__c;
        qli.UnitPrice = oppline.UnitPrice;
        insert qli;

    }

}

Any help would be greatly appreciated!
 
Hey everyone,

New to APEX so I apologize if my question doesn't have enough information to start to accurately portray my question.

The problem I'm trying to solve is that on Opportunities, we're currently using Products. I'd like for the Opportunity Stage Name of "Closed Pending" to 1. create a Quote and then 2. then take all of the Opportunity Products and clone them onto the Quote as Quote Line Items - keeping in mind that the Products are the same for both the Quote and the Opportunity. Here's my code:

This is to create the Quote at the point of Stage = Closed Pending (isPending is a formula checkbox for when Stage is Closed Pending, so may be redundant?)
trigger CreateQuote on Opportunity (after update) {
    
    List<Opportunity> o = new List <Opportunity>();
    o = [SELECT ID FROM Opportunity WHERE StageName = 'Closed Pending'];
    
    for (Opportunity opp: Trigger.new) {
        if(opp.isPending__c ==true){
        Quote newQuote = new Quote();
        
        newQuote.Name = opp.Name + ' Quote';
        newQuote.OpportunityId = opp.Id;
        
        insert newQuote;
    }
    }   
}
Here's the second trigger where I attempt to clone the Opportunity Products:
trigger QuoteLineItem on Quote (after insert) {
    
    Quote q=trigger.new[0];
    
    Opportunity oppID=[SELECT id FROM Opportunity WHERE id=:q.OpportunityId /*AND StageName = 'Closed Pending'*/];
    
    List<OpportunityLineItem> opplines=[SELECT id, PricebookEntryId, Product2ID, Quantity, UnitPrice, ARR__c 
                                        FROM OpportunityLineItem 
                                        WHERE OpportunityId=:oppID.id];
    
    for(OpportunityLineItem oppline:opplines){
        
        QuoteLineItem qli = new QuoteLineItem();
        qli.QuoteId = q.id; 
        qli.Product2Id = oppline.PriceBookEntry.Product2Id;
        qli.Quantity = oppline.Quantity;
        qli.ARR__c = oppline.ARR__c;
        qli.UnitPrice = oppline.UnitPrice;
        insert qli;

    }

}

Any help would be greatly appreciated!
 
Hello All,

I currently have an APEX trigger which (when an opportunity has a QUOTE and is closed won), creates a second quote (which will be used as an invoice)

I was wondering if anyone can help me with possibly editing this code to also pull the Opportunity Line Items and add a quote line item for each one in the new automatically generated quote?

For example, If I have the Opportunity Line Item "Dog" from the "Animals" Pricebook at "$100" how can I change the code to not only automatically create the quote but also add a Quote Line Item for a $100 Dog?

Here is my code:

trigger InvoicefromQuote on Opportunity (after insert, after update) {
Map<Quote, Id> quoteMap = new Map<Quote, Id>();
for (Opportunity o : Trigger.new) {
if(o.isWon == true && o.HasQuote__c == true ){
Quote q = new Quote();
q.name = 'Quote-' + o.name;
q.opportunityId = o.id;
insert q;
quoteMap.put(q, o.accountId);
}
}
}

Any help you can provide would be great!


Thank you!