+ Start a Discussion
bbrooke2bbrooke2 

Create New Quote Object

I am very new to APEX, triggers and classes and trying to create a trigger that will create a new quote record when a new opportunity has been created. (I would like the quote name to be "Quote-"+the opportunity name.)
 
I'm sure this is a very basic code but I am having a hard time getting my bearings straight.
 
Would someone be willing to share how this code would be written.
 
thanks in advance
 
Ben 

sunil316sunil316

Can you plzz provide information about, what all are the compulsory fileds in the Quote object, so that i can help you out.

 

 

Sunil 

prageethprageeth

Hello bbrooke2;

Your trigger should be something as below.

 

trigger QuoteCreator on Opportunity (after insert) {

for (Opportunity o : Trigger.new) {

Quote q = new Quote();

q.name = 'Quote-' + o.name;

q.opportunityId = o.id;

insert q;

}

} 

 
bbrooke2bbrooke2

prageeth, This code works perfect! Thanks for sharing your time and expertise.

 

What I'd like to do now is pre-populate the quote billing address field (BillingAddress) with the billing address of the associated account. Any thoughts on what I would add to the code achieve this?

 

Thanks you again for the time.

 

Ben 

 

 

prageethprageeth
Hello bbrooke2;
Following is one way that you can use to do this. If you want, you can query account details inside the first "for loop" and avoid using a Map. 
 

trigger QuoteCreator on Opportunity (after insert) {

Map<Quote, Id> quoteMap = new Map<Quote, Id>();

for (Opportunity o : Trigger.new) {

Quote q = new Quote();

q.name = 'Quote-' + o.name;

q.opportunityId = o.id;

insert q;

quoteMap.put(q, o.accountId);

}

Account[] accList = [SELECT

billingStreet,

billingCity,

billingState,

billingCountry

FROM

Account

WHERE

id IN :quoteMap.values()];

for (Quote quote : quoteMap.KeySet()) {

for (Account a : accList) {

if (quoteMap.get(quote) == a.Id) {

quote.billingStreet = a.billingStreet;

quote.billingCity = a.billingCity;

quote.billingState = a.billingState;

quote.billingCountry = a.billingCountry;

}

update quote;

}

}

} 

 
 
bbrooke2bbrooke2

This is great!

 

Last question I have is concerning how to make this trigger account for "bulk processing".

 

I'm not 100% sure I understand the concept but I believe it is important for me to make sure that the triggers I use account for this because I will have dozens of reps using the system at any one time. (is that valid condition for needing to account for bulk process?)

 

Does the code above consider bulk processing? If not can you show me how it can?

 

I can't thank you enough for the time you have shared this is extremely helpful.

 

prageethprageeth
Hello bbrooke2;
Im sorry, actually I didn't considered about bult processing. If you do bulk processing, you may need to change the code as below.

trigger QuoteCreator on Opportunity (after insert) {

List<Id> oppIdsList = new List<Id>();

List<Quote> quoteList = new List<Quote>();

for (Opportunity o : Trigger.new) {

oppIdsList.add(o.id);

}

Opportunity[] oppList = [SELECT

name,

id,

account.billingStreet,

account.billingCity,

account.billingState,

account.billingCountry

FROM

Opportunity

WHERE

id IN :oppIdsList];

for (Opportunity o : oppList) {

Quote q = new Quote();

q.name = 'Quote-' + o.name;

q.opportunityId = o.id;

q.billingStreet = o.account.billingStreet;

q.billingCity = o.account.billingCity;

q.billingState = o.account.billingState;

q.billingCountry = o.account.billingCountry;

quoteList.add(q);

}

insert quoteList;

} 

Mandy_in_MNMandy_in_MN

This thread has been super helpful so far.  Thank you!

 

This is what I'm trying to do.  After this thread, I have Steps 1 and 3 working, but I'm not having any luck on Step 2.

 

  1. Automatically create a Quote for every new Opportunity
  2. Automatically Synch that Quote to the Opportunity 
  3. Add a formula field to the Opportunity to display the Synched PNumber

 

Any ideas on how to get a Quote to automatically synch when its created?  

 

Thanks.

Mandy

Mauricio OliveiraMauricio Oliveira

To set a quote to be synchronized with the opportunity, you must update the opportunity field "SyncedQuoteId" with the quote's id.