How to clone object record?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












Hi i have code for cloning selected record, but i need to do it without defining each field one by one.
Here is an example with 4 fields but in complete, there are almost 40 fields.



@AuraEnabled
public static List<Claimed_Transaction__c> copyTransaction(String dataJson, String transId)
List<Claimed_Transaction__c> dataList = (List<Claimed_Transaction__c>) JSON.deserialize(dataJson, List<Claimed_Transaction__c>.class);

Claimed_Transaction__c sourceTransaction = [SELECT Id FROM Claimed_Transaction__c WHERE Id=:transId];
Claimed_Transaction__c targetTransaction = new Claimed_Transaction__c();

targetTransaction.Name = sourceTransaction.Name;
targetTransaction.Adress = sourceTransaction.Adress;
targetTransaction.Phone = sourceTransaction.Phone ;
targetTransaction.Temporary_Transaction__c = true;

insert targetTransaction;
dataList.add(targetTransaction);

return dataList;







share|improve this question


























    up vote
    1
    down vote

    favorite












    Hi i have code for cloning selected record, but i need to do it without defining each field one by one.
    Here is an example with 4 fields but in complete, there are almost 40 fields.



    @AuraEnabled
    public static List<Claimed_Transaction__c> copyTransaction(String dataJson, String transId)
    List<Claimed_Transaction__c> dataList = (List<Claimed_Transaction__c>) JSON.deserialize(dataJson, List<Claimed_Transaction__c>.class);

    Claimed_Transaction__c sourceTransaction = [SELECT Id FROM Claimed_Transaction__c WHERE Id=:transId];
    Claimed_Transaction__c targetTransaction = new Claimed_Transaction__c();

    targetTransaction.Name = sourceTransaction.Name;
    targetTransaction.Adress = sourceTransaction.Adress;
    targetTransaction.Phone = sourceTransaction.Phone ;
    targetTransaction.Temporary_Transaction__c = true;

    insert targetTransaction;
    dataList.add(targetTransaction);

    return dataList;







    share|improve this question






















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      Hi i have code for cloning selected record, but i need to do it without defining each field one by one.
      Here is an example with 4 fields but in complete, there are almost 40 fields.



      @AuraEnabled
      public static List<Claimed_Transaction__c> copyTransaction(String dataJson, String transId)
      List<Claimed_Transaction__c> dataList = (List<Claimed_Transaction__c>) JSON.deserialize(dataJson, List<Claimed_Transaction__c>.class);

      Claimed_Transaction__c sourceTransaction = [SELECT Id FROM Claimed_Transaction__c WHERE Id=:transId];
      Claimed_Transaction__c targetTransaction = new Claimed_Transaction__c();

      targetTransaction.Name = sourceTransaction.Name;
      targetTransaction.Adress = sourceTransaction.Adress;
      targetTransaction.Phone = sourceTransaction.Phone ;
      targetTransaction.Temporary_Transaction__c = true;

      insert targetTransaction;
      dataList.add(targetTransaction);

      return dataList;







      share|improve this question












      Hi i have code for cloning selected record, but i need to do it without defining each field one by one.
      Here is an example with 4 fields but in complete, there are almost 40 fields.



      @AuraEnabled
      public static List<Claimed_Transaction__c> copyTransaction(String dataJson, String transId)
      List<Claimed_Transaction__c> dataList = (List<Claimed_Transaction__c>) JSON.deserialize(dataJson, List<Claimed_Transaction__c>.class);

      Claimed_Transaction__c sourceTransaction = [SELECT Id FROM Claimed_Transaction__c WHERE Id=:transId];
      Claimed_Transaction__c targetTransaction = new Claimed_Transaction__c();

      targetTransaction.Name = sourceTransaction.Name;
      targetTransaction.Adress = sourceTransaction.Adress;
      targetTransaction.Phone = sourceTransaction.Phone ;
      targetTransaction.Temporary_Transaction__c = true;

      insert targetTransaction;
      dataList.add(targetTransaction);

      return dataList;









      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 6 at 6:43









      David

      244




      244




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          From the documentation:





          clone(preserveId, isDeepClone, preserveReadonlyTimestamps,
          preserveAutonumber)




          Creates a copy of the SObject record.




          So you can just do this:



          Claimed_Transaction__c targetTransaction = sourceTransaction.clone(false,true,true,false);


          Please read the documentation for more info on tweaking the function using the parameters it takes.






          share|improve this answer



























            up vote
            2
            down vote













            You can use the clone method from SObject class.
            Make sure your source object has all the fields queried. So if you have queried 4 fields then it will populate those fields only in the cloned object.



            Claimed_Transaction__c claimedTransaction = [SELECT Id FROM Claimed_Transaction__c WHERE Id=:transId];
            Claimed_Transaction__c clonedClaimedTransaction = claimedTransaction.clone(false, false, false, false);


            To query all the fields you can run a Dynamic SOQL Query for all fields






            share|improve this answer



























              up vote
              2
              down vote













              you can Use clone Function to copy the selected record.Just to make code easier and not to mention the fields in code,you can use fieldset and retrieve those field in code.



              PFB the sample code.



              List<String> fields = new List<String>();
              FieldSet fieldsToClone = SObjectType.Claimed_Transaction__c.fieldSets.FieldsToClone;
              for (FieldSetMember field : fieldsToClone.getFields())
              fields.add(field.getFieldPath());

              String soql = 'SELECT ' + String.join(fields, ',') +
              ' FROM Claimed_Transaction__c WHERE Id = '' + String.escapeSingleQuotes(transId) + ''';
              return Database.query(soql).Clone(...);


              This is sample code.please change it according to your functionality.






              share|improve this answer








              New contributor




              Gourav is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.

















                Your Answer







                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "459"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                convertImagesToLinks: false,
                noModals: false,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                 

                draft saved


                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f231422%2fhow-to-clone-object-record%23new-answer', 'question_page');

                );

                Post as a guest






























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                5
                down vote



                accepted










                From the documentation:





                clone(preserveId, isDeepClone, preserveReadonlyTimestamps,
                preserveAutonumber)




                Creates a copy of the SObject record.




                So you can just do this:



                Claimed_Transaction__c targetTransaction = sourceTransaction.clone(false,true,true,false);


                Please read the documentation for more info on tweaking the function using the parameters it takes.






                share|improve this answer
























                  up vote
                  5
                  down vote



                  accepted










                  From the documentation:





                  clone(preserveId, isDeepClone, preserveReadonlyTimestamps,
                  preserveAutonumber)




                  Creates a copy of the SObject record.




                  So you can just do this:



                  Claimed_Transaction__c targetTransaction = sourceTransaction.clone(false,true,true,false);


                  Please read the documentation for more info on tweaking the function using the parameters it takes.






                  share|improve this answer






















                    up vote
                    5
                    down vote



                    accepted







                    up vote
                    5
                    down vote



                    accepted






                    From the documentation:





                    clone(preserveId, isDeepClone, preserveReadonlyTimestamps,
                    preserveAutonumber)




                    Creates a copy of the SObject record.




                    So you can just do this:



                    Claimed_Transaction__c targetTransaction = sourceTransaction.clone(false,true,true,false);


                    Please read the documentation for more info on tweaking the function using the parameters it takes.






                    share|improve this answer












                    From the documentation:





                    clone(preserveId, isDeepClone, preserveReadonlyTimestamps,
                    preserveAutonumber)




                    Creates a copy of the SObject record.




                    So you can just do this:



                    Claimed_Transaction__c targetTransaction = sourceTransaction.clone(false,true,true,false);


                    Please read the documentation for more info on tweaking the function using the parameters it takes.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Sep 6 at 6:49









                    Santanu Halder

                    4,6671829




                    4,6671829






















                        up vote
                        2
                        down vote













                        You can use the clone method from SObject class.
                        Make sure your source object has all the fields queried. So if you have queried 4 fields then it will populate those fields only in the cloned object.



                        Claimed_Transaction__c claimedTransaction = [SELECT Id FROM Claimed_Transaction__c WHERE Id=:transId];
                        Claimed_Transaction__c clonedClaimedTransaction = claimedTransaction.clone(false, false, false, false);


                        To query all the fields you can run a Dynamic SOQL Query for all fields






                        share|improve this answer
























                          up vote
                          2
                          down vote













                          You can use the clone method from SObject class.
                          Make sure your source object has all the fields queried. So if you have queried 4 fields then it will populate those fields only in the cloned object.



                          Claimed_Transaction__c claimedTransaction = [SELECT Id FROM Claimed_Transaction__c WHERE Id=:transId];
                          Claimed_Transaction__c clonedClaimedTransaction = claimedTransaction.clone(false, false, false, false);


                          To query all the fields you can run a Dynamic SOQL Query for all fields






                          share|improve this answer






















                            up vote
                            2
                            down vote










                            up vote
                            2
                            down vote









                            You can use the clone method from SObject class.
                            Make sure your source object has all the fields queried. So if you have queried 4 fields then it will populate those fields only in the cloned object.



                            Claimed_Transaction__c claimedTransaction = [SELECT Id FROM Claimed_Transaction__c WHERE Id=:transId];
                            Claimed_Transaction__c clonedClaimedTransaction = claimedTransaction.clone(false, false, false, false);


                            To query all the fields you can run a Dynamic SOQL Query for all fields






                            share|improve this answer












                            You can use the clone method from SObject class.
                            Make sure your source object has all the fields queried. So if you have queried 4 fields then it will populate those fields only in the cloned object.



                            Claimed_Transaction__c claimedTransaction = [SELECT Id FROM Claimed_Transaction__c WHERE Id=:transId];
                            Claimed_Transaction__c clonedClaimedTransaction = claimedTransaction.clone(false, false, false, false);


                            To query all the fields you can run a Dynamic SOQL Query for all fields







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Sep 6 at 7:36









                            Naval Sharma

                            49728




                            49728




















                                up vote
                                2
                                down vote













                                you can Use clone Function to copy the selected record.Just to make code easier and not to mention the fields in code,you can use fieldset and retrieve those field in code.



                                PFB the sample code.



                                List<String> fields = new List<String>();
                                FieldSet fieldsToClone = SObjectType.Claimed_Transaction__c.fieldSets.FieldsToClone;
                                for (FieldSetMember field : fieldsToClone.getFields())
                                fields.add(field.getFieldPath());

                                String soql = 'SELECT ' + String.join(fields, ',') +
                                ' FROM Claimed_Transaction__c WHERE Id = '' + String.escapeSingleQuotes(transId) + ''';
                                return Database.query(soql).Clone(...);


                                This is sample code.please change it according to your functionality.






                                share|improve this answer








                                New contributor




                                Gourav is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                Check out our Code of Conduct.





















                                  up vote
                                  2
                                  down vote













                                  you can Use clone Function to copy the selected record.Just to make code easier and not to mention the fields in code,you can use fieldset and retrieve those field in code.



                                  PFB the sample code.



                                  List<String> fields = new List<String>();
                                  FieldSet fieldsToClone = SObjectType.Claimed_Transaction__c.fieldSets.FieldsToClone;
                                  for (FieldSetMember field : fieldsToClone.getFields())
                                  fields.add(field.getFieldPath());

                                  String soql = 'SELECT ' + String.join(fields, ',') +
                                  ' FROM Claimed_Transaction__c WHERE Id = '' + String.escapeSingleQuotes(transId) + ''';
                                  return Database.query(soql).Clone(...);


                                  This is sample code.please change it according to your functionality.






                                  share|improve this answer








                                  New contributor




                                  Gourav is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                  Check out our Code of Conduct.



















                                    up vote
                                    2
                                    down vote










                                    up vote
                                    2
                                    down vote









                                    you can Use clone Function to copy the selected record.Just to make code easier and not to mention the fields in code,you can use fieldset and retrieve those field in code.



                                    PFB the sample code.



                                    List<String> fields = new List<String>();
                                    FieldSet fieldsToClone = SObjectType.Claimed_Transaction__c.fieldSets.FieldsToClone;
                                    for (FieldSetMember field : fieldsToClone.getFields())
                                    fields.add(field.getFieldPath());

                                    String soql = 'SELECT ' + String.join(fields, ',') +
                                    ' FROM Claimed_Transaction__c WHERE Id = '' + String.escapeSingleQuotes(transId) + ''';
                                    return Database.query(soql).Clone(...);


                                    This is sample code.please change it according to your functionality.






                                    share|improve this answer








                                    New contributor




                                    Gourav is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.









                                    you can Use clone Function to copy the selected record.Just to make code easier and not to mention the fields in code,you can use fieldset and retrieve those field in code.



                                    PFB the sample code.



                                    List<String> fields = new List<String>();
                                    FieldSet fieldsToClone = SObjectType.Claimed_Transaction__c.fieldSets.FieldsToClone;
                                    for (FieldSetMember field : fieldsToClone.getFields())
                                    fields.add(field.getFieldPath());

                                    String soql = 'SELECT ' + String.join(fields, ',') +
                                    ' FROM Claimed_Transaction__c WHERE Id = '' + String.escapeSingleQuotes(transId) + ''';
                                    return Database.query(soql).Clone(...);


                                    This is sample code.please change it according to your functionality.







                                    share|improve this answer








                                    New contributor




                                    Gourav is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.









                                    share|improve this answer



                                    share|improve this answer






                                    New contributor




                                    Gourav is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.









                                    answered Sep 6 at 9:40









                                    Gourav

                                    525




                                    525




                                    New contributor




                                    Gourav is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.





                                    New contributor





                                    Gourav is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.






                                    Gourav is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.



























                                         

                                        draft saved


                                        draft discarded















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f231422%2fhow-to-clone-object-record%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        Comments

                                        Popular posts from this blog

                                        Long meetings (6-7 hours a day): Being “babysat” by supervisor

                                        Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                                        Confectionery