• Kenny Dick 7
  • NEWBIE
  • 5 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
I have developed below code fro cloning but unfortunately because of unique field i am getting error of duplication.

here one text field set to unique.

Apex class: 

public class GSDPartnerCloneWithSkillsController {

    public ApexPages.StandardController controller {get; set;}
    public GSD_Partner__c GSDP {get;set;}
    public ID newRecordId {get;set;}
    public Id OldGSDResourceId {get;set;}
    public Id NewGSDResourceId {get;set;}

    
    public GSDPartnerCloneWithSkillsController(ApexPages.StandardController controller) {

        this.controller = controller;
        GSDP = (GSD_Partner__c)controller.getRecord();

    }
    
    public PageReference cloneWithItems() {

         Savepoint sp = Database.setSavepoint();
         GSD_Partner__c newGSDP;

         try {
         
             GSDP = (GSD_Partner__c) GSDDataUtility.getObject('GSD_Partner__c', GSDP.id);
             
             newGSDP = GSDP.clone(false);
             insert newGSDP;

            
             newRecordId = newGSDP.id;
           
           
             GSD_Resource__c OldGSDResource = [select id from  GSD_Resource__c where Partner__c = :GSDP.id limit 1];
             OldGSDResourceId = OldGSDResource.id;
             system.debug('OldGSDResourceId :'+OldGSDResourceId );
             
             GSD_Resource__c NewGSDResource = [select id from  GSD_Resource__c where Partner__c = :newRecordId  limit 1];
             NewGSDResourceId = NewGSDResource.id;
             system.debug('NewGSDResourceId :'+NewGSDResourceId);
             
             CLoneGSDResourceLang();
             CLoneGSDResourceNon();
             CLoneGSDResourcePPS();
             CLoneGSDResourcePMaster();
             CLoneGSDResourceServices();
             CLoneGSDResourceTech();

         } catch (Exception e){
            Database.rollback(sp);
            ApexPages.addMessages(e);
            return null;
         }

        return new PageReference('/'+newGSDP.id+'/e?retURL=%2F'+newGSDP.id);
    }
    
    public void CLoneGSDResourceLang(){
    
        List<GSD_Resource_Skills_mapping__c> InsertnewSkillList = new List<GSD_Resource_Skills_mapping__c>();
        List<GSD_Resource_Skills_mapping__c> SkillsList = [select id,Skill_Name__c,Skill_Category__c,Standard__c,Stand_By__c,Non_Product_Skill_Level__c,Web_App_Resp_Created_Dt__c,Web_App_Resp_Modified_Dt__c,Responsibility_Skill_Level__c,Web_App_Employee_Id__c,Web_App_ID__c,Web_App_Skill_Id__c from GSD_Resource_Skills_mapping__c where Skill_Category__c = :Label.GSDResourceLang and Resource_ID__c = :OldGSDResourceId];  
        if(!SkillsList.isEmpty()){
            for (GSD_Resource_Skills_mapping__c GSDrskill : SkillsList) {
                  GSD_Resource_Skills_mapping__c newGSDrskill  = GSDrskill.clone(false);
                  newGSDrskill.Resource_ID__c = NewGSDResourceId;
                  InsertnewSkillList.add(newGSDrskill);
            }
            system.debug('InsertnewSkillList:'+InsertnewSkillList);
            insert InsertnewSkillList ;
        }
    }
    
    public void CLoneGSDResourceNon(){
    
        List<GSD_Resource_Skills_mapping__c> InsertnewSkillList = new List<GSD_Resource_Skills_mapping__c>();
        List<GSD_Resource_Skills_mapping__c> SkillsList = [select id,Skill_Name__c,Skill_Category__c,Standard__c,Stand_By__c,Non_Product_Skill_Level__c,Web_App_Resp_Created_Dt__c,Web_App_Resp_Modified_Dt__c,Responsibility_Skill_Level__c,Web_App_Employee_Id__c,Web_App_ID__c,Web_App_Skill_Id__c from GSD_Resource_Skills_mapping__c where Skill_Category__c = :Label.GSDResourceNon and Resource_ID__c = :OldGSDResourceId];  
        if(!SkillsList.isEmpty()){
            for (GSD_Resource_Skills_mapping__c GSDrskill : SkillsList) {
                  GSD_Resource_Skills_mapping__c newGSDrskill  = GSDrskill.clone(false);
                  newGSDrskill.Resource_ID__c = NewGSDResourceId;
                  InsertnewSkillList.add(newGSDrskill);
            }
            system.debug('InsertnewSkillList:'+InsertnewSkillList);
            insert InsertnewSkillList ;
        }
    }
    
    public void CLoneGSDResourcePPS(){
    
        List<GSD_Resource_Skills_mapping__c> InsertnewSkillList = new List<GSD_Resource_Skills_mapping__c>();
        List<GSD_Resource_Skills_mapping__c> SkillsList = [select id,Skill_Name__c,Skill_Category__c,Standard__c,Stand_By__c,Non_Product_Skill_Level__c,Web_App_Resp_Created_Dt__c,Web_App_Resp_Modified_Dt__c,Responsibility_Skill_Level__c,Web_App_Employee_Id__c,Web_App_ID__c,Web_App_Skill_Id__c from GSD_Resource_Skills_mapping__c where Skill_Category__c = :Label.GSDResourcePPS and Resource_ID__c = :OldGSDResourceId];  
        if(!SkillsList.isEmpty()){
            for (GSD_Resource_Skills_mapping__c GSDrskill : SkillsList) {
                  GSD_Resource_Skills_mapping__c newGSDrskill  = GSDrskill.clone(false);
                  newGSDrskill.Resource_ID__c = NewGSDResourceId;
                  InsertnewSkillList.add(newGSDrskill);
            }
            system.debug('InsertnewSkillList:'+InsertnewSkillList);
            insert InsertnewSkillList ;
        }
    }
    
    public void CLoneGSDResourcePMaster(){
    
        List<GSD_Resource_Skills_mapping__c> InsertnewSkillList = new List<GSD_Resource_Skills_mapping__c>();
        List<GSD_Resource_Skills_mapping__c> SkillsList = [SELECT Id, Name, Skill_Name__r.Name, Responsibility_Skill_Level__c, Skill_Name__r.Product_Description__c, Skill_Name__r.Portfolio__c FROM GSD_Resource_Skills_mapping__c WHERE Resource_ID__c = :OldGSDResourceId AND Skill_Category__c =: Label.GSDResourcePMaster ];
        if(!SkillsList.isEmpty()){
            for (GSD_Resource_Skills_mapping__c GSDrskill : SkillsList) {
                  GSD_Resource_Skills_mapping__c newGSDrskill  = GSDrskill.clone(false);
                  newGSDrskill.Resource_ID__c = NewGSDResourceId;
                  InsertnewSkillList.add(newGSDrskill);
            }
            system.debug('InsertnewSkillList:'+InsertnewSkillList);
            insert InsertnewSkillList ;
        }
    }
    
    public void CLoneGSDResourceServices(){
    
        List<GSD_Resource_Skills_mapping__c> InsertnewSkillList = new List<GSD_Resource_Skills_mapping__c>();
        List<GSD_Resource_Skills_mapping__c> SkillsList = [SELECT Id, Name, Skill_Name__r.Name, Responsibility_Skill_Level__c, Skill_Name__r.Product_Description__c, Skill_Name__r.Portfolio__c, Skill_Name__r.Support_Activity__c,Standard__c, Stand_By__c FROM GSD_Resource_Skills_mapping__c WHERE Resource_ID__c = :OldGSDResourceId AND Skill_Category__c ='Services' ];
        if(!SkillsList.isEmpty()){
            for (GSD_Resource_Skills_mapping__c GSDrskill : SkillsList) {
                  GSD_Resource_Skills_mapping__c newGSDrskill  = GSDrskill.clone(false);
                  newGSDrskill.Resource_ID__c = NewGSDResourceId;
                  InsertnewSkillList.add(newGSDrskill);
            }
            system.debug('InsertnewSkillList:'+InsertnewSkillList);
            insert InsertnewSkillList ;
        }
    }
    
    public void CLoneGSDResourceTech(){
    
        List<GSD_Resource_Skills_mapping__c> InsertnewSkillList = new List<GSD_Resource_Skills_mapping__c>();
        List<GSD_Resource_Skills_mapping__c> SkillsList = [SELECT Id, Name, Responsibility_Skill_Level__c, Skill_Name__r.Name, Skill_Name__r.Technologies_Name__c FROM GSD_Resource_Skills_mapping__c WHERE Resource_ID__c =:OldGSDResourceId  AND Skill_Category__c =: Label.GSDResourceTech ];
        if(!SkillsList.isEmpty()){
            for (GSD_Resource_Skills_mapping__c GSDrskill : SkillsList) {
                  GSD_Resource_Skills_mapping__c newGSDrskill  = GSDrskill.clone(false);
                  newGSDrskill.Resource_ID__c = NewGSDResourceId;
                  InsertnewSkillList.add(newGSDrskill);
            }
            system.debug('InsertnewSkillList:'+InsertnewSkillList);
            insert InsertnewSkillList ;
        }
    }
}

apex class:

public class GSDDataUtility {
    public static sObject getObject(string objName, string id) {
 
        String fieldnames = '';
        sObject obj;
        try {
            Map < String, Schema.SObjectType > m = Schema.getGlobalDescribe();
            Schema.SObjectType s = m.get(objName);
            Schema.DescribeSObjectResult r = s.getDescribe();
 
        
            Map < String, Schema.SObjectField > fields = r.fields.getMap();
            for (string field: fields.keySet()) {
                if (fieldnames == '') {
                    fieldnames = field;
                } else {
                    fieldnames += ',' + field;
                }
            }
 
            
            String sql = 'SELECT ' + fieldnames + ' FROM ' + objName + ' WHERE Id=\'' + id + '\'';
            System.debug(sql);
            obj = database.query(sql);
 
        } catch (Exception e) {
            System.debug('get Fields Error' + e.getMessage());
            ApexPages.addMessages(e);
            return null;
        }
        return obj;
    }
}