You need to sign in to do that
Don't have an account?
How to clone the Records Using Record Type Id Based on Opportunity Sobject Using Triggers
trigger cloningopportunity on Opportunity (after insert) {
set<id> oid = new set<id>();
RecordType record =[select id from RecordType where name='1*2'];
for(Opportunity opp : Trigger.new)
{
if(opp.RecordTypeId == record.Id)
{
oid.add(opp.Id);
System.debug('cloningvalues:' + oid);
}
}
list<Opportunity> olist = [select id,Name,CloseDate,StageName,RecordTypeId from Opportunity Where Id in:oid];
for(Opportunity opps :olist)
{
Opportunity o = new Opportunity();
o.Name=opps.Name;
o.CloseDate=opps.CloseDate;
o.StageName=opps.StageName;
o.RecordTypeId =opps.RecordTypeId ;
System.debug('cloningvalues1:'+o.StageName );
insert o;
}
}
throws error
AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY,
hi hitesh
it works fine. thank you very much. i did a small mistak previously
Thanks
arjun
All Answers
Hi arjun,
here is your trigger calls recursively because of that error occurs. you have to use static variable and class to handle this issue as below.
Apex trigger:
Apex class:
Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
Thanks,
Hitesh Patel
Thanks for reply,
I changed my code but it also shows same error
Hi arjun,
i have made one change in trigger now it will works fine try this one.
Apex trigger:
Apex Class:
Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
Thanks,
Hitesh Patel
Thanks!
However......I keep getting the error:
AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, cloningopportunity: maximum trigger depth exceeded Opportunity trigger event AfterInsert for
Hi arjun,
write this line clsStaticVar.blnIsOppInsert = false; above the for(Opportunity opps :olist){ line...
Thanks,
Hitesh Patel
hi hitesh
it works fine. thank you very much. i did a small mistak previously
Thanks
arjun
Hi arjun,
please mark it as a solution so it will helpful to others.
Thanks,
Hitesh Patel
Hi Hitesh
Nice Post
Anyone Pls provide testcases for this Clone the records using recordTypeId.............
Thanks
=========
VenkatSForce
Hi venkat,
Here is the sample example of test class for your trigger.
Important :
Hit Kudos if this provides you with useful information.
Thanks,
Hitesh Patel
SFDC Certified Developer & Administrator
Hi hitesh,
can you explain what is the need of this clsStaticVar.blnIsOppInsert = false; statement when cloning the opportunity.
Thanks in Advance
@Mandadi
It is a static variable which is used to stop to call trigger recursively.