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
venkata mani tejavenkata 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  
Best Answer chosen by venkata mani teja
Deepali KulshresthaDeepali Kulshrestha
Hi Venkata,

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

Deepali KulshresthaDeepali Kulshrestha
Hi Venkata,

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
 
This was selected as the best answer
venkata mani tejavenkata mani teja
Tq Deepali