You need to sign in to do that
Don't have an account?
venkata mani teja
how to Clone Records Through Fieldsets and Create Record In Same Object
I have Account When Record is created ,i have to COLONE THAT RECORD VALES AND create one Child Account Child record in same Object using FIELD SETS
I've gone through your code. Hope below code helps you:
1.Trigger-->
trigger CloneAccountUsingFieldSet on Account (after insert)
{
if(communitycontroller.flag==true)
{
communitycontroller.flag=false;
communitycontroller.cloneAccount(Trigger.New);
}
}
2.Apex Class-->
public class communitycontroller
{
public static Boolean Flag=true;
public static void cloneAccount(List<Account> allAccounts)
{
try
{
Set<Id> allaccountIds=new Set<Id>();
for(Account acc:allAccounts)
{
allaccountIds.add(acc.Id);
}
String Query='select id';
for(Schema.FieldSetMember fields:SObjectType.Account.FieldSets.FM.getFields())
{
Query+=','+fields.fieldpath;
}
Query+=' from Account where Id IN:allaccountIds';
System.debug('Query-->'+Query);
List<Account> getaccounts= Database.query(Query);
List<Account> cloneAccounts=new List<Account>();
for(Account acc:getaccounts)
{
Account cloneacc=new Account();
cloneacc=acc.Clone(false,true,false,false);
cloneacc.ParentId=acc.Id;
cloneAccounts.add(cloneacc);
}
if(cloneAccounts.size()>0)
insert cloneAccounts;
}
catch(Exception e)
{
System.debug('error in-->'+e.getLineNumber()+'and error is-->'+e.getMessage());
}
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
All Answers
I've gone through your code. Hope below code helps you:
1.Trigger-->
trigger CloneAccountUsingFieldSet on Account (after insert)
{
if(communitycontroller.flag==true)
{
communitycontroller.flag=false;
communitycontroller.cloneAccount(Trigger.New);
}
}
2.Apex Class-->
public class communitycontroller
{
public static Boolean Flag=true;
public static void cloneAccount(List<Account> allAccounts)
{
try
{
Set<Id> allaccountIds=new Set<Id>();
for(Account acc:allAccounts)
{
allaccountIds.add(acc.Id);
}
String Query='select id';
for(Schema.FieldSetMember fields:SObjectType.Account.FieldSets.FM.getFields())
{
Query+=','+fields.fieldpath;
}
Query+=' from Account where Id IN:allaccountIds';
System.debug('Query-->'+Query);
List<Account> getaccounts= Database.query(Query);
List<Account> cloneAccounts=new List<Account>();
for(Account acc:getaccounts)
{
Account cloneacc=new Account();
cloneacc=acc.Clone(false,true,false,false);
cloneacc.ParentId=acc.Id;
cloneAccounts.add(cloneacc);
}
if(cloneAccounts.size()>0)
insert cloneAccounts;
}
catch(Exception e)
{
System.debug('error in-->'+e.getLineNumber()+'and error is-->'+e.getMessage());
}
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com