Search code examples
javascriptdynamics-crmdynamics-crm-2013dynamics-crm-online

Subgrid data retrival in crm


I have a sub-grid dynamics 365 crm named competitors I have to get data from that sub-grid and print it into alert. I have searched in the net but every solution I get from there when I used is throwing error as deprecated method. How can I get the sub-grid data in dynamics 365 application using JavaScript. Can anyone provide sample code for that. Thanks in advance


Solution

  • Here is below code I tried in one of my Instance. Considering you want alert on Opportunity entity and that too onload of a form.

    Use Debugger and you will get the data.

    function onLoad(executionContext){
    debugger;
       var formContext = executionContext.getFormContext();   
    var formType= formContext.ui.getFormType();
    if(formType && formType!==1 && formContext.data.entity.getId()){
        retreiveOppCompRecords(formContext.data.entity.getId().replace(/[{}]/g, "")).then(
                function success(result) {              
                 if(result.entities.length > 0){
                     for(i=0;i<result.entities.length;i++){
    
                     }               
                 }               
    
                },
                function (error) {
                    var alertStrings = {
                        confirmButtonLabel: "OK",
                        text: 'Failed at Function: enableDisableExpiryDate with error: ' + error.message
                    };
                    var alertOptions = {
                        height: 250,
                        width: 450
                    };
                    Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
                }
            );
    }
    }
    
    
    function retreiveOppCompRecords(recordId){
        var fetchxml = ['<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">',
      '<entity name="opportunity" >',
        '<attribute name="name" />',
        '<filter type="and" >',
          '<condition attribute="opportunityid" operator="eq" value="' + recordId + '" />',
        '</filter>',
        '<link-entity name="opportunitycompetitors" from="opportunityid" to="opportunityid" alias="OppCompetitor" intersect="true" >',
          '<link-entity name="competitor" from="competitorid" to="competitorid" link-type="inner" alias="Competitor" >',
            '<attribute name="websiteurl" alias="CompetitorWebsite" />',
            '<attribute name="name" alias="CompetitoName" />',
          '</link-entity>',
        '</link-entity>',
      '</entity>',
    '</fetch>'].join("");
    
        fetchxml = "?fetchXml=" + encodeURIComponent(fetchxml); 
        return Xrm.WebApi.retrieveMultipleRecords("opportunity", fetchxml);
    }