Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

I want to calculate some values from subgrids in a custom entity with javascripts, but I'm not really familiar with it.

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hi,

As I said, I have a custom entity showing subgrids with views displaying opportunities. I want to count some of the rows of these views and do various operations on them. But the code I wrote isn't working the fields remain equal to 0.

Here is my code, can you help me make it work please?

function UpdateKpi(executionContext) {
var formContext = executionContext.getFormContext();
if(formContext.getAttribute("statecode").getValue() == 0){ //check if the record is active
var kpi1 = formContext.getControl("kpi1");//correspond to the names of the list displaying KPI views
var kpi2 = formContext.getControl("kpi2");
var kpi3 = formContext.getControl("kpi3");
var kpi4 = formContext.getControl("kpi4");
var kpi5 = formContext.getControl("kpi5");

var nb_kpi1 = formContext.getAttribute("rta_offresoombudgetencours");//get the attributes of various fields.
var nb_kpi2 = formContext.getAttribute("rta_offresfermesencours");
var nb_kpi3 = formContext.getAttribute("rta_offresfermesmises");
var k_kpi3 = formContext.getAttribute("rta_offresfermesmisesk");
var nb_kpi4 = formContext.getAttribute("rta_dlaideremiseoffresferme");
var nb_kpi5 = formContext.getAttribute("rta_offresfermesacceptes");
var k_kpi5 = formContext.getAttribute("rta_offresfermesacceptesk");

var debmoi = formContext.getAttribute("rta_dbutdumois");//beginning of a month
var finmoi = formContext.getAttribute("rta_findumoi");//end of a month

if (kpi1 == null || kpi2 == null || kpi3 == null || kpi4 == null || kpi5 == null) { //make sure the grid has loaded
setTimeout(function () { UpdateKpi(); }, 2000); //if the grid hasn't loaded run this again when it has
return;
}

nb_kpi1.setValue(kpi1.getGrid().getTotalRecordCount());// nb_kpi1 = number of rows in the view contained by the grid
nb_kpi2.setValue(kpi2.getGrid().getTotalRecordCount());

var nbrow = kpi4.getGrid().getTotalRecordCount();//nbrow= nb of rows in the 4th grid
if (nbrow == 0){
nb_kpi4.setValue(0); //if nbrow == 0 , nb_kpi4 =0
}else{
var sum = 0;
var cnt = nbrow;
for( var i = 0; i < nbrow;i++){
if ( kpi4.getGrid().getRows().get(i).getData().getEntity().attributes.get("rta_loofferduration").getValue() ===""){//if lo offer duration empty cnt diminishes
cnt--;
}else{//if lo offer duration of a row is not empty, sum increases
sum += parseInt(kpi4.getGrid().getRows().get(i).getData().getEntity().attributes.get("rta_loofferduration").getValue());
}
}
nb_kpi4.setValue(sum/cnt);//nb kpi4 = sum/cnt
}

//KPI 3
var nbrow1 = kpi3.getGrid().getTotalRecordCount();
var nombre_row = 0;
var sum1 = 0;
for( var i = 0; i < nbrow1;i++){
var parts = kpi3.getGrid().getRows().get(i).getData().getEntity().attributes.get("rta_biddate").getValue().split('/');
var mydate = new Date(parts[2],parts[1]-1,parts[0]);
if(mydate > debmoi.getValue() && mydate < finmoi.getValue()){
sum1 += parseInt(kpi3.getGrid().getRows().get(i).getData().getEntity().attributes.get("estimatedvalue").getValue().toString().replace(/\s/g,''));
nombre_row++;
}
}
if (nombre_row == 0){
k_kpi3.setValue(0);
}else{
k_kpi3.setValue(sum1);
}
nb_kpi3.setValue(nombre_row);
// KPI 5
var nbrow2 = kpi5.getGrid().getTotalRecordCount();
var nombre_row1 = 0;
var sum2 = 0;
for( var i = 0; i < nbrow2;i++){// cette boucle parcourt toutes les lignes du tableau kpi 5
var parts = kpi5.getGrid().getRows().get(i).getData().getEntity().attributes.get("rta_porderdate").getValue().split('/');//part = une entrée du tableau à la fois
var mydate = new Date(parts[2],parts[1]-1,parts[0]);// mydate = la date du l'entrée sélectionnée par le pointeur
if(mydate > debmoi.getValue() && mydate < finmoi.getValue()){ //si la date est plus grande que le debut du mois et plus petite que la fin du mois entre dans la cond
sum2 += parseInt(kpi5.getGrid().getRows().get(i).getData().getEntity().attributes.get("estimatedvalue").getValue().toString().replace(/\s/g,''));
nombre_row1++;
}
}
if (nombre_row1 == 0){
k_kpi5.setValue(0);
}else{
k_kpi5.setValue(sum2);
}
nb_kpi5.setValue(nombre_row1);

}
}

*This post is locked for comments

  • Suggested answer
    Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: I want to calculate some values from subgrids in a custom entity with javascripts, but I'm not really familiar with it.

    Hello,

    1. To calculate values from the child entity, have you considered using rollup fields (technet.microsoft.com/.../dn832162.aspx)

    2. To calculate values, you can query related entity using Web API instead of waiting for the subgrid to load, as all the data comming from the related entity only, to write web api you can use rest builder (github.com/.../CRMRESTBuilder)

    You can look sample code here as well: himbap.com/blog

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,401 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans