<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MSCRM Blogger &#187; lookup</title>
	<atom:link href="http://mscrmblogger.com/tag/lookup/feed/" rel="self" type="application/rss+xml" />
	<link>http://mscrmblogger.com</link>
	<description>Achieving it all with Microsoft Dynamics CRM™</description>
	<lastBuildDate>Thu, 10 May 2012 20:24:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Lookup data from a related entity (Lookup) using JScript and the XrmServiceToolkit</title>
		<link>http://mscrmblogger.com/2012/05/10/lookup-data-from-a-related-entity-lookup-using-jscript-and-the-xrmservicetoolkit/</link>
		<comments>http://mscrmblogger.com/2012/05/10/lookup-data-from-a-related-entity-lookup-using-jscript-and-the-xrmservicetoolkit/#comments</comments>
		<pubDate>Thu, 10 May 2012 20:22:56 +0000</pubDate>
		<dc:creator>Carlton Colter</dc:creator>
				<category><![CDATA[API-SDK]]></category>
		<category><![CDATA[Extensions]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Web Resources]]></category>
		<category><![CDATA[CodePlex]]></category>
		<category><![CDATA[crm]]></category>
		<category><![CDATA[crm 2011]]></category>
		<category><![CDATA[CRM Online]]></category>
		<category><![CDATA[crm sdk]]></category>
		<category><![CDATA[crm2011]]></category>
		<category><![CDATA[field]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jscript]]></category>
		<category><![CDATA[lookup]]></category>
		<category><![CDATA[Microsoft Dynamics CRM 2011]]></category>
		<category><![CDATA[onchange]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[Web Resource]]></category>
		<category><![CDATA[XrmServiceToolkit]]></category>

		<guid isPermaLink="false">http://mscrmblogger.com/?p=1007</guid>
		<description><![CDATA[Lookup information from a related entity using the XrmServiceToolkit or just the REST endpoint.]]></description>
			<content:encoded><![CDATA[<p>Do you need the value from a related entity, like a lookup?  I had written this for CRM 4 a while back, and people use it, but I occasionally get questions about looking up other values in CRM.  This bit of code will help you find those values on a related object.  Below are two options to do this.  Option 1: using the REST SDK and JSON2, and then Option 2: using Jaimie Ji&#8217;s <a href="http://xrmservicetoolkit.codeplex.com/" target="_blank">XRM Service Toolkit</a>.</p>
<p>Jaimie Ji&#8217;s <a href="http://xrmservicetoolkit.codeplex.com/" target="_blank">XRM Service Toolkit</a> provides a comprehensive set of JScript libraries for interacting with the SOAP and REST SDK through JavaScript.  While Option 1 in this particular use case is fairly simple, when you start needing to do more advanced things, you may want to look at using that kit.  It is fairly comprehensive and he keeps it up to date.</p>
<h1>Option 1: Using the REST SDK and JSON2</h1>
<p>Using the REST SDK is great, and there are some really good examples in the CRM SDK help file.  Now in order to use the below code-block, you need to use JSON.</p>
<p>JSON, or JavaScript Object Notation, is a text format that is used to interchange data.  JSON2.js is a lightweight javascript that convert the strings to javascript objects and vice-versa.  The CRM REST SDK can return data in JSON format.</p>
<p>To get the code for JSON2, you can go <a href="https://github.com/douglascrockford/JSON-js" target="_blank">here</a>.  You will need to embed this code into your javascript file or include json2.js along with the code below.  Without it, you will not be able to parse the data properly, and the code below uses the JSON library in json2.js.</p>
<p>Ok, below we have 3 functions:</p>
<ul>
<li><b>getServerUrl</b> :  which gets the server URL &#8211; taken from the <a href="http://crmrestkit.codeplex.com/" target+"_blank">CrmRestKit</a></li>
<li><b>Lookup_Changed</b> : what you call when a lookup is changed</li>
<li><b>retrieveReqCallBack</b> : the callback function that performs any actions</li>
<li>
</li>
</ul>
<p>You would put Lookup_Changed in the change event of a lookup field and specify the attributes like one of the following:</p>
<ul>
<li>&#8216;contactid&#8217;,'Contact&#8217;,'contactlookup&#8217;</li>
<li>&#8216;contactid&#8217;,'Contact&#8217;,'contactlookup&#8217;,['Telephone1']</li>
<li>&#8216;contactid&#8217;,'Contact&#8217;,'contactlookup&#8217;,['Telephone1','FullName]</li>
</ul>
<p>Then you&#8217;d put your actions inside the retrieveReqCallBack.</p>
<pre name="code" class="javascript">
function getServerUrl() {
    // From CrmRestKit.js
    var localServerUrl = window.location.protocol + &quot;/&quot; + window.location.host;
    var context = parent.Xrm.Page.context;

    if (context.isOutlookClient() &amp;&amp; !context.isOutlookOnline()) {
        return localServerUrl;
    }
    else {
        var crmServerUrl = context.getServerUrl();
        crmServerUrl = crmServerUrl.replace(/^(http|https):\/\/([_a-zA-Z0-9\-\.]+)(:([0-9]{1,5}))?/, localServerUrl);
        crmServerUrl = crmServerUrl.replace(/\/$/, &quot;&quot;);
    }
    return crmServerUrl;
}

function Lookup_Changed(attributeName, entityName, callbackId, columns) {
    var serverUrl = Xrm.Page.context.getServerUrl();
    var ODataPath = serverUrl + &quot;/XRMServices/2011/OrganizationData.svc&quot;;

    var lookup = Xrm.Page.data.entity.attributes.get(attributeName).getValue();
    if (lookup===null) {
        return false;
    }

    var id = lookup[0].id;
    if (id == null) {
        return false;
    }

    var retrieveReq = new XMLHttpRequest();

    var url = ODataPath + &quot;/&quot;+entityName+&quot;Set(guid'&quot; + id + &quot;')&quot;;
    if (columns !== undefined &amp;&amp; columns !== null) {
        url = url + &quot;?$select=&quot; + columns.join(',');
    }

    retrieveReq.open(&quot;GET&quot;, url, true);
    retrieveReq.setRequestHeader(&quot;Accept&quot;, &quot;application/json&quot;);
    retrieveReq.setRequestHeader(&quot;Content-Type&quot;, &quot;application/json; charset=utf-8&quot;);
    retrieveReq.onreadystatechange = function () {
        if (this.readyState == 4 /* complete */) {
            if (this.status == 200) {
                var data;
                var jData;
                jData = JSON.parse(this.responseText);
                if (jData &amp;&amp; jData.d &amp;&amp; jData.d.results &amp;&amp; jData.d.results.length &gt; 0) {
                    data = jData.d.results[0];
                } else if (jData &amp;&amp; jData.d) {
                    data = jData.d;
                }

                if (data == null) {
                    return;
                }
                retrieveReqCallBack(callbackId, data);
            }
        }
    };
    retrieveReq.send();
    return true;
}

function retrieveReqCallBack(calltype, data) {
    var toLookup=function(data) {
        if (data==null || data==&quot;&quot; || (data.Id==null)) {
            return null;
        }

        var result=new Array();
        result[0] = {};
        result[0].id = data.Id;
        result[0].name = data.Name;
        result[0].entityType = data.LogicalName;
        return result;
    }

    switch (calltype) {
        case 'pricelookup':
            Xrm.Page.data.entity.attributes.get(&quot;new_pricecategorylookup&quot;).setValue(toLookup(c.new_pricecategorylookup);
            break;
        case 'contactlookup':
            Xrm.Page.data.entity.attributes.get(&quot;insp_poctelephonenumber&quot;).setValue(c.Telephone1);
            break;
        default:
            break;
    }
}
</pre>
<h1>Option 2: Using the XrmServiceToolkit with similar functions</h1>
<p>Ok, now let&#8217;s assume you are using the <a href="http://xrmservicetoolkit.codeplex.com/" target="_blank">XRM Service Toolkit</a>, and you want to do the same thing, using a Lookup_Changed function and the retrieveReqCallBack function.  Instead of embedding JSON and a method to get the server url, you can use the following:</p>
<pre name="code" class="javascript">
function Lookup_Changed(attributeName, entityName, callbackId, columns) {
    var lookup = Xrm.Page.data.entity.attributes.get(attributeName).getValue();
    if (lookup===null) {
        return false;
    }

    if (lookup[0].id == null) {
        return false;
    }

	XrmServiceToolkit.Rest.Retrieve(
        entityName,
        lookup[0].id,
        columns,
		null,
        function (result) {
            retrieveReqCallBack(callbackId, result);
        },
        function (error) {
            throw error;
        },
		true
    );
}

function retrieveReqCallBack(calltype, data) {
    var toLookup=function(data) {
        if (data==null || data==&quot;&quot; || (data.Id==null)) {
            return null;
        }

        var result=new Array();
        result[0] = {};
        result[0].id = data.Id;
        result[0].name = data.Name;
        result[0].entityType = data.LogicalName;
        return result;
    }

    switch (calltype) {
        case 'pricelookup':
            Xrm.Page.data.entity.attributes.get(&quot;new_pricecategorylookup&quot;).setValue(toLookup(c.new_pricecategorylookup);
            break;
        case 'contactlookup':
            Xrm.Page.data.entity.attributes.get(&quot;insp_poctelephonenumber&quot;).setValue(c.Telephone1);
            break;
        default:
            break;
    }
}
</pre>
<p>The <a href="http://xrmservicetoolkit.codeplex.com/" target="_blank">XRM Service Toolkit</a> does a lot, and I would suggest checking it out.</p>
]]></content:encoded>
			<wfw:commentRss>http://mscrmblogger.com/2012/05/10/lookup-data-from-a-related-entity-lookup-using-jscript-and-the-xrmservicetoolkit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lookup a lookup on another entity using JScript</title>
		<link>http://mscrmblogger.com/2009/10/15/lookup-a-lookup-on-another-entity-using-jscript/</link>
		<comments>http://mscrmblogger.com/2009/10/15/lookup-a-lookup-on-another-entity-using-jscript/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 20:37:32 +0000</pubDate>
		<dc:creator>Carlton Colter</dc:creator>
				<category><![CDATA[Extensions]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Web Resources]]></category>
		<category><![CDATA[crm]]></category>
		<category><![CDATA[crm 4]]></category>
		<category><![CDATA[crm4]]></category>
		<category><![CDATA[jscript]]></category>
		<category><![CDATA[lookup]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[microsoft dynamics CRM 4]]></category>

		<guid isPermaLink="false">http://mscrmblogger.com/?p=188</guid>
		<description><![CDATA[Do you need the value of a lookup?  I have written this bit of code to look a lookup value stored on another entity.  Like finding the Customer lookup on an Incident (Case).  Here is a bit of code that can lookup a lookup on any entity in CRM.]]></description>
			<content:encoded><![CDATA[<p>Do you need the value of a lookup?  I have written this bit of code to look a lookup value stored on another entity.  Like finding the Customer lookup on an Incident (Case).</p>
<ul>
<li>ID: The unique id value of the entity&#8217;s object</li>
<li>ENTITY: The name of the entity to lookup</li>
<li>IDFIELD: The entity&#8217;s primary id field name, like accountid, incidentid, etc.</li>
<li>LOOKUPFIELD: The name of the field you want to lookup, like customerid</li>
<li>RETURNTYPE: The name of the entity&#8217;s return type, like contact or account.  If there are multiple possible, then specify &#8220;multiple&#8221; and it will pass back the type instead of type name.  If you specify &#8220;value&#8221; it will return just the value.</li>
</ul>
<hr />
<pre name="code" class="javascript">
function getEntityLookup(id, entity, idfield, lookupfield, returntype) {
    var xml = &quot;&quot; +
	&quot;&lt; ?xml version='1.0' encoding='utf-8'?&gt;&quot; +
	&quot;&lt;soap :Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'&quot; +
	&quot; xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'&quot; +
	&quot; xmlns:xsd='http://www.w3.org/2001/XMLSchema'&gt;&quot; +
	GenerateAuthenticationHeader() +
	&quot;&lt;/soap&gt;&lt;soap :Body&gt;&quot; +
	&quot;&lt;fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'&gt;&quot; +
	&quot;&lt;fetchxml&gt;&quot; +
	&quot; &amp;lt;fetch mapping='logical' count='1'&amp;gt;&quot; +
	&quot; &amp;lt;entity name='&quot; + entity + &quot;'&amp;gt;&quot; +
	&quot; &amp;lt;attribute name='&quot; + lookupfield + &quot;' /&amp;gt;&quot; +
	&quot; &amp;lt;filter&amp;gt;&quot; +
	&quot; &amp;lt;condition attribute='&quot; + idfield + &quot;' operator='eq' value='&quot; + id + &quot;' /&amp;gt;&quot; +
	&quot; &amp;lt;/filter&amp;gt;&quot; +
	&quot; &amp;lt;/entity&amp;gt;&quot; +
	&quot; &amp;lt;/fetch&amp;gt;&quot; +
	&quot;&lt;/fetchxml&gt;&quot; +
	&quot;&lt;/fetch&gt;&quot; +
	&quot;&lt;/soap&gt;&quot; +
	&quot;&quot;;

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

    var resultXml = xmlHttpRequest.responseXML;
    var resultSet = resultXml.text;
    resultSet.replace('&amp;lt;', '&lt; ');
    resultSet.replace('&amp;gt;', '&gt;');

    var oXmlDoc = new ActiveXObject(&quot;Microsoft.XMLDOM&quot;);
    oXmlDoc.async = false;
    oXmlDoc.loadXML(resultSet);

    var result = oXmlDoc.getElementsByTagName(lookupfield);
    if (returntype == 'value') return result[0].text;

    var lookupItem = new Object();
    for (var i = 0; i &lt; result[0].attributes.length; i++) {
        var att = result[0].attributes[i];
        if (returntype == 'multiple' &amp;&amp; att.name == &quot;type&quot;) lookupItem.type = att.value;
        if (att.name == &quot;name&quot;) lookupItem.name = att.value;
    }
    if (returntype != 'multiple') lookupItem.typename = returntype;

    lookupItem.id = result[0].text;

    return lookupItem;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://mscrmblogger.com/2009/10/15/lookup-a-lookup-on-another-entity-using-jscript/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

