locked
getting Lookup field Text - JavaScript RRS feed

  • Question

  • Created new Entity new_Country  and included it as a Lookup to Account Entity.

    With the following Onload JavaScript I am able to provide default Value to the new lookup field Country in account form.

    var value = new Array();

    value[0] = new Object();

    value[0].id = '{611F2E13-E545-E011-8C99-F4CE46F35CC7}';

    value[0].name = 'United Kingdom';

    value[0].typename = 'new_country';

     

    Xrm.Page.getAttribute("Country").setValue(value);

     

     

    OR

     

    Xrm.Page.getAttribute("Country").setValue([{ id: '{611F2E13-E545-E011-8C99-F4CE46F35CC7}', name: 'United Kingdom', typename: 'new_country'}]);

     

     

    Which is working fine ;-)

     

    Problem is I would like to get the New Country Lookup Text which is 'United Kingdom'

    to out of the box address1_country field, on change event of new lookup Country.

     

    How to get the Lookup Text using Java Script?


    Hari Prabhu
    Friday, March 4, 2011 4:42 PM

Answers

All replies

  • //Here you go!!
    var lookupItem = new Array;
    
    // This gets the lookup for the attribute primarycontactid on the Account form.
    lookupItem = crmForm.all.primarycontactid.DataValue;
    
    // If there is data in the field, show it in a series of alerts.
    if (lookupItem[0] != null)
    {
      // The text value of the lookup.
      alert(lookupItem[0].name);
    
      // The GUID of the lookup.
      alert(lookupItem[0].id);
    
      // The entity type name.
      alert(lookupItem[0].typename);
    
    }

    Cheers,

    Jamie Miley

    Friday, March 4, 2011 4:47 PM
    Moderator
  • Sorry, that was a 4.0 example, but in 2011 it's pretty similiar http://social.microsoft.com/Forums/en/crm2011beta/thread/27549749-f018-448c-a70a-8223a8a8f096
    Jamie Miley
    Friday, March 4, 2011 4:50 PM
    Moderator
  • Works Perfectly well Thanks. Can you please help converting following code to CRM 2011.

    //------------------------------FUNCTIONS-------------------------

    function htmlEncode(source, display, tabs) {
        function special(source) {
            var result = '';
            for (var i = 0; i < source.length; i++) {
                var c = source.charAt(i);
                if (c < ' ' || c > '~') {
                    c = '&#' + c.charCodeAt() + ';';
                }
                result += c;
            }
            return result;
        }

        function format(source) {
            // Use only integer part of tabs, and default to 4
            tabs = (tabs >= 0) ? Math.floor(tabs) : 4;
            // split along line breaks
            var lines = source.split(/\r\n|\r|\n/);

            // expand tabs
            for (var i = 0; i < lines.length; i++) {
                var line = lines[i];
                var newLine = '';
                for (var p = 0; p < line.length; p++) {
                    var c = line.charAt(p);
                    if (c === '\t') {
                        var spaces = tabs - (newLine.length % tabs);
                        for (var s = 0; s < spaces; s++) {
                            newLine += ' ';
                        }
                    }
                    else {
                        newLine += c;
                    }
                }
                // Leading or ending spaces will be removed from a HTML Request, unless flagged as a nbsp type character
                newLine = newLine.replace(/(^ )|( $)/g, '&nbsp;');
                lines[i] = newLine;
            }
            // re-join lines
            var result = lines.join('<br />');
            // break up contiguous blocks of spaces with non-breaking spaces
            result = result.replace(/  /g, ' &nbsp;');

            return result;
        }
        var result = source;

        // ampersands (&)
        result = result.replace(/\&/g, '&amp;');
        // less-thans (<)
        result = result.replace(/\</g, '&lt;');
        // greater-thans (>)
        result = result.replace(/\>/g, '&gt;');

        if (display) {
            // format for display
            result = format(result);
        }
        else {
            // Replace quotes if it isn't for display,
            // since it's probably going in an html attribute.
            result = result.replace(new RegExp('"', 'g'), '&quot;');
        }

        result = special(result);
        return result;
    }

    FetchXmlResponse = function(fetchXml) {
        var xml =
            "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
            "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
                GenerateAuthenticationHeader() +
                "<soap:Body>" +
                    "<Fetch xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
                        "<fetchXml>" + htmlEncode(fetchXml) + "</fetchXml>" +
                    "</Fetch>" +
                "</soap:Body>" +
            "</soap:Envelope>";

        var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
        xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Fetch");
        xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        xmlHttpRequest.setRequestHeader("Content-Length", xml.length);

        xmlHttpRequest.send(xml);

        return xmlHttpRequest.responseXML;
    }

    FetchPriceList = function() {
        var productId;
        var assume = true;

        var fetchXml =
                "<fetch mapping=\"logical\">" +
                    "<entity name=\"pricelevel\">" +
                        "<attribute name=\"name\" />" +
                        "<attribute name=\"pricelevelid\" />" +
                        "<filter>" +
                            "<condition attribute=\"statecode\" operator=\"eq\" value=\"" + "0" + "\" />" +
                        "</filter>" +
                    "</entity>" +
                "</fetch>";

        if (assume == true) {
            var resultXml = FetchXmlResponse(fetchXml);

            var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            oXmlDoc.async = false;
            oXmlDoc.loadXML(resultXml.text);

            var pricelevel_name = oXmlDoc.getElementsByTagName('name');
            var pricelevel_pricelevelid = oXmlDoc.getElementsByTagName('pricelevelid');

            if (pricelevel_pricelevelid != null) {
                var lookupData = new Array();
                var lookupItem = new Object();

                // Set the id, typename, and name properties to the object.
                lookupItem.id = pricelevel_pricelevelid[0].text;
                lookupItem.typename = 'pricelevel';
                lookupItem.name = pricelevel_name[0].text;
                lookupData[0] = lookupItem;
                crmForm.all.pricelevelid.DataValue = lookupData;
            }
        }
    }

    //-------------------------------------MAIN-------------------------------

    if (( crmForm.FormType == 1 ) && (crmForm.all.pricelevelid.DataValue == null))
    {
      FetchPriceList();
    }

    Reference Link - http://crmconsultancy.wordpress.com/tailoring-the-product-selection-for-opportunities-and-quotes-in-dynamics-crm/1-defaulting-the-opportunity-price-list/


    Hari Prabhu
    Friday, March 4, 2011 10:57 PM
  • There's an app for that.  :)

    http://crm2011scriptconvert.codeplex.com/

     


    Jamie Miley
    Saturday, March 5, 2011 1:56 AM
    Moderator
  • Yes that did the Magic, Thank you very much.

    It may be basic but just to get experts sugession, in CRM2011 will the following code be the same as it was in CRM 4

    <soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">

     

    <Fetch xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">

    var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
    xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Fetch");

     


    Hari Prabhu
    • Marked as answer by Hari Prabhu Sunday, March 6, 2011 12:56 PM
    Saturday, March 5, 2011 8:53 AM