Wednesday, 22 May 2019

Retrieve Local Option set metadata in java script using webapi, Dynamic 365

   function GetOptionSetOptionValue(optionText, optionSetLogicalName)
{
        debugger;
        var req = new XMLHttpRequest();
        req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/EntityDefinitions(LogicalName='ab_bid')/Attributes/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$filter=LogicalName eq '" + optionSetLogicalName + "' &$expand=OptionSet", false);
        req.setRequestHeader("OData-MaxVersion", "4.0");
        req.setRequestHeader("OData-Version", "4.0");
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
        req.send(null);
        if (req.readyState === 4) {
            req.onreadystatechange = null;
            if (req.status === 200) {
                var results = JSON.parse(req.response);
                debugger;
                for (var i = 0; i < results.value[0].OptionSet.Options.length; i++) {
                    debugger;
                    var Label = results.value[0].OptionSet.Options[i].Label.UserLocalizedLabel.Label;
                    if (Label.trim().toLowerCase() == optionText.trim().toLowerCase()) {
                        return results.value[0].OptionSet.Options[i].Value;
                    }
                    //alert(Label);
                }
            } else {
                Xrm.Utility.alertDialog(req.statusText);
            }
        }
    }

Thursday, 16 May 2019

Retrieve note from opportunity and associate with custom entity note in C#



private void RetrieveRelatedNoteFromOppAndAttachWithBid(IOrganizationService orgService, Guid oppId, Guid bidId)
        {
            try
            {
                string fetchxml = @"<fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'>
                                    <entity name='annotation'>
                                    <attribute name='subject'/>
                                    <attribute name='notetext'/>
                                    <attribute name='filename'/>
                                    <attribute name='annotationid'/>
                                    <attribute name='isdocument'/>
                                    <attribute name='filesize'/>
                                    <attribute name='mimetype' />
                                    <attribute name='documentbody'/>
                                    <order descending='true' attribute='createdon'/>
                                    <filter type='and'>
                                       <condition attribute='objectid' operator='eq' value='" + oppId + @"'/>
                                    </filter>
                                    </entity>
                                    </fetch>";
                EntityCollection results = orgService.RetrieveMultiple(new FetchExpression(fetchxml));
                if (results.Entities.Count > 0)
                {
                    Entity noteOpp = results.Entities[0];
                    Entity noteBid = new Entity("annotation");
                    noteBid["objectid"] = new EntityReference("ab_bid", bidId);
                    noteBid["objecttypecode"] = "ab_bid";
                    noteBid["subject"] = noteOpp["subject"];
                    noteBid["documentbody"] = noteOpp["documentbody"];
                    noteBid["mimetype"] = noteOpp["mimetype"];
                    noteBid["filename"] = noteOpp["filename"];

                    orgService.Create(noteBid);
                    orgService.Delete(noteOpp.LogicalName, noteOpp.Id);
                }
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(ex.Message);
            }
        }

Monday, 22 April 2019

Fetch Xml query in webapi Javascript Dynamic 365

    try {

    var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical'                                                              distinct='false'>";
fetchXml += "<entity name='opportunity'>";
fetchXml += "<attribute name='budgetamount' />";
fetchXml += "<attribute name='statuscode' />";
fetchXml += "<attribute name='opportunityid' />";
fetchXml += "<attribute name='name' />";
fetchXml += "<attribute name='createdby' />";
fetchXml += "<attribute name='ss_requesttype' />";
fetchXml += "<order attribute='name' descending='false' />";
fetchXml += "<filter type='and'>";
fetchXml += "<condition attribute='opportunityid' operator='in'>";
for (var i = 0; i < arrayVals.length; i++) {
var oppId = arrayVals[i].replace("{", "").replace("}", "");
fetchXml += "<value uitype='opportunity'>" + oppId + "</value>";
}
fetchXml += "</condition>";
fetchXml += "</filter>";
fetchXml += "</entity>";
fetchXml += "</fetch>";

        var webapiPath = lXrm.Page.context.getClientUrl() + "/api/data/v9.1/opportunities?fetchXml=" + encodeURIComponent(fetchXml);
        var service = new XMLHttpRequest();
        service.open("GET", webapiPath, false);
        service.setRequestHeader("Accept", "application/json");
        service.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        service.setRequestHeader("OData-MaxVersion", "4.0");
        service.setRequestHeader("OData-Version", "4.0");
        service.setRequestHeader("Prefer", "odata.include-annotations=*");
        //service.setRequestHeader("Prefer", "odata.maxpagesize = 3"); this tag isused to retrieve no of //records
        service.send(null);

        if (service.readyState == 4 /* complete */) {
            if (service.status == 201 || service.status == 200) {
                var result = eval('(' + service.responseText + ')');
                if (result.value.length > 0) {
                   
                    for (var i = 0; i < result.value.length; i++) {
                        var opportunityid = result.value[i].opportunityid;
                        var name = result.value[i].name;
                        var budgetamount = result.value[i].budgetamount;
                        var createdby = result.value[i]['_createdby_value@OData.Community.Display.V1.FormattedValue'];
                       
                    }
                   
                }
            }
        }
    } catch (ex) {

    }

Monday, 15 April 2019

Web API request to refresh Rollup field Dynamic 365 through JavaScript

The following JS function is used to refresh Rollup field through webapi fom javascript

function calcRollupField(serverURL,strTargetEntitySetName, strTargetRecordId, strTargetFieldName)
{
    strTargetRecordId = strTargetRecordId.replace("{", "").replace("}", "");
    var req = new XMLHttpRequest();
    req.open("GET", serverURL + "/api/data/v9.0/" +
        "CalculateRollupField(Target=@p1,FieldName=@p2)?" +
        "@p1={'@odata.id':'" + strTargetEntitySetName + "(" + strTargetRecordId + ")'}&" +
        "@p2='" + strTargetFieldName + "'", false);
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.onreadystatechange = function () {
        if (req.readyState === 4) {
            req.onreadystatechange = null;
            if (req.status === 200) {
                var results = JSON.parse(this.response);
            }
            else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send(JSON.stringify({}));
}

Monday, 25 February 2019

Show only Account or Contact in customer lookup dynamic 365


To show only account or contact in customer lookup, follow the below steps

1) On Load of form add the following code

if (Xrm.Page.getControl("customerid") != null) {
            Xrm.Page.getControl("customerid").addPreSearch(function () {
                filterCustomerLookup ();
            });
        }

2)Register this function on OnChange event of customer lookup to show only account in customer lookup

    function filterCustomerLookup () {

        var lookup = Xrm.Page.getControl('customerid');

        //check if multiple type dropdowns enabled for this lookup
        if (lookup.getEntityTypes().length > 1) {
            lookup.setEntityTypes(['account']);
        }

        var fetchXml = "<filter type='and'>";
        fetchXml += "<condition attribute='statecode' operator='eq' value='0'/>";
        fetchXml += "</filter>";

        if (Xrm.Page.getAttribute("customerid") != null) {
            Xrm.Page.getControl("customerid").addCustomFilter(fetchXml, "account");
        }
    }



To show only contact  then change account to contact.

function filterCustomerLookup () {

        var lookup = Xrm.Page.getControl('customerid');

        //check if multiple type dropdowns enabled for this lookup
        if (lookup.getEntityTypes().length > 1) {
            lookup.setEntityTypes(['account']);
        }

        var fetchXml = "<filter type='and'>";
        fetchXml += "<condition attribute='statecode' operator='eq' value='0'/>";
        fetchXml += "</filter>";

        if (Xrm.Page.getAttribute("customerid") != null) {
            Xrm.Page.getControl("customerid").addCustomFilter(fetchXml, "contact");
        }
    }


Tuesday, 13 June 2017

SSRS Custom Report Loaded Partial Images from Notes Entity in Dynamics CRM

For One of Our Client , I have to show images from  Notes entity in custom SSRS report.
I have to show Quote information and related Product information with images in report.


When i use this fetch XML query images display partially.



<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">
  <entity name="quote">
    <attribute name="owneridname" />
    <attribute name="customerid" />
    <attribute name="quotenumber" />
    <attribute name="totalamount" />
    <attribute name="discountpercentage" />
    <attribute name="totallineitemamount" />
    <attribute name="quoteid" />
    <attribute name="modifiedon" />
    <attribute name="new_billtoaddress" />
    <attribute name="new_shiptoaddress" />
    <attribute name="new_termsconditions" />
    <attribute name="new_projectname" />
    <attribute name="new_contactphonecell" />
    <attribute name="new_fax" />
    <attribute name="discountamount" />
    <attribute name="new_properties" />
 <attribute name="new_quoteapprovedbygm" />
 <attribute name="new_salesconsultant" />
    <filter type="and">
      <condition attribute="quoteid" operator="eq"  uitype="quote" value="@QuoteId" />
    </filter>
    <link-entity name="quotedetail" from="quoteid" to="quoteid" alias="am" link-type="outer">
   <attribute name="productid" alias="am_productidname"/>
      <attribute name="priceperunit" alias="am_priceperunit"/>
      <attribute name="quantity" alias="am_quantity"/>
      <attribute name="extendedamount" alias="am_extendedamount"/>
      <attribute name="productdescription" alias="am_proddescription"/>
      <attribute name="new_properties" alias="am_properties"/>
      <attribute name="manualdiscountamount" alias="am_manualdiscountamount"/>
      <attribute name="new_discount" alias="am_discountPercentage"/>
      <link-entity name="product" from="productid" to="productid" alias="an">
        <link-entity name="annotation" from="objectid" to="productid" alias="ao">
            <attribute name="subject" />
            <attribute name="notetext" />
            <attribute name="filename" />
            <attribute name="documentbody" />
            <attribute name="annotationid" />
  </link-entity>
      </link-entity>
    </link-entity>
  </entity>
</fetch>

The issue is with    distinct="true"  make it  distinct="false" the issue resolved

Actually when distinct="true"  image is retrieved as base 64 string like BST25SASDFSDFOSDFSD09FSD9F78SDFPSODFSDF9SDF789SD7F9S8DF7S


distinct remove the repetition which results in almost half string

so the images display partially.



Read only field , get latest value from read only field in in power app portal

Below line is used to make field readonly in power app portal through js $( "#bdo_relationshiprole_name" ).parent().css( "po...