web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Plugin to do a roll up field in Dynamics CRM 2013

(0) ShareShare
ReportReport
Posted on by 334

Unfortunately Dynamics CRM 2013 does not have a roll up field which perfectly suits my requirement.

So I  have a parent entity called Savings and a child entity called deposit.

So savings as many deposits.

On the deposit entity there is a currency field called amount deposited.

and on the savings entity there is a field called total amount deposited.

I need a plugin on update , delete or modify of the deposit amount field, it will roll up the deposit amount filed tied to the savings and update the field Total Amount deposited.

On the deposit entity the savings look up is a required field.

I just started learning plugins and have been able to fully understand the follow up plugin in the sample SDK and execute it myself.

will appreciate any help , already figured the plugin logic.

on the deposit entity anytime the change is made the code will do a look up to the savings then do a loop of the deposit amount associated the the savings and add it up, the loop  is my challenge

Regards.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    I've done many similar plugins. Here are my hints:

    1. Don't develop 3 different plugins. It's possible to incorporate all the logic inside one plugin.

    2. For calculation use aggregations - msdn.microsoft.com/.../gg309565.aspx

    3. To get Id of parent record in "Create" logic use the record itself. For "Update" and "Delete" logic use Pre-Image.

    4. Register your plugin to be executed on Post-Operation stage.

    Feel free to update thread if you have questions.

  • lawix10 Profile Picture
    334 on at

    Thanks, i was not thinking of doing 3 plugins one is just fine.

    I don't understand using the loop with the fetch xml , i think logically am searching for all amount number in the deposit entity where the GUID of the savings is equals to the savings look up.

    I have used advance find on the UI

    <fetch mapping="logical" version="1.0" distinct="false" output-format="xml-platform">
    <entity name="plx_deposit">
    <attribute name="plx_depositid" />
    <attribute name="plx_amount"/>
    <order descending="false" attribute="plx_name" />
    <filter type="and">
    <condition value="{4F559AC6-2467-E711-811B-480FCFE97E21}" attribute="plx_savings" operator="eq" uitype="plx_savings" uiname="Test" />
    </filter>
    </entity>
    </fetch>

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Yeah. That's possible. But with aggregation you will not need to loop through all child records because you will get total aggregated amount in 1 record with no need to loop through x child records and summing up amounts. That aggregation is something similar to aggregations in SQL. If you know SQL then this is something similar to

    Select sum(field) from Table

  • lawix10 Profile Picture
    334 on at

    the xml from the advance find

    <fetch mapping="logical" version="1.0" distinct="false" output-format="xml-platform">

    <entity name="plx_deposit">

    <attribute name="plx_depositid" />

    <attribute name="plx_amount"/>

    <order descending="false" attribute="plx_name" />

    <filter type="and">

    <condition value="{4F559AC6-2467-E711-811B-480FCFE97E21}" attribute="plx_savings" operator="eq" uitype="plx_savings" uiname="Test" />

    </filter>

    </entity>

    </fetch>

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Try to use following aggregated query:

    <fetch mapping="logical" version="1.0" distinct="false" output-format="xml-platform">
    <entity name="plx_deposit">
    <attribute name="plx_amount" alias='totalamount' aggregate='sum'/>
    <filter type="and">
    <condition value="{4F559AC6-2467-E711-811B-480FCFE97E21}" attribute="plx_savings" operator="eq" uitype="plx_savings" uiname="Test" />
    </filter>
    </entity>
    </fetch>


    and here is the code you can use to get total:

    var totalrecord = service.RetrieveMultiple(new FetchExpression(fetchxmlhere)).Entities.FirstOrDefault();
    
    var total = totalrecord.Contains("totalamount") ? ((Money)totalrecord.GetAttributeValue<AliasedValue>("totalamount").Value).Value : decimal.Zero;


    Looping in example has ho sense. I agree with you on that.

  • lawix10 Profile Picture
    334 on at

    Thanks so so much, I can understand it better now.  

    So the number 4F559AC6-2467-E711-811B-480FCFE97E21 is the GUID? how will i convert it to text

    on the deposit entity

    var Savings id = Deposit["plx_savings"].GUID.tostring(); ??

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Should be something like

    var SavingsId = Deposing.GetAttributeValue<EntityReference>("plx_savings").Id;//Will give you guid of record

  • lawix10 Profile Picture
    334 on at

    Thanks , tried contacting you from your blog no email,

    Please how do I call the other entity from the plugin and update.

    Entity savings = Entity("plx_savings") // where filter where do i filter the GUID on the svaings

    savings["TotalAmount"] = total; // from the fetchxml

    service.update (savings)

    Regards

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    It should be something like:

    Entity savings = Entity("plx_savings", <Put Guid you retrieved earlier here>);

    savings["plx_totalamount"] = new Money(total);//use schema field name, not display name

    service.Update(savings);

  • lawix10 Profile Picture
    334 on at

    Thanks you literally wrote the code for me.

    I really appreciate.

    There is more to plugins than creating the follow up plugins

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans