• Developer post
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
rigger cloningAccount on Account (after insert) {
    if(clsStaticVar.blnIsOppInsert == false) {
       return;
    }
    set<id> aid = new set<id>();
    for(Account ac : Trigger.new){
        if(ac.Name != null){
           aid.add(ac.Id);
           System.debug('cloningvalues:' + aid);
       }
    }
   list<Account> alist = [select Name,Fax,Type,Active__C from Account Where Id in:aid];
    if(clsStaticVar.blnIsOppInsert == true) {
        clsStaticVar.blnIsOppInsert = false;
        for(Account acc :alist){
           Account a = new Account();
            a.Name=acc.Name;
            a.Fax=acc.Fax;
            a.Type=acc.Type;          
                 
            System.debug('cloningvalues1:'+a.Type );
            System.debug('cloningvalues1:'+a.Rating );
            System.debug('cloningvalues1:'+a.Industry);
                 
            insert a;
        }       
    }
}


"The above trigger clones the records but only the fields mentioned in the code n not all. How can we clone it dynamically?"

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,