0
votes

I have requirement on Dynamics crm 2015 online:

  1. I have a lookup and subgrid on a custom entity.
  2. When the user clicks on the 'search' button, which shows search box, I want that search to be pre-populated with the lookup field value, so to save the user from copy/pasting or typing the lookup value into the search box.

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.

What I am trying to achieve!

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

3
Well maybe the css selector used to get the plus button is wrong hence click event isn't attached properly. Cant say much without seeing the DOM or code.blessenm
What you are trying to achieve is an unsupported customization that may break in future versions of the product. And, without sharing some code it's hard to tell what is going wrong.Henk van Boeijen
Hey guys, I have updated my question and added html and js code that I used.Mahmood

3 Answers

1
votes

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);
}
0
votes

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>
0
votes
  • You have to put the html script as a web resource in your solution.
  • Insert HTML inside your Form
  • You have to put the js script as a web resource in your solution.
  • You have to event the frame on Form Libraries : web resource
  • You have to event the frame on Event Handler -> Form -> onload Add: myObj.init()

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>