locked
Fetching Data from Another Entity (Lookup) - Done in 4.0 and needs to be done in 2013 RRS feed

  • Question

  • We have a situation where we need to grab associated fields from a filtered lookup.  We're willing to use javascript in conjunction with with either odata, soap or fetchxml.  The goal is to bring in numbers from a custom entity and then fill in fields to do mathematical calculations.

     

    Basically on change of a lookup, you grab data, put it on the form, then perform the calculations.

     

    Here is how the code used to work.  How would you achieve the objective in 2013?

     

     

    function Literature1_onchange()

    {

    if (crmForm.all.ftgbase_literature1id.DataValue != null)

    {

       crmForm.all.ftgbase_quantity1.DataValue = 11;

       crmForm.all.ftgbase_total1.DataValue = null;

     

       var idToFetch = crmForm.all.ftgbase_literature1id.DataValue[0].id+

    "" ;

     

       //Create AJAX request

       var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

       xmlhttp.onreadystatechange = function() {

           //A value of 4 indicates that the response has been returned

    successfully

           if (xmlhttp.readyState == 4) {

               strXML = xmlhttp.responseText;

               var bundleQty = ReturnValueFromXML

    (strXML,'BundleQuantity');

               var maxQty = ReturnValueFromXML(strXML,'MaxOrderQty');

     

               if (maxQty != null && maxQty != "null"){

                   crmForm.all.ftgbase_litmax1.DataValue = parseInt

    (maxQty) ;

               }else{

                   crmForm.all.ftgbase_litmax1.DataValue = null;

               }

              

               if (bundleQty != null && bundleQty != "null"){

                   crmForm.all.ftgbase_bundlequantity1.DataValue =

    parseInt(bundleQty) ;

               }else{

                   crmForm.all.ftgbase_bundlequantity1.DataValue = null;

               }

     

           }

       }

     

       GetRecordXML(xmlhttp, UserInfo._orgId, 'GetLiterature', idToFetch,

    'GetLiterature', idToFetch );

    }

    else

    {

       crmForm.all.ftgbase_bundlequantity1.DataValue = null;

       crmForm.all.ftgbase_total1.DataValue = null;

       crmForm.all.ftgbase_quantity1.DataValue = 11;

    }

    }

     

    We're willing to either get a method working similar to this or go another route, but not a fetchxml grid, we need to preserve historical data.

     

    Thursday, February 5, 2015 11:52 PM

All replies

  • Hi,

    To Retrieve from other entity and populate the fields, for example on entering the account name in opportunity form the fields from account form like Sector, Region can be retrieved and can be auto populated in opportunity form using Odata as follows.

    Call the function getAccountDetails in onchange event of Companyname. Do not forget to add Jquery and Json in libraries.

           

    function getAccountDetails() {
        var companyName = Xrm.Page.getAttribute("customerid").getValue();
        if ((companyName != null)) {
            var companyNameValue = companyName[0].name;
            var companyNameID = companyName[0].id;

            var serverUrl = Xrm.Page.context.getServerUrl();
            //The XRM OData end-point
            var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";


            var odataSetName = "AccountSet";

            var odataSelect = serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "(guid'" + companyNameID + "')";

            //alert(odataSelect);

            $.ajax({
                type: "GET",
                contentType: "application/json; charset=utf-8",
                datatype: "json",
                url: odataSelect,
                beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
                success: function (data, textStatus, XmlHttpRequest) {

                    var result_account = data.d;
                    var name;
                    var Id;
                    var entityType;

                    //replace the fields with the fields on your entity
                    Xrm.Page.getAttribute("new_sector").setValue(result_account.new_Sector.Value);
                    Xrm.Page.getAttribute("new_region").setValue(result_account.new_Region.Value);                     
                },
                error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + odataSelect); }
            }
            );
        }
    }

    Also as another option if you donot wat the java script then you can go for a workflow to poplate, please refer this link:  https://www.powerobjects.com/blog/2013/11/25/retrieving-data-from-a-related-entity-crm-2013/

    Reference: link


    Regards, Rekha.J

    Friday, February 6, 2015 6:01 AM