function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
B NagarjunaB Nagarjuna 

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,

Best Answer chosen by Admin (Salesforce Developers) 
B NagarjunaB Nagarjuna

hi hitesh

 

it works fine. thank you very much. i did a small mistak previously

 

 

Thanks

arjun

All Answers

hitesh90hitesh90

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:

trigger cloningopportunity on Opportunity (after insert) {
    if(clsStaticVar.blnIsOppInsert == false) {
        return;
    }
	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];
	if(clsStaticVar.blnIsOppInsert == true) {
		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;
		}
		clsStaticVar.blnIsOppInsert = false;
	}
}

 

Apex class:

Public Class clsStaticVar {
    Public Static Boolean blnIsOppInsert = true;
}

 

 

 

 

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

B NagarjunaB Nagarjuna

Thanks for reply,

 

I changed  my code but it also  shows same error

hitesh90hitesh90

Hi arjun,

i have made one change in trigger now it will works fine try this one.

Apex trigger:

trigger cloningopportunity on Opportunity (after insert) {
    if(clsStaticVar.blnIsOppInsert == false) {
        return;
    }
    set<id> oid = new set<id>();
    for(Opportunity opp : Trigger.new){
        if(opp.Name == 'Test'){
            oid.add(opp.Id);
            System.debug('cloningvalues:' + oid);
        }
    }
    list<Opportunity> olist = [select id,Name,CloseDate,StageName from Opportunity Where Id in:oid];
    if(clsStaticVar.blnIsOppInsert == true) {
        clsStaticVar.blnIsOppInsert = false;
        for(Opportunity opps :olist){
            Opportunity o = new Opportunity();
            o.Name=opps.Name;
            o.CloseDate=opps.CloseDate;
            o.StageName=opps.StageName;
           
            System.debug('cloningvalues1:'+o.StageName );
            insert o;
        }        
    }
}

  Apex Class:

Public Class clsStaticVar {
    Public Static Boolean blnIsOppInsert = true;
}

 

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

B NagarjunaB Nagarjuna

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

 

 

hitesh90hitesh90

Hi arjun,

 

write this line clsStaticVar.blnIsOppInsert = false; above the for(Opportunity opps :olist){ line...

 

Thanks,

Hitesh Patel

B NagarjunaB Nagarjuna

hi hitesh

 

it works fine. thank you very much. i did a small mistak previously

 

 

Thanks

arjun

This was selected as the best answer
hitesh90hitesh90

Hi arjun,

 

please mark it as a solution so it will helpful to others.

 

Thanks,

Hitesh Patel

venkatsforcevenkatsforce

Hi Hitesh

 

               Nice Post

               Anyone  Pls provide testcases for this Clone the records using recordTypeId.............

 

 

Thanks

=========

VenkatSForce

hitesh90hitesh90

Hi venkat,

 

Here is the sample example of test class for your trigger.

@istest
public class Testcloningopportunity {
    Private Static testmethod void Testcloningopportunity(){
		RecordType objRecType  =[select id from RecordType where name='1*2'];
		
		Opportunity objOpp = new Opportunity();
		objOpp.Name = 'Test Opp';
		objOpp.RecordTypeId = objRecType.id;
		objOpp.CloseDate = system.today();
		objOpp.StageName = 'Closed Won';
		insert objOpp;
	}
}

 

Important :
Hit Kudos if this provides you with useful information.
 
Thanks,
Hitesh Patel
SFDC Certified Developer & Administrator

MandadiMandadi

Hi hitesh,

 

               can you explain what is the need of this clsStaticVar.blnIsOppInsert = false; statement when cloning the opportunity.

 

 

Thanks in Advance

hitesh90hitesh90

@Mandadi

 

It is a static variable which is used to stop to call trigger recursively.

Developer postDeveloper post
This triger clones the feilds only which are mentioned in the code.But i want a trigger that clone the record with whatever field we populate.EX("lead source and probability or any other feilds")