Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Cloning Records in Plugin

Posted on by Microsoft Employee

Hello All,

I have requirement 

Opportunity -> Opportunity Product (1:N)

In Opportunity we have Opportunity Product sub-grid.

But once Currency in Opportunity is set and Opportunity Products are added then Currency of Opportunity cannot be changed (Agree -> By design we cannot modify Opportunity currency since its associated records are in previously set currency)

Actual Requirement :

In order to change the currency we need to delete all Opportunity Product records associated with the Opportunity and then change currency of the Opportunity.(So its very cumbersome)

We need to Clone records of Opportunity Products with updated currency at once when currency is changed.(and of course delete previously created Opportunity Product records with old currency)

Please note that in the error pops-up as shown below in In-Operation stage of Event execution pipeline as this CRM side validation.(My assumption). Thus we cannot register any Plugin in Post-Operation stage.

6644.Error.png

Please help me out how to implement this scenario.

*This post is locked for comments

  • Suggested answer
    khoait Profile Picture
    khoait 450 on at
    RE: Cloning Records in Plugin

    Hi. I would try following approaches:

    1. Use custom action

    2. Use Plugin

    • Create a plugin on Pre-validation Update message for Opportunity entity, filter for Currency attribute change.
    • Create a Pre Entity Image, include attributes. This image will be used to get the "before-update" Currency.
    • Get "before-update" record and "updating" record:
    Entity preImage = (Entity)context.PreEntityImages["PreImage"];
    Entity entity = (Entity)context.InputParameters["Target"];
    
    EntityReference oldCurrency = (EntityReference)preImage["transactioncurrencyid"];
    EntityReference newCurrency = (EntityReference)entity["transactioncurrencyid"];
    • Apply more logic as needed, and use SDK messages to create new Opportunity with fields copied from the current one, delete/deactivate the current one
    • Then you'll want to cancel the Update event, and show a message that a new Opportunity has been created. 
    throw new InvalidPluginExecutionException("A new Opportunity has been cloned");


    Each approach has good and bad things, they depend on your requirements.

    Hope it helps.

  • Verified answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Cloning Records in Plugin

    One thing that you might be able to do here is create an action (let's say it's called Change Currency).

    You pass the new currency to the action via JavaScript which executes the Action code.

    The action code would then clone the Product records with the new currency, and after they are created, delete/deactivate the old ones.

    Once the old records are removed, you should probably be able to modify the Currency via the Plugin (Action) code.

    Hope this helps.

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