Tuesday 22 September 2015

Get subgrid data from the database using javascript

Get subgrid data from the database using javascript


                Previously I shared how to retrieve Subgrid data from UI. But in CRM the subgrid values may vary from user to user. So if we want all values from that particular entity then we should get the data from the database. For that, we need to generate FetchXML.
Here I am getting all Competitors(Competitor is a subgrid) from related opportunity entity.

Step1:   We need to add javascript files from "XrmServiceToolkit".
Here you can download XrmServiceToolkit: https://xrmservicetoolkit.codeplex.com/



Step2:   Now go to Sales --> Opportunity Entity--> Advance Finder-->Write your own query here. I attached sample query for getting all competitors related to the current entity.



Step3:   Download the fetchXML.

Step4:   Change the format of fetchXML.

Step5:   Start writing Javascript. Here I add my custom javascript code.

function RetrieveSubgridRecord() {
       var Xmlsample =
    "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
    "<entity name='competitor'>" +
    "<attribute name='name' />" +
   "<attribute name='websiteurl' />" +
   "<attribute name='competitorid' />" +
   "<order attribute='name' descending='false' />" +
   "<link-entity name='opportunitycompetitors' from='competitorid' to='competitorid' visible='false' intersect='true'>" +
   "<link-entity name='opportunity' from='opportunityid' to='opportunityid' alias='am'>" +
    "<filter type='and'>" +
      "<condition attribute='opportunityid' operator='eq' uitype='opportunity' value='" + Xrm.Page.data.entity.getId() + "' />" +        //Xrm.Page.data.entity.getId() gives current entity ID
    "</filter>" +
  "</link-entity>" +
"</link-entity>" +
"</entity>" +
"</fetch>"
    var Records = XrmServiceToolkit.Soap.Fetch(Xmlsample);
alert(Records.length);
//Here you can write your own logic based on your requirement.

 Thanks for reading this.Hope it helps you.

Wednesday 9 September 2015

Get Subgrid data using javascript


Get Subgrid data using javascript

Forgetting subgrid values from your entity, you can use the following javascript

function RetrieveSubgridRecord() {
   if (document.getElementById("Competitors")) {     //Competitors is logical name of my grid
        var grid = document.getElementById("Competitors").control;
        for (var rowNo = 0; rowNo <= grid.GetRecordsFromInnerGrid().length; rowNo++)
            for (var cellNo = 0; cellNo <= grid.GetRecordsFromInnerGrid()[rowNo][1].length; cellNo++)
                  alert(grid.GetRecordsFromInnerGrid()[rowNo][3].cells[cellNo].outerText);
  }
}

This is the way to getting subgrid values from local entity. From getting subgrid data from database you need to download FetchXML from your advance find...
I will upload that soon

Hope this helps you



Wednesday 2 September 2015

Important javascript Functions for CRM

Important javascript Functions for CRM


Getting the value of optionset


Xrm.Page.getAttribute("field logical name").getValue();

Getting the Text of optionset

Xrm.Page.getAttribute("fieldlogical name").getText();

Getting the GUID of Lookup


Xrm.Page.getAttribute("fieldlogical name").getValue()[0].id;

Getting the Text of Lookup


Xrm.Page.getAttribute("fieldlogical name ").getValue()[0].name;

Getting the value of single line text


Xrm.Page.getAttribute("fieldlogical name").getValue();

Setting value to the field


Xrm.Page.getAttribute("fieldlogicalname").setValue("Value we want to set");

Getting Entity Name


Xrm.Page.data.entity.getEntityName();

For refresh the Form


Location.Reload();

Save the page

Xrm.Page.data.entity.save();