3
votes

I have a 1:N relationship between Account and Portfolios in Dynamics CRM I.e each account has multiple Portfolios and Each Portfolio has Specific Assets.

I am trying to create a field on Account Form which calculates the sum of "ALL Assets of All related portfolios" of the account and display it on the Account form

As a workaround,I tried to create a Portfolio view grouping by Account but it doesnt SUM and rollup the Portfolio assets to Account level.

enter image description here

enter image description here

So on account Form i am trying to create a textfield which calculates the Total Account Assets to be $25,000 in this example

3
When is the calculation supposed to happen? When you create an Account? When you update it? Or maybe during creation of a Portfolio? - Konrad Viltersten
It should appear on "Account" Form just like any other field and be able to pull it onto a view too !! - Ravi Yenugu
Youcan populate the field using javascript on form load by fetching data from the db with fetch xml or any other form of going to thr db.. - Greg Oks
Does it need to be stored, so you can run reports off of it, or is it solely for the user viewing the entity? - Daryl
Doesnt need to be stored because its a variable number, My goal is to create a view on account and pull in the Total assets column - Ravi Yenugu

3 Answers

3
votes
function setupGridRefresh() {
var targetgrid = document.getElementById("NAME OF SUBGRID");

// If already loaded
if (targetgrid.readyState == 'complete') {
    targetgrid.attachEvent("onrefresh", subGridOnload);
}
else {
    targetgrid.onreadystatechange = function applyRefreshEvent() {
        var targetgrid = document.getElementById("NAME OF SUBGRID");
        if (targetgrid.readyState == 'complete') {
            targetgrid.attachEvent("onrefresh", subGridOnload);
        }
    }
}
subGridOnload();
}

function subGridOnload() {
//debugger;
var grid = Xrm.Page.ui.controls.get('NAME OF SUBGRID')._control;
var sum = 0.00;

if (grid.get_innerControl() == null) {
    setTimeout(subGridOnload, 1000);
    return;
}
else if (grid.get_innerControl()._element.innerText.search("Loading") != -1) {
    setTimeout(subGridOnload, 1000);
    return;
}

var ids = grid.get_innerControl().get_allRecordIds();
var cellValue;
for (i = 0; i < ids.length; i++) {
    if (grid.get_innerControl().getCellValue('FIELD NAME LOWER CASE', ids[i]) != "") {
        cellValue = grid.get_innerControl().getCellValue('FIELD NAME LOWER CASE', ids[i]);
        cellValue = cellValue.substring(2);
        cellValue = parseFloat(cellValue);
        sum = sum + cellValue;
    }

}

var currentSum = Xrm.Page.getAttribute('DESTINATION FIELD').getValue();
if (sum > 0 || (currentSum != sum && currentSum != null)) {
    Xrm.Page.getAttribute('DESTINATION FIELD').setValue(sum);
}
}

I pieced this together from a couple of sources and currently use it one of my solutions. Let me know if you need some more help or if I've misread the question. (Btw, this solution is based on the assumption that you need the total to change when the subgrid has entries added or removed. If this is not the requirement, I would suggest the RetrieveMultiple OData call.)

2
votes

Take a look at AutoSummary from Gap Consulting, well worth the cost. Or spend time to build your own. You need a field on the Account record which is updated every time you:

  • create a Portfolio record
  • update the value in a Portfolio record
  • delete a Portfolio record
  • re-parent a Partfolio record from one Account to another

The first two are easy enough to do with workflow or javascript on the onSave event on the portfolio. Third can only be done by workflow, not javascript (I think). Last one would need onLoad javascript to store current value of Account lookup so that onSave can compare and then decrement one and increment the other. All four could be done with a plugin.

2
votes

Although this has been answered already, I'll put a second option on the plate for you. Take a look at FormulaManager from North 52 as well. You get a certain amount of Formulas for free so it might be an even more cost effective solution.

Update To add to this, if the field is solely for reporting a value (and doesn't need to be saved to the database) then rather than using a physical field and plugins you could build a Web Resource that executes an Aggregated FetchXml query and simply displays the resulting value.

Again, this is something that I know Formula Manager does out of the box. Have never used Auto Summary.