Showing posts with label Lookup. Show all posts
Showing posts with label Lookup. Show all posts

24 October 2009

Change the lookup view on Many-to-Many relationship

The N:N lookup view in CRM 4.0 is very basic: it only shows the primary field on the lookup entity. Say if you create a N:N relationship for Account and Contact, the lookup view looks like:


It's not convenient if you want to see more details of the lookup records. So, how can we change it?
How about if we put the standard view(Active Contacts View, Contact Lookup View, etc) on the left panel - Sounds great!

The solution includes two parts:
1. A customized lookupmulti.aspx in the ISV folder;
- Used for generating the lookup view.
2. Jscript code on the referencing entity(which is 'Account' in this case )
- Used for passing parameters to the custom ASPX page;



First of all, create a new folder under the: \CRMWeb\ISV\
Save the below code as a ASPX file in that folder, so you got, for instance: \CRMWeb\ISV\lookup\lookupmulti.aspx


<!--
-- Show entity's standard View in a N:N LookupMulti dialog window.
-- Jim Wang @ October 2009
-- http://jianwang.blogspot.com
-- http://www.mscrm.cn
-->
<html><head><META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=utf-8">
<title>Look Up Records</title>
<script type="text/javascript">
var viewDoc = this;
var _mode = 4;
var IS_PATHBASEDURLS = true;
var ORG_UNIQUE_NAME = window.dialogArguments.split("/")[1];
var iTypeCode = window.dialogArguments.substring(window.dialogArguments.indexOf("etc=") + 4, window.dialogArguments.indexOf("&viewid="));
</script>
<script type="text/javascript" src="/_static/_common/scripts/encodedecode.js"></script>
<script type="text/javascript" src="/_static/_controls/util/util.js"></script>
<script type="text/javascript" src="/_static/_common/scripts/global.js"></script>
<script type="text/javascript" src="/_static/_common/scripts/xmlutil.js"></script>
<script type="text/javascript" src="/_static/_controls/remotecommands/remotecommand.js"></script>
<script type="text/javascript" src="/_common/windowinformation/windowinformation.aspx"></script>
<script type="text/javascript" src="/_static/_controls/lookup/lookupdialogs.js"></script>
<script type="text/javascript" src="/_static/_forms/addrelated.js"></script>
<script type="text/javascript" src="/_static/_common/scripts/details.js"></script>
<script type="text/javascript" src="/_static/_common/scripts/select.js"></script>
<script type="text/javascript" src="/_static/_common/scripts/presence.js"></script>
<script type="text/javascript" src="/_static/_controls/number/number.js"></script>
<script type="text/javascript" src="/_static/_controls/lookup/lookup.js"></script>
<link rel="stylesheet" type="text/css" href="/_common/styles/global.css.aspx?lcid=1033" />
<link rel="stylesheet" type="text/css" href="/_common/styles/global-styles.css.aspx?lcid=1033" />
<link rel="stylesheet" type="text/css" href="/_common/styles/global-dynamic-styles.css.aspx?lcid=1033" />
<link rel="stylesheet" type="text/css" href="/_common/styles/fonts.aspx?lcid=1033" />
<link rel="stylesheet" type="text/css" href="/_forms/controls/form.css.aspx?lcid=1033" />
<link rel="stylesheet" type="text/css" href="/_forms/controls/controls.css.aspx?lcid=1033" />
<link rel="stylesheet" type="text/css" href="/_common/styles/dialogs.css.aspx?lcid=1033" />
<link rel="stylesheet" type="text/css" href="/_controls/lookup/lookupdialogs.css.aspx?lcid=1033" />
<link rel="stylesheet" type="text/css" href="/_common/styles/select.css.aspx?lcid=1033" />
<link rel="stylesheet" type="text/css" href="/_controls/notifications/notifications.css.aspx?lcid=1033" />
</head><body><div style="width:100%; height:100%; overflow:auto"><table style="width:100%; height:100%;" cellspacing="0" cellpadding="0"><tr><td colspan="2" class="ms-crm-Dialog-Header">
<div class="ms-crm-Dialog-Header-Title" id="DlgHdTitle">Look Up Records</div><div class="ms-crm-Dialog-Header-Desc" id="DlgHdDesc">Type the information you are looking for in the Look for box and click Find. Then, select the records you want from the Available records list and move them to the Selected records list.</div></td></tr>
<tr><td colspan="2" style="height:100%;"><div class="ms-crm-Dialog-Main" ><div id="divWarning" style="height:100%;">
<script language="JavaScript">

function initFrame() {
viewDoc = document.getElementById("frmResults").contentWindow.document;
tblResults = viewDoc.getElementById("crmGrid").InnerGrid;
if (tblResults == undefined) {
return;
}
}

function createNew() {
openObj(iTypeCode, null, null);
}

function applychanges() {
window.returnValue = buildReturnValue(tblSelected.rows);
window.close();
}

function cancel() {
window.close();
}

function window.onload() {
if (window.dialogArguments) {
document.getElementById("frmResults").src = window.dialogArguments;
document.getElementById("frmResults").onreadystatechange = resultsReady;
}
else {
alert("No arguments found.");
return;
}
}

function resultsReady() {
if (frmResults.document.readyState == "complete") {
initFrame();
viewDoc.body.scroll = "no";
viewDoc.body.style.padding = "0px";
viewDoc.body.style.border = "1px";
viewDoc.body.firstChild.firstChild.firstChild.firstChild.bgColor = "#E3EFFF";
viewDoc.getElementById("crmMenuBar").parentNode.parentNode.removeNode(true);
viewDoc.getElementById("crmGrid").onpropertychange = function() { setTimeout(onAfterChange, 100); }

setNavigationState();
}
}

function onAfterChange() {
initFrame();
setNavigationState();
}

function removeSelected() {
var items = tblSelected.selectedItems;
for (var i = 0; i < items.length; i++) {
items[i].removeNode(true)
}

items.splice(0, items.length);
if (tblSelected.rows.length > 0) {
selectItem(tblSelected, tblSelected.rows[0], true);
}
setNavigationState();
}

function duplicateSelection(oid) {
var len = tblSelected.rows.length;
for (var i = 0; i < len; i++) {
if (tblSelected.rows[i].oid == oid) {
return true;
}
}
return false;
}

function appendItem(id, type, html, originalItem) {
var tr = tblSelected.insertRow();
tr.oid = id;
tr.otype = type;
tr.originalItem = originalItem;

var td = tr.insertCell();
td.className = "sel";
td.noWrap = true;
td.innerHTML = html;

if (tr.rowIndex == 0) {
selectItem(tblSelected, tr, false);
}
}

function appendSelected() {
initFrame();
var items = tblResults.SelectedRecords;
if (items) {
var len = items.length;
var html = "<TD class=ms-crm-List-DataCell align=middle><IMG style='CURSOR: hand' alt='Click to preview' src='/_imgs/grid/row_selected.gif'> </TD>";

for (var i = 0; i < len; i++) {
var o = items[i];

if (!duplicateSelection(o[0])) {

appendItem(o[0], o[1], o[3].innerHTML.indexOf("row_selected.gif") == -1 ? html + o[3].innerHTML : o[3].innerHTML, null);
}
}
setNavigationState();
}
}

function setNavigationState() {
if (tblResults != undefined) {
if (tblResults.SelectedRecords != null && tblResults.SelectedRecords.length != null && tblResults.SelectedRecords.length > 0) {
btnProperties.disabled = false;
btnAppend.disabled = false;
}
else {
btnProperties.disabled = true;
btnAppend.disabled = true;
}
btnRemove.disabled = (tblSelected.rows.length == 0);
tblNoRecords.runtimeStyle.display = (tblSelected.rows.length == 0 ? "" : "none");
}
}

function showProperties() {
initFrame();
var items = tblResults.SelectedRecords;
if (items == null || items.length == null) {
return;
}
if (items.length == 0) {

alert("You must select one object.");
}
else if (items.length > 1) {
alert("You must only select one object.");
}
else {
var nWidth = 560;
var nHeight = 525;
var oWindowInfo = GetWindowInformation(items[0][1]);
if (oWindowInfo != null) {
nWidth = oWindowInfo.Width;
nHeight = oWindowInfo.Height;
}

switch (Number(items[0][1])) {
case Service:
openStdWin(prependOrgName("/sm/services/readonly.aspx?objTypeCode=" + items[0][1] + "&id=" + items[0][0]), "readonly" + buildWinName(items[0][0]), nWidth, nHeight);
break;
case Workflow:
openObj(items[0][1], items[0][0]);
break;
case ImportMap:
openStdWin(prependOrgName("/tools/managemaps/readonly.aspx?objTypeCode=" + items[0][1] + "&id=" + items[0][0]), "readonly" + buildWinName(items[0][0]), nWidth, nHeight);
break;
default:
openStdWin(prependOrgName("/_forms/readonly/readonly.aspx?objTypeCode=" + items[0][1] + "&id=" + items[0][0]), "readonly" + buildWinName(items[0][0]), nWidth, nHeight);
break;
}
}
}

</script>

<table cellspacing="0" cellpadding="0" width="100%" height="100%">
<td>
<table height="100%" width="100%" id="tblFind" cellpadding="0" cellspacing="0">
<tr height="20">
<td>Available records:</td>
<td></td>
<td>Selected records:</td>
</tr>
<tr>
<td width="45%">

<iframe scrolling="no" class="ms-crm-Dialog-Lookup-Results" id="frmResults" ></iframe>

</td>
<td width="60" align="center">
<button id="btnAppend"disabled="disabled" style="width: 40px;" onclick="appendSelected()" title="Add the selected record">>></button>
<p>
<button id="btnRemove" disabled="disabled" style="width: 40px;" onclick="removeSelected();" title="Remove the selected record"><<</button>
</td>
<td>
<div id="rtnObjList" class="ms-crm-Dialog-Lookup-Objects" onkeydown="listKeyDown(tblSelected)" onfocusin="focusSelectedItems(tblSelected, true);" onfocusout="focusSelectedItems(tblSelected, false);">

<table hidefocus="true" tabindex="0" id="tblSelected" cellpadding="2" cellspacing="0" width="100%" onclick="clickItem( this )" ondblclick="removeSelected()"></table>

<table class="ms-crm-Dialog-Lookup-InlineMsg" id="tblNoRecords">
<tr>
<td class="ms-crm-Dialog-Lookup-InlineMsg" align="center">No records have been selected yet.</td>
</tr>
</table>
</div>
</td>
</tr>
<tr height="20" style="padding-top: 10px;">
<td colspan="3" nowrap>
<button id="btnProperties" disabled="disabled" onclick="showProperties();" class="ms-crm-Button" Title="View the selected record's properties" >Properties</button><span style="width: 5px;"></span>
<button id="btnNew" onclick="createNew();" class="ms-crm-Button" Title="Create a new record">New</button>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div></div>
<div id='divFillBg' style='display:none;position:absolute;top:80px;left:20px;height:23px;width:355px;background-color:#ffffff;'> </div>
<div id='divFill' style='display:none;position:absolute;top:80px;left:20px;height:23px;width:0px;filter: progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr=#00ff00, EndColorStr=#00cc00);'> </div>
<div id='divStatus' style='display:none;position:absolute;top:80px;left:19px;height:23px;width:357px;'><img alt='' src='/_imgs/statusbar.gif' height='23' width='357'></div></td></tr><tr><td class="ms-crm-Dialog-Footer ms-crm-Dialog-Footer-Left"> </td>
<td class="ms-crm-Dialog-Footer ms-crm-Dialog-Footer-Right"><button id="butBegin" onclick="applychanges();" class="ms-crm-Button">OK</button> <button id="cmdDialogCancel" onclick="cancel();" class="ms-crm-Button">Cancel</button></td></tr></table></div>

</body>
</html>



Put the below code into Account.Onload(), you need to replace the: nnId, lookupTypeCode, lookupViewId to yours.


var nnId = "new_account_contact"; // entity N:N relationship id
var lookupTypeCode = 2; // entity type code
var lookupViewId = "A2D479C5-53E3-4C69-ADDD-802327E67A0D"; // the view id of referenced entity

var lookupSrc = "/" + ORG_UNIQUE_NAME + "/ISV/lookup/lookupmulti.aspx";
var lookupArg = "/" + ORG_UNIQUE_NAME + "/_root/homepage.aspx?etc=" + lookupTypeCode +"&viewid=" + lookupViewId;

var lookupEntityTypeCode;
var navId = document.getElementById("nav" + nnId);
if (navId != null)
{
var la = navId.onclick.toString();
la = la.substring(la.indexOf("loadArea"), la.indexOf(";"));

navId.onclick = function()
{
eval(la);

var areaId = document.getElementById("area" + nnId + "Frame");
if(areaId != null)
{
areaId.onreadystatechange = function()
{
if (areaId.readyState == "complete")
{
var frame = frames[window.event.srcElement.id];
var li = frame.document.getElementsByTagName("li");

for (var i = 0; i < li.length; i++)
{
var action = li[i].getAttribute("action");
if(action != null && action.indexOf(relId) > 1)
{
lookupEntityTypeCode = action.substring(action.indexOf("\(")+1, action.indexOf(","));
li[i].onclick = CustomLookup;
break;
}
}
}
}
}
}
}

function CustomLookup()
{
var lookupItems = window.showModalDialog(lookupSrc, lookupArg, "dialogWidth:800px; dialogHeight:600px;");

if (lookupItems) // This is the CRM internal JS funciton on \_static\_grid\action.js
{
if ( lookupItems.items.length > 0 )
{
AssociateObjects( crmFormSubmit.crmFormSubmitObjectType.value, crmFormSubmit.crmFormSubmitId.value, lookupEntityTypeCode, lookupItems, true, null, nnId);
}
}
}


Notice:

04 August 2009

CRM Filtered Lookup Multi

I had some posts last year about the CRM Filtered Lookup, these technique are broadly used in the CRM community.

The mysterious CRM Lookup (I)
The mysterious CRM Lookup (II)
The mysterious CRM Lookup (III)

A few days ago, I saw a post on the Microsoft Dynamics CRM Chinese Forum about how to add filter to LookupMulti.aspx ?
I think it's a very common requirements, so I'd like to give my idea.
When I start with this customization, my bottom line was: Not change any files/databases. However this customization should be marked as a "unsupported customization" (call CRM/JS function directly).


OK, the question was:
A customized entity: ShippingMark (new_shippingmark), it has N:1 relationship with Account; it also has N:N relationship with Quote.
And as we known by default, Quote has N:1 relationship with Account(via customerid)

So the relationship is simple: Account -< (customerid)Quote >< ShippingMark(new_accountid) >- Account

What the user wants was classic: Open a Quote record, then go to Add Existing ShippingMark, then in the LookupMulti page, only return the ShippingMark which has the same Account(new_account) with Quote's(customerid).

There are two parts of the code: server side Plugin.Execute event and client side CRM.Onload event. What the client side code does is: create a custom lookup window, and pass the customerid as a parameter, so the lookup URL looks like: …&id=…, then the server side plugin will replace the FilterXml query string based on the parameter.

I give the code prototype for this specific requirement, you need to modify it for re-use. This technique should work for both LookupSingle.aspx and LookupMulti.aspx.


1. Plug-Ins
Register the Execute message on the Pre Stage/Synchronous/Server/Parent Pipeline.



/*
* Microsoft Dynamics CRM Lookup Filter
* Plug-Ins: Execute message on the Pre Stage/Synchronous/Server/Parent Pipeline.
* Jim Wang @ Aug 2009, http://jianwang.blogspot.com, http://mscrm.cn
*
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;
using Microsoft.Crm.Sdk;

namespace CRMExecuteEvent
{
public class CRMExecuteEvent : IPlugin
{
string lookupId;

public void Execute(IPluginExecutionContext context)
{

lookupId = HttpContext.Current.Request.QueryString["id"] == null ? null : HttpContext.Current.Request.QueryString["id"].ToString();

if (lookupId == null) return;

try
{
if (context.InputParameters.Contains("FetchXml"))
{
string beforeXml = (String)context.InputParameters["FetchXml"];

if (beforeXml.Contains("<entity name=\"new_shippingmark\">") && beforeXml.Contains("xml-platform"))
{
//Customise the FetchXml query string
string afterXml =
"<fetch version='1.0' page='1' count='100' output-format='xml-platform' mapping='logical'> " +
"<entity name='new_shippingmark'> " +
"<attribute name='new_shippingmarkid' /> " +
"<attribute name='new_name' /> " +
"<attribute name='createdon' /> " +
"<order attribute='new_name' /> " +
"<link-entity name='quote' to='new_accountid' from='customerid'> " +
"<filter type='and'> " +
"<condition attribute = 'customerid' operator='eq' value='" + lookupId + "'/> " +
"</filter> " +
"</link-entity> " +
"<filter type='and'> " +
"<condition attribute='statecode' operator='eq' value='0' /> " +
"<condition attribute='new_name' operator='like' value='%' /> " +
"</filter> " +
"</entity> " +
"</fetch>";

//Replace the FetchXml query string
context.InputParameters["FetchXml"] = beforeXml.Replace(beforeXml, afterXml);

}
}
}

catch (System.Web.Services.Protocols.SoapException ex)
{
throw new InvalidPluginExecutionException("An error occurred in the CRM plug-in.", ex);
}
}

}
}




2. Quote.OnLoad()


var relId = "new_new_shippingmark_quote";
var lookupId = crmForm.all.customerid;

var lookupEntityTypeCode;
var navId = document.getElementById("nav" + relId);
if (navId != null)
{
var la = navId.onclick.toString();
la = la.substring(la.indexOf("loadArea"), la.indexOf(";"));

navId.onclick = function()
{
eval(la);

var areaId = document.getElementById("area" + relId + "Frame");
if(areaId != null)
{
areaId.onreadystatechange = function()
{
if (areaId.readyState == "complete")
{
var frame = frames[window.event.srcElement.id];
var li = frame.document.getElementsByTagName("li");

for (var i = 0; i < li.length; i++)
{
var action = li[i].getAttribute("action");
if(action != null && action.indexOf(relId) > 1)
{
lookupEntityTypeCode = action.substring(action.indexOf("\(")+1, action.indexOf(","));
li[i].onclick = CustomLookup;
break;
}
}
}
}
}
}
}

function CustomLookup()
{
var lookupSrc = "/" + ORG_UNIQUE_NAME + "/_controls/lookup/lookupmulti.aspx?class=&objecttypes=" + lookupEntityTypeCode + "&browse=0";
if(lookupId != null && lookupId.DataValue != null && lookupId.DataValue[0] != null)
{
lookupSrc = lookupSrc + "&id=" + lookupId.DataValue[0].id;
}

var lookupItems = window.showModalDialog(lookupSrc, null);
if (lookupItems) // This is the CRM internal JS funciton on \_static\_grid\action.js
{
if ( lookupItems.items.length > 0 )
{
AssociateObjects( crmFormSubmit.crmFormSubmitObjectType.value, crmFormSubmit.crmFormSubmitId.value, lookupEntityTypeCode, lookupItems, true, null, relId);
}
}
}

18 May 2008

The mysterious CRM Lookup (III)

5. Let's have a look another common used filtered lookup example: I want the regarding field is set to open cases.

a. CRM 3.0

/* set the regarding to open case : Form.onLoad() */
crmForm.all.regardingobjectid.lookuptypes = "112";
crmForm.all.regardingobjectid.lookuptypeIcons = "/_imgs/ico_16_112.gif";

/* only show the active cases : Form.onLoad() */
if (crmForm.ObjectId != null)
{
crmForm.all.regardingobjectid.lookupbrowse = 1;
crmForm.all.regardingobjectid.additionalparams = "fetchXml="
+ "<fetch mapping='logical'><entity name='incident'><all-attributes /><filter>"
+ "<condition attribute='statecode' operator='eq' value='0' />"
+ "</filter></entity></fetch>";
}


b. CRM 4.0

In 4.0, the above feature is not supported anymore which means not working any more, but we can still use the method which mentioned in my previous post. Set 'statecode' as a find column of the Case Lookup View, then add the following code to the entity's onLoad():

/* set the regarding to open case : Form.onLoad() */
crmForm.all.regardingobjectid.lookuptypes = "112";
crmForm.all.regardingobjectid.lookuptypeIcons = "/_imgs/ico_16_112.gif";
crmForm.all.regardingobjectid.additionalparams = 'search=Active';


All good, now we are changing the requirement to: I want the regarding field is set to open cases which owned by the current user!

Ok, this simple and clean approach will not work for this a little complicated search. We need to build up a fetch query first. It's easy to use advanced find:

Look for: cases
-Status Equals Active
-Owner Equals Current User'

And then click Find, we get results.

Now, how can we get benefit from this Advanced Find and get the FetchXml statements from the query? Thanks Ronald Lemmen who first find a trick, in the result page, paste it in the IE Address Bar:

javascript:prompt("", resultRender.FetchXml.value);

Then you will get a prompt window, the value is the FetchXml statements which we need.

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false"><entity name="incident"><attribute name="title"/><attribute name="ticketnumber"/><attribute name="createdon"/><attribute name="incidentid"/><order attribute="title" descending="false"/><filter type="and"><condition attribute="statecode" operator="eq" value="0"/><condition attribute="ownerid" operator="eq-userid"/></filter></entity></fetch>

Thanks Adi Katz who found a brilliant way to do it, and George modified it slightly. Basicly it overwrites the code-behind function, which I think it could be a risk. However so far so good after three months since it has been first released, I will post updates if it occurs any issue.(There is a supported filtered lookup product from Michael Höhne which is not free but great product.)

You need to modify lookupsingle.aspx file in the path \CRMWeb\_controls\lookup\lookupsingle.aspx
Add the following codes:
 
<script runat="server">
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
crmGrid.PreRender += new EventHandler(crmGrid_PreRender);
}
void crmGrid_PreRender(object sender, EventArgs e)
{
if (crmGrid.Parameters["search"] != null && crmGrid.Parameters["search"].StartsWith("<fetch"))
{
crmGrid.Parameters.Add("fetchxml", crmGrid.Parameters["search"]);
crmGrid.Parameters.Remove("searchvalue");
this._showNewButton = false;
}
}
</script>


And then you can filter it from entity's onLoad() just like what we did in CRM 3.0. Notice that we have pasted the FetchXml statements below(fetchStr).

/* set the regarding to open case which owned by current user : Form.onLoad() */
crmForm.all.regardingobjectid.lookuptypes = "112";
crmForm.all.regardingobjectid.lookuptypeIcons = "/_imgs/ico_16_112.gif";

var fetchStr = "<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false"><entity name="incident"><attribute name="title"/><attribute name="ticketnumber"/><attribute name="createdon"/><attribute name="incidentid"/><order attribute="title" descending="false"/><filter type="and"><condition attribute="statecode" operator="eq" value="0"/><condition attribute="ownerid" operator="eq-userid"/></filter></entity></fetch>";
crmForm.all.regardingobjectid.lookupbrowse = 1;
crmForm.all.regardingobjectid.additionalparams = "search=" + fetchStr;


That's it, now we reach the end. :)

11 May 2008

The mysterious CRM Lookup (II)

4. Now, how can we set/filter the lookup content? Because CRM3 and CRM4 are so different in lookup, so the solution is different. Let’s see a common example: In the Account record, only show the account owned contacts in the Primary Contact (primarycontactid) lookup.

a. CRM 3.0


/* CRM 3.0: only show account owned contacts in the primarycontactid lookup : Form.onLoad() */
if (crmForm.FormType == 2 && crmForm.ObjectId != null)
{
crmForm.all.primarycontactid.lookupbrowse = 1;
crmForm.all.primarycontactid.additionalparams = "fetchXml="
+ "<fetch mapping='logical'><entity name='contact'><all-attributes /><filter>"
+ "<condition attribute='accountid' operator='eq' value='" + crmForm.ObjectId + "' />"
+ "</filter></entity></fetch>";
}


b. CRM 4.0

As far as I know, there are two unsupported ways to do that. Because we don't need a complicated fetchxml in this case, we so could use the first approach:

1. Customize Contact entity, open Contacts Lookup View, click 'Add Find Column', add the Parent Customer (parentcustomerid), save and publish it.
2. Customize Account entity, put the following code into Form.onLoad() :


/* CRM 4.0: only show account owned contacts in the primarycontactid lookup : Form.onLoad() */
if (crmForm.FormType == 2 && crmForm.ObjectId != null)
{
var name = crmForm.all.name.DataValue;
crmForm.all.primarycontactid.additionalparams = 'search=' + name;
}

It's a nice approach which used the 'search' feature of the CRM lookup. When this parameter is specified it defaults the search string in the lookup dialog and applies the search when the dialog is opened.


It's great, now let's change the requirement:
How about: Only show the Parent Account (parentaccountid) owned contacts in the Primary Contact (primarycontactid) lookup.

We still need to repeat step (1), and then in the step (2):


/* CRM 4.0: only show parent account owned contacts in the primarycontactid lookup: Form.onLoad() */
FilterLookup = function(source, target)
{
if (IsNull(source) IsNull(target)) { return; }
var name = IsNull(source.DataValue) ? '' : source.DataValue[0].name;
target.additionalparams = 'search=' + name;
}

Also, we need to put the following code into parentaccountid.onChange():


/* CRM 4.0: only show parent account owned contacts in the primarycontactid lookup : parentaccountid.onChange() */
FilterLookup(crmForm.all.parentaccountid, crmForm.all.primarycontactid);


It's great too, now how about we add another requirement on the above example:
The Primary Contact (primarycontactid) should be automatically selected when this contact is the primary contact of the selected Parent Account (parentaccountid).

Although we could do it through AJAX, we can also do it through CRM 4.0 lookup field automatic resolutions technique. Thanks for Adi Katz, let's start from begin:

(1) Turn off the Parent Account (parentaccountid) "automatic resolutions in field" feature by double click the field on the Form.
(2) Put the following codes in Account.onLoad():


function OnAfterAccountSelect()
{
var contactLookup = crmForm.all.primarycontactid;
if( contactLookup.DataValue != null ) {return;}

contactLookup.AutoResolve = 1;
var accountLookup = crmForm.all.parentaccountid;
primaryContact = accountLookup.items[0].keyValues.primarycontactid;

contactLookup.SetFocus();
contactDiv = document.all.primarycontactid_d.getElementsByTagName("DIV")[0];
contactDiv.innerText = primaryContact.value;
contactLookup.Lookup( true , true , primaryContact.value , true );
}

function OnCrmPageLoad()
{
crmForm.all.parentaccountid.attachEvent( "onafterselect" , OnAfterAccountSelect );
}

OnCrmPageLoad();


The second approach which can deal with some complicated conditions will be introduced in the next post. :)

04 May 2008

The mysterious CRM Lookup (I)

1. As many of you already know that the CRM lookup field saves the GUID of the related entity. Let's have a deep view of the lookup field. When you create a relationship between two entities, one entity can reference the other entity through a lookup field. However it's not just create one attribute in the database, it means although you can only see one relationship created in CRM interface, there are some invisible attributes for internal/customizer use. Let's see an example, we know that we can reference some values from a lookup fields:


crmForm.all.regardingobjectid.DataValue[0].id; // The GUID of the lookup.
crmForm.all.regardingobjectid.DataValue[0].name; // The text value of the lookup.
crmForm.all.regardingobjectid.DataValue[0].typename; // The entity type name.


But how CRM get those values? Actually when user open a CRM record, those attributes are downloaded from CRM database to the 'containers' which can be seen from entity's customization.xml, it's a complex structure which I don't want to explain in this post. So when you create a relationship between two entities, the CRM system will create more than 2 'containers'(attributes) in the database to keep other information about the lookup field(e.g. id, typename etc).

2. Set the default value for a lookup field, let's take a look at the special lookup field again: regardingobjectid
In many cases, the regardingobjectid is default to Account, but how can we change the default value to Contact?

In the onLoad() event, you can set the default attributes by using setAttribute method which is unsupported by Microsoft, however it's a standard XML DOM method.

Let's see some examples:


crmForm.all.regardingobjectid.setAttribute("lookuptypes", "1,2"); //only show account and contact
crmForm.all.regardingobjectid.setAttribute("lookuptypeIcons", "/_imgs/ico_16_1.gif :/_imgs/ico_16_2.gif"); //set the icons
crmForm.all.regardingobjectid.setAttribute("defaulttype", "2"); //default to contact

Instead of using setAttribute method, you can also use CRM method directly:

crmForm.all.regardingobjectid.lookuptypes = "1,2";
crmForm.all.regardingobjectid.lookuptypeIcons = "/_imgs/ico_16_1.gif:/_imgs/ico_16_2.gif";
crmForm.all.regardingobjectid.defaulttype = "2";



3. If you have a look of the URL when you open a lookup window, you may see how CRM calls a lookup:



/lookupsingle.aspx?class=ActivityRegarding&objecttypes=1,2,3,4&browse=0&ShowNewButton=1&ShowPropButton=1&DefaultType=0

lookupsingle.aspx has some parameters which can be referenced by our developers, those parameters are:

Objecttypes : Entity code, e.g. Objecttypes = "1, 2" //show account and contact
DefaultType : the default lookup entity, e.g. DefaultType = "2" //default to contact
Browse : bool, 0 = show the "Look for" bar; 1 = browse model, hide the "Look for " bar.
ShowNewButton : bool, 0 = hide the "New" button; 1 = show the "New" button.
ShowPropButton : bool, 0 = hide the "Properties" button; 1 = show the "Properties" button.

In an IFRAME or a ISV solution, if you don't want users to see the 'New' Button, you can just set the URL to:
/lookupsingle.aspx?class=ActivityRegarding&objecttypes=1,2,3,4&browse=0&ShowNewButton=0&ShowPropButton=1&DefaultType=0

It's cool so far! But how about if want to hide the 'New' button in CRM?
You can't just say: crmForm.all.regardingobjectid.ShowNewButton = 0; it doesn't work. But what you can do is in the onLoad() event, use attachEvent method to attach a setadditionalparams event for the regardingobjectid. Again, those methods are all unsupported customisations, however, those are widely used in the Web development.

/*
Function: show/hide the 'New' button of lookup
bShow = 0 : hide the New Button
bShow = 1 : show the New Buton
*/
function NewButton(bShow)
{
return function()
{
crmForm.all.regardingobjectid.AddParam("ShowNewButton", bShow);
}
}
crmForm.all.regardingobjectid.attachEvent("setadditionalparams",NewButton(0));