Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Clone Object

Posted on by 12,070 Super User 2024 Season 1

Hi all

I want to copy the logic as we have on product as shown below to my custom entity:

701546.Untitled.png

How could I achieve this functionality on my custom entity?

Thank you

Regards,

AW

*This post is locked for comments

  • Abdul Wahab Profile Picture
    Abdul Wahab 12,070 Super User 2024 Season 1 on at
    RE: Clone Object

    Hi Aric and Pawel,

    I created my custom workflow which is cloning a record successfully but the problem is after cloning a record. I am not able to open my cloned record in the mean while or in real time.

    So that I changed my mind and start writing JS which is working successfully but the problem is I have to retrieve many to many relationship records through JS and I am not able to find any method to retrieve. Is there any way that solve my problem?

    Thank you

    Regards,

    AW

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Clone Object

    The problem as I showed in the code above in on the create function, that you have to make sure that you don't clone the primary key of the record that you are cloning, which will give you a duplicate record error.

    Notice here, that for the primary key, I don't copy the field, but all other fields I do:

    case "new_entitynameid":

       break;

    default:

       newEntity[attributeName] = attributeValue;

       break;

    Hope this resolves the issue.

  • Abdul Wahab Profile Picture
    Abdul Wahab 12,070 Super User 2024 Season 1 on at
    RE: Clone Object

    Hi Aric and Pawel

    I wrote custom workflow and run it through run workflow option. I have copied all the field in my logic and create new record I have below error:

    1537.Untitled-1.png

    What I have to do to resolve this one?

    Thank you

    Regards,

    AW

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Clone Object

    The logic is pretty simple.

    1. Create a action under processes to clone the new record. You don't need to enter any input/output parameters

    2. Add javascript code to your page to execute the clone method. You can use the process.js to simplify your logic.

    function executeClone()

    {

       var entityId = Xrm.Page.data.entity.getId();

       var entityName = "new_entity";

       var messageName = "new_clonemessage";

       var success = callCloneAction(entityId, entityName, messageName, callback);

       if (success) {

       }

    }

    function callCloneAction(entityId, entityName, messageName, name) {

       var inputParams =

           [

               {

                   key: "Target",

                   type: Process.Type.EntityReference,

                   value: new Process.EntityReference(entityName, entityId)

               }

           ];

       var success = callProcessAction(entityId, entityName, messageName, inputParams);

       return success;

    }

    function callProcessAction(entityId, entityName, messageName, inputParams) {

       var success = true;

       Process.callAction(messageName, inputParams,

       function (params) {

           // Success

           var result = "";

           for (var i = 0; i < params.length; i++) {

               result = params[i].key + "=" + params[i].value + ";";

           }

           // alert(result);

           success = true;

       },

       function (e, t) {

           // Error

           success = false;

           alert(e);

           if (window.console && console.error)

               console.error(e + "\n" + t);

       }

       );

       return success;

    }

    3. Create a plugin to execute the process

    4. Add a function to retrieve the current entity values:

           private Entity RetrieveData(string entityName, Guid entityId)

           {

               Entity custom = service.Retrieve(entityName, entityId, new ColumnSet(true));

               return custom;

           }

    5. Add a function to create the new entity:

           private Guid CreateNewEntity(Entity current)

           {

               Entity newEntity = new Entity(myEntity.EntityLogicalName);

               foreach (KeyValuePair<String, Object> attribute in newEntity.Attributes)

               {

                   string attributeName = attribute.Key;

                   object attributeValue = attribute.Value;

                   switch (attributeName.ToLower())

                   {

                       case "new_entitynameid":

                           break;

                       default:

                           newEntity[attributeName] = attributeValue;

                           break;

                   }

               }

               try

               {

                   Guid newEntityId = service.Create(newEntity);

                   return newEntityId;

               }

               catch (FaultException<OrganizationServiceFault> ex)

               {

                   throw new InvalidPluginExecutionException("An error occurred in the CreateNewEntity function of the plug-in.", ex);

               }

           }

    That's basically it.

    Hope this helps...

    Good luck.

  • Suggested answer
    tw0sh3ds Profile Picture
    tw0sh3ds 5,600 on at
    RE: Clone Object

    You need to create and action/plugin/custom activity that will do the cloning logic and run it under this button (which you can add to your custom entity using Ribbon Workbench). This is not trivial to do correctly, so I will not post the code here, but I believe that it would be a nice training for you to implement such function. There are also many third-party solutions for such functionality if you don't want to write it by yourself.

  • Abdul Wahab Profile Picture
    Abdul Wahab 12,070 Super User 2024 Season 1 on at
    RE: Clone Object

    Hi Thomas,

    I think the way you are saying it is not possible.

    Thank you

    Regards,

    AW

  • Suggested answer
    Thomas David Dayman Profile Picture
    Thomas David Dayman 11,323 on at
    RE: Clone Object

    Could you not go into the Ribbon Workbench and copy the functionality of 'Clone' into your new custom button?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans