+ Start a Discussion
SFDC 2017SFDC 2017 

Creating related records Multiple Times when stage is changed to closed won....For 1 opp 1 certification record to be created when stage changed to closed won....

Hi All,
My scenario when opportunity stage is closed won  it should create certification record under that opportunity .But it should create only one time.I wrote a trigger as follows:
trigger CertificationDetailsautocreate on Opportunity(after insert,before update)

{

         List<Certification_Detail__c> listCd = new List<Certification_Detail__c>();
       
        //Id rtId = [select Id,name from RecordType where name='Certification - Public(Company Sponsered)' and SObjectType='Opportunity_Detail__c' limit 1].Id;
       
        for(Opportunity O : trigger.new)
        {

        if(Trigger.isInsert)

            {

                if(O.StageName =='Closed Won')

                {

                  listCd.add(new Certification_Detail__c(

                  //RecordTypeId = rtId ,

                  Opportunity__c= O.Id,
                  Account__c=O.AccountId,                              
                  Program_Name__c=O.Certification_Program__c

                  ));

               }
              
                 
              

            }

    

        if(listCd.size() > 0)

        {

            insert listCd;

        }
       
        if(Trigger.isUpdate)
        {
        if(O.StageName =='Closed Won')

                {

                  listCd.add(new Certification_Detail__c(

                  //RecordTypeId = rtId ,

                  Opportunity__c= O.Id,
                  Account__c=O.AccountId,                              
                  Program_Name__c=O.Certification_Program__c

                  ));

               }
               

       

            insert listCd;

       
        /*if(Trigger.isAfter&&Trigger.isupdate)

            {
            for(Opportunity O:Trigger.new)
            {
            if(O.StageName =='Closed Won')
            {
            listCd.add(new Certification_Detail__c(

                  //RecordTypeId = rtId ,

                  Opportunity__c= O.Id,
                  Account__c=O.AccountId,                              
                  Program_Name__c=O.Certification_Program__c

                  ));
            }
if(listCd.size() > 0)

        {

            Update listCd;

        }
       
    }
    }*/
    }
    }
    }
But the problem is every time i am changing the stage to closed won it is creating so many certification records under 1 opportunity .I want under 1 opportunity 1 certification record to be created when stage is changed to closed won.
Please any one help me to solve this issue in urgent Basis.Thanks in advance
Vamsi KrishnaVamsi Krishna
Hi

few changes
1. most probably you will not create new opportunities in Closed Won stage.. so if the trigger runs on after update it should be fine..
2. we need to do the insert/create of records outside the for loop

trigger CertificationDetailsautocreate on Opportunity(after update)

{

        List<Certification_Detail__c> listCd = new List<Certification_Detail__c>();
       
        
        for(Opportunity O : trigger.new)
        {
                Opportunity oldOpp = trigger.oldMap.get(O.id);
                Opportunity newOpp = trigger.newMap.get(O.id);

                if(oldOpp.StageName !='Closed Won' && newOpp.StageName == 'Closed Won')

                {

                  listCd.add(new Certification_Detail__c(

                  //RecordTypeId = rtId ,

                  Opportunity__c= O.Id,
                  Account__c=O.AccountId,                              
                  Program_Name__c=O.Certification_Program__c

                  ));

               }
              
        }
    


        if(listCd.size() > 0)   {

            insert listCd;

        }
}


SFDC 2017SFDC 2017
Thanks for your reply Vamsi.But the problem is if i changed closed won stage to some other stage and if again i changed to closed won then also it is creating one certification record.
SFDC 2017SFDC 2017
I want to create one Certification record under 1 opportunity .But Whenever i am changing that stage as closed won it is creating certification records multiple times for 1 opportunity.
Vamsi KrishnaVamsi Krishna
Hi,
Usual Business process is not to go back and forth with the opportunity stages..

you can create a custom checkbox field in your opportunity say call it CertificationCreated (api name will be CertificationCreated__c )
and set the checkbox to true the first time the opportunity goes to settled won..

then in the trigger code you can check if the field is true or false and decide wether to create new certificate or not..
trigger CertificationDetailsautocreate on Opportunity(after update)

{

        List<Certification_Detail__c> listCd = new List<Certification_Detail__c>();
       
        
        for(Opportunity O : trigger.new)
        {
                Opportunity oldOpp = trigger.oldMap.get(O.id);
                Opportunity newOpp = trigger.newMap.get(O.id);

                if(oldOpp.StageName !='Closed Won' && newOpp.StageName == 'Closed Won' && newOpp.CertificationCreated__c == false)

                {

                  listCd.add(new Certification_Detail__c(

                  //RecordTypeId = rtId ,

                  Opportunity__c= O.Id,
                  Account__c=O.AccountId,                              
                  Program_Name__c=O.Certification_Program__c

                  ));

               }
              
        }
    


        if(listCd.size() > 0)   {

            insert listCd;

        }
}

the other option to query existing certificate for the opportunity in the trigger and see if the count is 0 or more.. if its 0 create new certificate otherwise dont create.. btu this will required anothe SOQL in your trigger..

Vamsi KrishnaVamsi Krishna
oh i forgot to mention how to update the new field in the same trigger.. you can change it to before udpate trigger and add the new field assignment as well

here's the updated code..

trigger CertificationDetailsautocreate on Opportunity(before update)

{

        List<Certification_Detail__c> listCd = new List<Certification_Detail__c>();
       
        
        for(Opportunity O : trigger.new)
        {
                Opportunity oldOpp = trigger.oldMap.get(O.id);
                Opportunity newOpp = trigger.newMap.get(O.id);

                if(oldOpp.StageName !='Closed Won' && newOpp.StageName == 'Closed Won' && O.CertificationCreated__c == false)

                {

                  listCd.add(new Certification_Detail__c(

                  //RecordTypeId = rtId ,

                  Opportunity__c= O.Id,
                  Account__c=O.AccountId,                              
                  Program_Name__c=O.Certification_Program__c

                  ));

                  O.CertificationCreated__c = true;

               }
              
        }
    


        if(listCd.size() > 0)   {

            insert listCd;

        }
}


SFDC 2017SFDC 2017
Thanks Vamsi for your reply.But resolved using size()  method in that trigger.Now it is working fine.