You need to sign in to do that
Don't have an account?
Nirmal9114
trigger to clone account with related list
my requirement is to clone an existing account with all the fields as well as related list.
it should open in an edit mode so that user selects to save it or not. (edit mode so that if they want to make any changes)
my trigger :
trigger TestClone on Account (after insert) {
Account [] accToSave = new Account[]{};
if(checkRecursive.runOnce()){
// query accounts and store by there name to lookup it up quickly
Map<Id,Account> accountMap = new Map<Id,Account>([ Select Id, Name, Description, Phone, Fax
From Account
Where Id IN: trigger.new]);
// clone
for (Account acc : trigger.new){
Account Clone = new Account();
Clone.Name = acc.Name;
Clone.Type = acc.Type;
Clone.Phone = acc.Phone;
Clone.Fax = acc.Fax;
Clone.Description = acc.Description;
accToSave.add(Clone);
}
insert accToSave;
}
}
class:
public Class checkRecursive{
private static boolean run = true;
public static boolean runOnce(){
if(run){
run=false;
return true;
}else{
return run;
}
}
}
i may be wrong somewhere PLEASE HELP.
thanks
it should open in an edit mode so that user selects to save it or not. (edit mode so that if they want to make any changes)
my trigger :
trigger TestClone on Account (after insert) {
Account [] accToSave = new Account[]{};
if(checkRecursive.runOnce()){
// query accounts and store by there name to lookup it up quickly
Map<Id,Account> accountMap = new Map<Id,Account>([ Select Id, Name, Description, Phone, Fax
From Account
Where Id IN: trigger.new]);
// clone
for (Account acc : trigger.new){
Account Clone = new Account();
Clone.Name = acc.Name;
Clone.Type = acc.Type;
Clone.Phone = acc.Phone;
Clone.Fax = acc.Fax;
Clone.Description = acc.Description;
accToSave.add(Clone);
}
insert accToSave;
}
}
class:
public Class checkRecursive{
private static boolean run = true;
public static boolean runOnce(){
if(run){
run=false;
return true;
}else{
return run;
}
}
}
i may be wrong somewhere PLEASE HELP.
thanks
Refer the below link you get some idea,
https://developer.salesforce.com/forums/?id=906F0000000AeTSIA0
Thanks,
Vijay
use deep clone to copy the Account object first,then deep clone all related list objects in simar way and insert in database.
Example(of deep clone on Opportunity):
// Load the original back, this time also getting a referenced Account object.
Opportunity original = [select Name, Account.Name from Opportunity where id=:opp.id];
// Clone both ways.
Opportunity deep = original.Clone(true,true);
Opportunity shallow = original.Clone(true,false);
// All the Account Names will be the same.
system.debug('original.Account.Name: ' + original.Account.Name);
system.debug('deep.Account.Name: ' + deep.Account.Name);
system.debug('shallow.Account.Name: ' + shallow.Account.Name);
For Opening it in edit mode use this url in pageReference : https://[instance name]/[Account ID]/e?id=[Account ID].