Reputation: 177
I have requirement on Dynamics crm 2015 online:
What I have tried so far! I have written a JavaScript in which I tried to catch the event of '+' button on sub grid, using 'addEventListener' on 'gridname_addImageButton' id but the event was not caught. This is quite basic stuff for normal web development, but not happening on dynamics crm.
UPDATE This the HTML of the "+" button that appears on the grid.
<a action="tec|ManyToMany|SubGridStandard|Mscrm.AddExistingRecordFromSubGridAssociated" tabindex="1340"
title="Add record." class="ms-crm-ImageStrip-addButton" style="display: block; cursor: pointer;"
onclick="return false;" id="tec_addImageButton" href="#">
<img class="ms-crm-add-button-icon" title="Add Experlogix Model record." alt="Add record."
id="Tec_addImageButtonImage" src="/_imgs/imagestrips/transparent_spacer.gif?ver=-893257913" />
</a>
And this is the javascript that I tried:
var elem = document.getElementById('tec_addImageButton');
elem.addEventListener('click',myFunc(),false);
What am I missing? Regards, Momi
Upvotes: 0
Views: 5076
Reputation: 986
if (typeof (myObj) == "undefined") {
myObj = {};
}
(function () {
myObj.init = function () {
Xrm.Page.ui.controls.get('id_iframe').getObject().onload= function() {
var el = Xrm.Page.ui.controls.get('id_iframe').getObject().contentWindow.document.getElementById('myLabel');
addEvent(el, 'click', function () { alert('Evento'); });
};
}
function addEvent(element, evnt, funct) {
if (element.attachEvent)
return element.attachEvent('on' + evnt, funct);
else
return element.addEventListener(evnt, funct, false);
}
})();
<HTML>
<BODY onload="SetupEvents()">
<label id="myLabel" >Click me</label>
</HTML>
Upvotes: 0
Reputation: 1453
Make sure that event is attaching to the anchor and add the function without brackets () while adding the event listener like in the following example.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
window.onload = function () {
document.getElementById('tec_addImageButton').addEventListener('click', myFunc, false)
}
function myFunc() {
alert("testing");
}
</script>
</head>
<body>
<a id="tec_addImageButton" href="#">test button</a>
</body>
</html>
Upvotes: 0
Reputation: 7224
CRM forms are not "normal web development" as the product does not support direct DOM manipulation. The only manipulation allowed is that done using CRM's Xrm.Page object. The general how-to of using Xrm.Page is documented in the SDK: https://msdn.microsoft.com/en-us/library/gg328261.aspx.
Specifically, you are looking to add a custom filter to a lookup, which can be done with the code:
Xrm.Page.getControl(arg).addCustomFilter(filter, entityLogicaName)
Where filter
is a string with a valid FetchXML filter, i.e.:
<filter type="and">
<condition attribute="new_somefieldname" operator="eq" value="somevalue" />
</filter>
You can call addCustomFilter in the OnLoad event or you can use a PreSearch event handler to add your filter:
Xrm.Page.getControl(arg).addPreSearch(handler)
Credit to http://missdynamicscrm.blogspot.com/2014/08/crm-2013-using-addcustomfilter-to-get-filtered-lookup-field-based-on-linked-entity.html for this fully implemented example.
function onLoad()
{
addEventHandler();
}
function addEventHandler() {
// add the event handler for PreSearch Event
Xrm.Page.getControl("parentcontactid").addPreSearch(addFilter);
}
function addFilter() {
//find contact contains this @example.com
var email = "%@example.com%";
//create a filter xml
var filter = "<filter type='and'>" +
"<condition attribute='emailaddress1' operator='like' value='" + email + "'/>" +
"</filter>";
//add filter
Xrm.Page.getControl("parentcontactid").addCustomFilter(filter);
}
Upvotes: 1