You need to sign in to do that
Don't have an account?
SFDC 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
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
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
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..
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..
here's the updated code..