Thursday, February 27, 2014

Filtering records in the sub grid in Microsoft Dynamics CRM 2011 and CRM 2013

function updateSubGrid() {
debugger
    //This will get the related products grid details and store in a variable.

    var relatedProducts = document.getElementById("AllActivities");
    //Initializing the lookup field to store in an array.

    var lookupfield = new Array;



    //Get the lookup field

    lookupfield = Xrm.Page.getAttribute("to").getValue();



    //This will get the lookup field guid if there is value present in the lookup

    if (lookupfield != null) {

        var lookupid = lookupfield[0].id;

    }



    //Else the function will return and no code will be executed.

    else {

        return;

    }

     

    //This method is to ensure that grid is loaded before processing.

    if (relatedProducts ==null || relatedProducts.readyState != "complete")

    {



        //This statement is used to wait for 2 seconds and recall the function until the grid is loaded.

        setTimeout('updateSubGrid()', 2000);

        return;

    }  

    //This is the fetch xml code which will retrieve all the order products related to the order selected for the case.

    var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";

    fetchXml += "<entity name='activitypointer'>";

    fetchXml += "<attribute name='activitytypecode' />";

    fetchXml += "<attribute name='subject' />";

    fetchXml += "<attribute name='prioritycode' />";

    fetchXml += "<attribute name='activityid' />";

    fetchXml += "<attribute name='instancetypecode' />";

    fetchXml += "<attribute name='regardingobjectid' />";

fetchXml += "<attribute name='scheduledstart' />";

fetchXml += "<attribute name='scheduledend' />";

    fetchXml += "<filter type='and'>";

    fetchXml += "<condition attribute='salesorderid' operator='eq'  value='" + lookupid + "' />";

    fetchXml += "</filter>";

    fetchXml += "</entity>";

    fetchXml += "</fetch>";

     

    //Setting the fetch xml to the sub grid.

    relatedProducts.control.SetParameter("fetchXml", fetchXml);



    //This statement will refresh the sub grid after making all modifications.

    relatedProducts.control.refresh();

}   

Monday, February 3, 2014

Create View in CRM using coding.

 var fetchxml = "<fetch distinct='true' mapping='logical' output-format='xml-platform' version='1.0'><entity name='account'><attribute name='name'/><attribute name='primarycontactid'/><attribute name='telephone1'/><attribute name='accountid'/><order descending='false' attribute='name'/><link-entity name='incident' alias='ac' link-type='outer' to='accountid' from='customerid'/><filter type='and'><condition attribute='customerid' operator='null' entityname='incident'/><condition attribute='statecode' operator='eq' value='0'/></filter></entity></fetch>";

            var savedQuery = new SavedQuery()
            {
                Name = "Acount with no Cases",
                ReturnedTypeCode = "account",
                FetchXml = fetchxml,

                QueryType = 0
            };
            _service.Create(savedQuery);