+ Start a Discussion
Radhe Shyam.Radhe Shyam. 

Create parent record from child trigger then populate on child

Hi DEVs,

I have Quote(child) and opportunity(parent).
I need to create Opportunity if Quote is created. AND then need to populate same oppty on Quote. 
I have done half task, created oppty from Quote Trigger on After Insert. But now challange how to populate this back on quote In bulkify way.
I am missing somthing that how we will keep track of quote wise oppty created??
Please guide.
Here is my code.
//Handler for Quote Trigger

public class QuoteTriggerHandler{

    public static void AfterInsertActions(List<Quote> lstQuotes){
    System.debug('Hello Trigger After Insert');
    List<opportunity> lstOppToInsert = new list<opportunity>();
    //Iterate loop over all new created records of Quote
            for(Quote eachQuote : lstQuotes){
            //Create instanse
            Opportunity opp = new opportunity();
            opp.Name = eachQuote.Name;
            opp.StageName= 'Closed Won';
            opp.Amount= 100; //This will be calculated and overrited automaticlly form oppty lines's price sum. We just need to populate dummy as it is required field.
            opp.CurrencyIsoCode= eachQuote.CurrencyIsoCode;
            opp.AccountId= eachQuote.AccountId;
            opp.CloseDate= System.Today();//eachQuote.Name;
            opp.ForecastCategoryName= 'Closed';
            opp.OwnerId= eachQuote.OwnerId;
            if(lstOppToInsert!=null && lstOppToInsert.size()>0){
                Database.SaveResult[] srList = Database.insert(lstOppToInsert, false);
                System.debug('lstOppToInsert '+lstOppToInsert);
        }catch(Exception e){
        System.debug('Error  '+e.getMessage());
    //Once Opportunity has been inserted, Populate Same opportunity on quote



Ajay K DubediAjay K Dubedi
Hi Radhe,
You need to create an inline vf page on Quote to display the Opportunites associated with a Quote. In that inline Vf page you just need to fetch and display the opportunities associated with the Quote.
Radhe Shyam.Radhe Shyam.

I am looking for bulk record creation.
Will upload data from csv and then these thing should happen.

I have found one way, please let me know if i can do some optimization in this.

1. in above code, i made few change and capturing the Quote id on opportunity.
2. written trigger on Opportunity on after insert: Here is code - please suggest where i can do optimization if required.
This code is working fine.
trigger Opp_CommonTrigger on Opportunity(after insert, before update) {
    if (Trigger.IsAfter && Trigger.IsInsert) {
        Map < id, string > map_quoteId = new Map < id, string > ();
        Set < id > qIds = new set < id > ();
        Set < string > quoteIds = new set < string > ();
        for (Opportunity opp: trigger.new) {
            if (opp.Quote_Id__c != null) {
                map_quoteId.put(opp.Quote_Id__c, opp.id);


        //Get all Quotes where id in quoteIds
        List < Apttus_Proposal__Proposal__c > lstQuote = new list < Apttus_Proposal__Proposal__c > ([select id, Apttus_Proposal__Opportunity__c from Apttus_Proposal__Proposal__c where
            id IN: quoteIds
        List < Apttus_Proposal__Proposal__c > lstQuoteToUpdate = new List < Apttus_Proposal__Proposal__c > ();
        //Iterate loop over quotes and put oppty id in quote.
        if (!map_quoteId.isEmpty()) {
            if (lstQuote != null && lstQuote.size() > 0) {

                for (Apttus_Proposal__Proposal__c eachQuote: lstQuote) {

                    Apttus_Proposal__Proposal__c q = new Apttus_Proposal__Proposal__c();
                    q.id = eachQuote.id;
                    q.Apttus_Proposal__Opportunity__c = map_quoteId.get(eachQuote.id);


        //Update Quotes
        if (lstQuoteToUpdate != null && lstQuoteToUpdate.size() > 0) {
            List<Database.SaveResult> SR = Database.update(lstQuoteToUpdate);



Radhe S