CRM 4.0 OnChange code not firing on multi-entity lookup.

abaines
abaines used Ask the Experts™
on
Hi Folks

I have used the following code to make a multi-entity lookup;

function MultiEntityTypeLookup(PrimaryLookupFieldName){this.PrimaryLookupFieldName=PrimaryLookupFieldName;this.MultiLookupFieldName="c5_multilookup_"+this.PrimaryLookupFieldName;this.LookUps=new Object();this.LookUpsCount=0;this.ProcessMultiLookup=true;this.ReadOnly=!(crmForm.FormType==1||crmForm.FormType==2||crmForm.FormType==6);try{eval('var lookup = crmForm.all.'+this.PrimaryLookupFieldName+'; if (lookup) this.LookUps[\''+this.PrimaryLookupFieldName+'\'] = lookup;');eval('this.PrimaryLookupControl = crmForm.all.'+this.PrimaryLookupFieldName);}catch(ex){}if(this.PrimaryLookupControl){this.LookUpsCount=1;}else{this.ProcessMultiLookup=false;alert('Client side script multi-lookup argument invalid '+this.PrimaryLookupFieldName);}for(var i=1;i<arguments.length;i++){try{eval('var lookup = crmForm.all.'+arguments[i]+'; if (lookup) this.LookUps[\''+arguments[i]+'\'] = lookup;');}catch(ex){}if(!this.LookUps[arguments[i]]){alert('Client side script multi-lookup argument invalid '+arguments[i]);}else{this.LookUpsCount++;}}if(!this.ReadOnly){var duplicateType=false;for(testKey in this.LookUps){var testType=this.LookupAttribute(testKey,'lookuptypes');for(searchKey in this.LookUps){var searchType=this.LookupAttribute(searchKey,'lookuptypes');if(testType==searchType&&testKey!=searchKey){this.ProcessMultiLookup=false;var message='Duplicate lookup types are not supported. '+testKey+' and '+searchKey+' are of the same lookup type!';alert(message);return;}}}}this.ProcessMultiLookup=this.LookUpsCount>=2;if(this.ProcessMultiLookup){this.PrimaryLookupInnerHTML=this.GetLookupCell(this.PrimaryLookupFieldName).innerHTML;var MultiEntityData;for(keyVar in this.LookUps){var lookup=this.LookUps[keyVar];if(lookup.DataValue!=null){MultiEntityData=lookup.DataValue;break;}}var MultiEntityTypeLookupEditField=this.GetField(this.PrimaryLookupFieldName+"_ledit");var tabIndex;if(MultiEntityTypeLookupEditField==null){tabIndex=PrimaryLookupControl.getAttribute("tabIndex");}else{tabIndex=MultiEntityTypeLookupEditField.getAttribute("tabIndex");}var lookupTypes=this.ConcatLookupAttributes('lookuptypes',',');var lookupTypeNames=this.ConcatLookupAttributes('lookuptypenames',',');var lookupTypeIcons=this.ConcatLookupAttributes('lookuptypeIcons',':');var reqLevel=this.LookupAttribute(this.PrimaryLookupFieldName,'req');var multiLookupIdHtml=this.GetLookupFieldHtml(this.MultiLookupFieldName,tabIndex,lookupTypes,lookupTypeNames,lookupTypeIcons,reqLevel);this.ExchangeLookups(this.MultiLookupFieldName,this.PrimaryLookupFieldName,multiLookupIdHtml,true);this.MultiLookup=this.GetField(this.MultiLookupFieldName);this.MultiLookup.DataValue=MultiEntityData;if(this.ReadOnly){this.Disable(true);}}};
MultiEntityTypeLookup.prototype.ConcatLookupAttributes=function(attributeName,delimiter){var concat='';for(keyVar in this.LookUps){var lookup=this.LookUps[keyVar];if(concat.length>0)concat+=delimiter;concat+=lookup.attributes.item(attributeName).value;}return concat;};
MultiEntityTypeLookup.prototype.LookupAttribute=function(lookupName,attributeName){var lookup=this.LookUps[lookupName];if(lookup){return lookup.attributes.item(attributeName).value;}else{return null;}};
MultiEntityTypeLookup.prototype.GetLookupFieldHtml=function(name,tabIndex,lookupTypes,lookupTypeNames,lookupTypeIcons,reqLevel){var html="<table class=\"ms-crm-Lookup\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" style=\"table-layout:fixed;\">"+"<tbody>"+"<tr>"+"<td>"+"<div ime-mode=\"auto\" class=\"ms-crm-Lookup\" tabindex=\""+(parseInt(tabIndex)+1)+"\"></div>"+"<label class=\"ms-crm-Hidden-NoBehavior\" for=\""+name+"_ledit\"></label>"+"<input class=\"ms-crm-Hidden-NoBehavior\" ime-mode=\"auto\" type=\"text\" tabindex=\""+tabIndex+"\" id=\""+name+"_ledit\" maxlength=\"1000\"/>"+"</td>"+"<td width=\"25\" class=\"Lookup_RenderButton_td\">"+"<img src=\"/_imgs/btn_off_lookup.gif\" id=\""+name+"\" class=\"ms-crm-Lookup\" req=\""+reqLevel+"\" style=\"ime-mode:auto\" lookuptypes=\""+lookupTypes+"\" lookuptypenames=\""+lookupTypeNames+"\" lookuptypeIcons=\""+lookupTypeIcons+"\" lookupclass=\"BasicCustomer\" lookupbrowse=\"0\" lookupstyle=\"single\" defaulttype=\"0\" autoresolve=\"1\" showproperty=\"1\" resolveemailaddress=\"0\">"+"<a href=\"#\" onclick=\"previousSibling.click();\" tabindex=\"-1\"></a>"+"</td>"+"</tr>"+"</tbody>"+"</table>";return html;};
MultiEntityTypeLookup.prototype.GetField=function(name){var oItem=crmForm.all.item(name);return oItem;};
MultiEntityTypeLookup.prototype.GetLookupCellId=function(name){return name+"_d";};
MultiEntityTypeLookup.prototype.GetLookupCell=function(name){return this.GetField(this.GetLookupCellId(name));};
MultiEntityTypeLookup.prototype.ExchangeLookups=function(lookupToShowId,lookupToHideId,newLookupHtml,isNewLookup){var prevCell=this.GetLookupCell(lookupToHideId);var prevCellIndex=prevCell.cellIndex;var row=prevCell.parentNode;if(isNewLookup){prevCell.style.display="none";var newCell=row.insertCell(prevCellIndex);newCell.id=this.GetLookupCellId(lookupToShowId);newCell.innerHTML=newLookupHtml;}else{row.deleteCell(prevCellIndex);this.GetLookupCell(lookupToShowId).style.display="";}};
MultiEntityTypeLookup.prototype.OnSave=function(){if(this.ProcessMultiLookup&&!this.ReadOnly){var MultiEntity=this.MultiLookup.DataValue;this.ExchangeLookups(this.PrimaryLookupFieldName,this.MultiLookupFieldName,this.PrimaryLookupInnerHTML,false);eval('this.LookUps[\''+this.PrimaryLookupFieldName+'\'] = crmForm.all.'+this.PrimaryLookupFieldName+';');var MultiEntitytype='';if(MultiEntity)MultiEntitytype=MultiEntity[0].type+'';for(keyVar in this.LookUps){var lookup=this.LookUps[keyVar];var thistype=this.LookupAttribute(keyVar,'lookuptypes');if(thistype==MultiEntitytype){lookup.DataValue=MultiEntity;}else{lookup.DataValue=null;}lookup.ForceSubmit=true;}this.ProcessMultiLookup=false;}else{}};
MultiEntityTypeLookup.prototype.HasValue=function(){return(this.MultiLookup!=null&&this.MultiLookup.DataValue!=null);};
MultiEntityTypeLookup.prototype.SelectedName=function(){return this.HasValue()?this.MultiLookup.DataValue[0].name:null;};
MultiEntityTypeLookup.prototype.SelectedType=function(){return this.HasValue()?this.MultiLookup.DataValue[0].type:null;};
MultiEntityTypeLookup.prototype.Disable=function(disabled){var CLASS_ENABELD='ms-crm-Lookup';var CLASS_DISABELD='ms-crm-Lookup ms-crm-ReadOnly ms-crm-ReadOnly';this.MultiLookup.disabled=disabled;this.MultiLookup.src=disabled?"/_imgs/btn_dis_lookup.gif":"/_imgs/btn_off_lookup.gif";this.MultiLookup.style.setAttribute("cursor",disabled?"auto":"hand");this.MultiLookup.className=disabled?CLASS_DISABELD:CLASS_ENABELD;this.MultiLookup.parentNode.parentNode.children[0].childNodes[0].className=disabled?CLASS_DISABELD:CLASS_ENABELD;};
MultiEntityTypeLookup.prototype.ReadOnly=false;

/////////////////// Multi entity lookup form code ////////////////////////
debugger;
OnLoad = function()
{             
   document.CustomLookup=new MultiEntityTypeLookup('dlr_contactid', 'dlr_organisationid');
  
    if (crmForm.all.tab3Tab != null) {
        crmForm.all.tab3Tab.style.display = "none";
crmForm.all.dlr_contactid.FireOnChange();
    }
}
OnSave = function()
{
    if (document.CustomLookup)  document.CustomLookup.OnSave();
}
OnLoad();
/////////////////// End multi entity lookup form code ////////////////////////

Open in new window


It works fine but now I've discovered that the OnChange code for this lookup field will not fire. I have tried a number of combinations of using;

crmForm.all.LOOKUPFIELD.onclick = function()
or
crmForm.all.LOOKUPFIELD.FireOnChange();

But nothing works. Does anybody know how to run OnChange code on a multi-entity lookup field?

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Hi abaines,

On which Lookup field you are trying to run this code?

Regards,
Chinmay.

Author

Commented:
Hi Chinmay

dlr_contactid

Thanks
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Hi abaines,

try : crmForm.all.LOOKUPFIELD.attachEvent("onclick", function);

Regards,
Chinmay.
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Author

Commented:
Hi Chinmay

Is there anywhere in particualar this code needs to go. I have it in the Form OnLoad page but it isn't working.

Thanks
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Hi abaines,

Yes it should be on Form OnLoad.

This is interesting. Can you use IE Developer Tools[press F12] and check if it's the right  element where you are registering the event? You can try manual registration as well from within the console.

Regards,
Chinmay.

Author

Commented:
Hi Chinmay

I've downloaded Developer Tools but I can't get it to open when CRM closes the original windows and opens itself in a new one without the usual toolbars. Anyway just to double check, relating to the code above in my original post, do you think the  code you gave me (crmForm.all.LOOKUPFIELD.attachEvent("onclick", function);) should be within either of the OnLoad or OnSave functions or should that line of code be completely separate?

Thanks
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
It should be on OnLoad and.. wait a second.... it is part of IE itself what have you downloaded? and when you press F12 when IE is open Dev tools will automatically start. Which version of IE you are using?

Author

Commented:
I'm using version 7.0.5730.13. I can open the developer toolbar from the menu but F12 doesn't open it. The problem is when I open CRM it opens itself in a new window without any of the normal tool bars.
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
I strongly recommned you use IE8 or even better IE9 for this purpose. IE8 onwards you have the built in Dev Toolbar support[F12].

Also your CRM is configured to run in Application mode. You can disable it from System settings area.
Or
You could open a normal IE window and then type in the entire URL
http://server:port/orgname/main.aspx
Commented:
Hi Chinmay

thank you for all your efforts but I finally have this figured out. It required the addition of this piece of code ;

if (document.CustomLookup.ProcessMultiLookup){
document.CustomLookup.MultiLookup.onchange = function(){alert('change event fired');};
}
into the OnLoad function.

Thanks again.
abaines

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial