logo

MS CRM 3.0 : custom filtered lookup field generator

Death Angel posted on Saturday, January 12, 2008 11:55 PM

Hi all,
I finally finished my JS function to create automatically a custom
lookup field (see the code bellow).
I want to thank Darren Liu (post on
http://crowechizek.typepad.com/crm/2007/05/adding_a_second.html?cid=96621316#comments),
Stefano Demiliani (post on http://demiliani.com/blog/archive/2006/12/29/4770.aspx)
and AndrewN23 (post on http://andrewn23.blogspot.com/2006/09/overcoming-relationship-restrictions.html)
for the help of their posts.
I've added another functionnality to the lookup, the onChange event is
a parameter of the function. So if you want an action to perform after
the change of the lookup, just pass the javascript code in params.
Thanks to Ronald (post on http://ronaldlemmen.blogspot.com/2006/11/using-advanced-find-for-fetchxml.html),
filtering the lookup popup is also available.
Just think to replace the html code for % ("%") (when using

Here is the code

//===============================================
//==   Transform a text field into a           ==
//==   lookup field                            ==
//==   sFieldName : field name                 ==
//==   sLookuptype : object type code of the   ==
//==   		entity you lookup              ==
//==   sEntity : name of the entity you lookup ==
//==   sOnChangeCode : javascript code to      ==
//==        execute on the onChange event      ==
//==   oGUID : GUID to affect on the loading   ==
//==   sDisplayText : text dipayed in lookup   ==
//==   sFilters : filters to apply             ==
//===============================================
function Text_To_LookUp(sFieldName, sLookuptype, sEntity,
sOnChangeCode, oGUID, sDisplayText, sFilters)
{

var sIcons = "/_imgs/";
var sLookupTypeNames = "";
var sLookupClass = "";

if (sEntity.substr(0,4) == "new_")
{
sIcons += "icon.aspx?objectTypeCode=" + sLookuptype +
sLookupTypeNames = " lookuptypenames='" + sEntity + ":" +
sLookuptype + "' ";
}
else
{
sLookupClass = " lookupclass='" + sEntity + "' ";
sIcons += "ico_16_" + sLookuptype + ".gif";
}

if (sOnChangeCode != null && sOnChangeCode != "")
sOnChangeCode = "onChange=\" return " + sOnChangeCode + "\"";

var New_HTML = ""
var Div_HTML = ""

Div_HTML = "<DIV class=lu>";
if (oGUID != null)
{
Div_HTML += "<SPAN class=lui onclick=\'openlui();\' otype=\'"
+ sLookuptype + "\' oid=\'" + oGUID + "\' otypename=\'" + sEntity +
Div_HTML += "<IMG class=lui src=\'" + sIcons + "\'>";
Div_HTML += sDisplayText;
Div_HTML += "</SPAN>";
}
else
{
Div_HTML += " ";
}
Div_HTML += "</DIV>";

New_HTML += "<table class='lu' cellpadding='0' cellspacing='0'
width='100%' style='table-layout:fixed;'>";
New_HTML += "<tr>";
New_HTML += "<td>";
New_HTML += Div_HTML;
New_HTML += "</td>";
New_HTML += "<td width='25' style='text-align: right;'>";
New_HTML += "<img src='/_imgs/btn_off_lookup.gif' id='" +
sFieldName + "' class='lu' tabindex='1000' lookuptypes='" +
sLookuptype + "' " +  sLookupTypeNames + " lookuptypeIcons='" + sIcons
+ "' " + sLookupClass + " lookupbrowse='0' lookupstyle='single'
defaulttype='0' req='0' " + sOnChangeCode + ">";
New_HTML += "</td>";
New_HTML += "</tr>";
New_HTML += "</table>";


var field = crmForm.elements[sFieldName];

field.insertAdjacentHTML("afterEnd",New_HTML)
field.parentNode.removeChild(field);
field = crmForm.elements[sFieldName];
field.value = oGUID;

//Add filters to lookup box
if (sFilters !=null && sFilters != "")
{
field.lookupbrowse = 1;
field.additionalparams = 'fetchXml=' + sFilters;
}

}

example of use :
Assuming you want 2 lookups on contact entity on the same form
one lookup is a standard lookup, the second is a textbox which will
store the GUID of the second contact (new_contact_id)
When a contact in this custom lookup is selected, I want to update
part of your form's fields (for example firstname, lastname and
country)
Assuming you have a JS function (fn_Update_Fields) that takes a guid
and do this update, and another that get a specific field from an
entity for a guid specified (fn_Get_Info)
The only thing you have to do with my function is this simple line :
Text_To_LookUp("new_contact_id", "2", "contact",
crmForm.all.new_contact_id.DataValue, fn_Get_Info("contact",
crmForm.all.new_contact_id.DataValue, "fullname"));

Oh, and just be carefull, in some crm forms, like the customeraddress
one, there are 2 JS variables that are missing :
var LOCID_LU_SELECT_VALUE_FOR
var LOCID_LU_SELECT_VALUE
This missing cause a JS error, but doesn't block the lookup behaviour.
Those 2 vars are for the tooltip text on the lookup field.
A little explaination for why they're not in the page code : it's
because you cannot add a relation between customeraddress entity and a
another entity (referential of countries for example) where
cutomeraddress is the "n" part of the relation. So, as you cannot
create a relation, the crmform code does'nt contain the code for
lookup controls. So just Think to add those 2 vars in your onLoad code
^o^.

Hope it will help !

Don't hesitate to ask question or post comments to correct bugs if
there are some.
At last, this code is provided "as is", and I cannot be hold
responsible for any issues that it might cause.

See you

DA

Hi How can i add this variables in the code? Can you give me a sample?

Na posted on Monday, February 25, 2008 9:50 AM

Hi
How can i add this variables in the code? Can you give me a sample?

MS CRM 3.0 : custom filtered lookup field generator

Death Angel posted on Tuesday, March 18, 2008 10:12 PM

Hi Naw,

Sorry for the delay, I was on hollidays ^o^

Here is a sample :
if (!self.LOCID_LU_SELECT_VALUE_FOR)
{
var LOCID_LU_SELECT_VALUE_FOR =3D "Click to select a
value for \u007b0\u007d\u002e";
var LOCID_LU_SELECT_VALUE =3D "Click to select a value
\u002e";
}

You can just add this test in the onload event of the crm Form. The
text is a personnal translation from my french CRM. To see the exact
text, open a crm form that contains a native lookup field, press Ctrl
+N to open the form in a classical IE window and edit the source code
of the page.
Search for those 2 variables, and there you will find the crm original
text.

Hope it will help.

See you

DA


Didn't Find The Answer You Were Looking For?

EggHeadCafe has experts online right now that may know the answer to your question.  We pay them a bonus for answering as many questions as they can.  So, why not help them and yourself by becoming a member (free) and ask them your question right now?
Create Account & Ask Question In Live Forum





Pete's Resume  |  Robbe's Resume  |  Neado  |  Free Icons  |  Privacy  |   (c) 2010