You need to sign in to do that
Don't have an account?
bbrooke2
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
Can you plzz provide information about, what all are the compulsory fileds in the Quote object, so that i can help you out.
Sunil
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;
}
}
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
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;
}
}
}
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.
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;
}
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.
Any ideas on how to get a Quote to automatically synch when its created?
Thanks.
Mandy
To set a quote to be synchronized with the opportunity, you must update the opportunity field "SyncedQuoteId" with the quote's id.