Monday, December 26, 2011

CRM2011 Subgrids and JavaScript

I have been thinking of bloging about this topic from last 6 months. I wanted to get an aggregate of numeric/money field in the grid. It is very easy to do, if subgrid contains only one page. But How would you get the aggregate if subgrid has more than 1 pages.

I posted this question on the forums and I never get an answer. I still don't have a solution for this problem.
Anyway....

The following sample code displaying the message box with the aggregate of “Estimated Revenue” field  of the opportunity.ce
function subGridOnload() 
{ 
 //debugger;
 var grid = Xrm.Page.ui.controls.get('Opps')._control;
 var sum =0.00;
 //if  (grid.readyState!="complete") 
 //{ 
 // delay one second and try again. 
 //setTimeout(subGridOnload, 1000); 
 //return; 
 //} 

 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('estimatedvalue', ids[i]) !="")
     { 
       cellValue = grid.get_innerControl().getCellValue('estimatedvalue', ids[i]); 
       cellValue = cellValue.substring(2); 
       cellValue = parseFloat(cellValue);  
       sum = sum + cellValue;
     }
 
 }//end for   
 alert(sum);
}
I also noticed that  grid.readyState!="complete" does not work all the time.  It happens when the control is already loaded but the grid is still retrieving the data. So I changed the code to check if the records are already loaded.
See you guys……

7 comments:

  1. Hi,

    Thanks for the Post

    Can you please explain what you mean by this lines?

    "subgrid contains only one page. But How would you get the aggregate if subgrid has more than 1 pages."

    I don't exactly get with "Subgrids contain" more than one Pages?

    ReplyDelete
    Replies
    1. It means the code will you the aggregate of records on the current page. If your subgrid contain multiple pages. It will still give the aggregate of the current page. It won't consider the records on page 2, 3 and so on.
      Regards,

      Delete
    2. I am having this same problem! If anyone comes up with a solution, can you please post it and let me know? Thanks!

      Delete
  2. Hi,subgrid might be having relation ship with parent entity.write a java script function to retrive all related records data of current entity and you can do what ever you want

    ReplyDelete
  3. Of course you can do that. I wanted to do this without making a web service call.
    The other reason to use sub grid was that I can change the view of the sub grid and it would give me the results based on the selected view.

    Thanks mate.

    ReplyDelete
  4. Hello,
    I have a subgrid that displays data of the two related entities. I use the getCellValue method to retrieve the appropriate value. I don't have any problem with the field wich is the part of the entity itself. Once I extract the value of the field belonging to the related one, the getCellValue returns null. Is there any special naming convention for related etities? Where can I see the documentation for the grid methods at all?

    TY

    ReplyDelete