Debugging · Dynamics CRM 2011 · Javascript · Ribbon · Workaround

CRM 2011: Filtered Lookup for Add Existing button for One-to-Many Relationship (crmFormSubmit is undefined)

Awhile back I wrote a post on .

Yesterday I came across a requirement of filtering lookup for Add Existing button for a one-to-many relationship in crm, and I kept getting a debugging error crmFormSubmit is undefined.

I managed to figure out how to make it work based on the post above with some changes below.

1. Display Rule

<DisplayRule Id="Mscrm.ShowForOneToManyGrids" />

2. Ribbon Action Parameter

<Actions>
  <JavaScriptFunction FunctionName="replaceAddExistingButtonViewForAccount" Library="$webresource:new_CustomAddExistingView">
	<CrmParameter Value="SelectedEntityTypeCode" />
	<CrmParameter Value="SelectedControl" />
	<CrmParameter Value="PrimaryEntityTypeName" />
	<CrmParameter Value="PrimaryEntityTypeCode"/>
	<CrmParameter Value="FirstPrimaryItemId"/>
  </JavaScriptFunction>
</Actions>

3. Javascript

function replaceAddExistingButtonView(params) {
    var relName = params.gridControl.getParameter("relName"),
        roleOrd = params.gridControl.getParameter("roleOrd"),
        viewId = "{1DFB2B35-B07C-44D1-868D-258DEEAB88E2}"; //Random GUID
    
    var customView = {
        fetchXml: params.fetchXml,
        id: viewId, 
        layoutXml: params.layoutXml,
        name: params.name,
        recordType: params.gridTypeCode,
        Type: 0
    };

    var lookupItems = LookupObjects(null, "multi", params.gridTypeCode, 0, null, "", null, null, null, null, null, null, viewId, [customView]);
    if (lookupItems && lookupItems.items.length > 0) {
        AssociateObjects(params.primaryEntityTypeCode, params.primaryItemId, params.gridTypeCode, lookupItems, IsNull(roleOrd) || roleOrd == 2, null, relName);
    }
}

Notice the check if (primaryEntityName == "new_x") for filtering on the correct primary entity. For entities other than new_x, we call the default ribbon action Mscrm.GridRibbonActions.addExistingFromSubGridStandard instead.

function replaceAddExistingButtonViewForAccount(gridTypeCode, gridControl, primaryEntityName, primaryEntityTypeCode, firstPrimaryItemId) {
    if (primaryEntityName == "new_x") {
		replaceAddExistingButtonView({
			gridTypeCode: gridTypeCode,
			gridControl: gridControl,
			primaryEntityTypeCode: primaryEntityTypeCode,
			primaryItemId: firstPrimaryItemId,
			fetchXml: "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
						  "<entity name='account'>" +
							"<attribute name='name' />" +
							"<attribute name='primarycontactid' />" +
							"<attribute name='telephone1' />" +
							"<attribute name='accountid' />" +
							"<order attribute='name' descending='false' />" +
							"<filter type='and'>" +
							  "<condition attribute='name' operator='like' value='Abc%' />" +
							"</filter>" +
						  "</entity>" +
						"</fetch>",
			layoutXml: "<grid name='resultset' object='1' jump='name' select='1' icon='1' preview='1'>" +
						  "<row name='result' id='accountid'>" +
							"<cell name='name' width='300' />" +
							"<cell name='primarycontactid' width='150' />" +
							"<cell name='telephone1' width='100' />" +
						  "</row>" +
						"</grid>",
			name: "Accounts Beginning with Abc"
		});
	}
	else {
		Mscrm.GridRibbonActions.addExistingFromSubGridStandard(gridTypeCode, gridControl);
	}
}

Cheers – Sy

Advertisements

5 thoughts on “CRM 2011: Filtered Lookup for Add Existing button for One-to-Many Relationship (crmFormSubmit is undefined)

  1. I follow this post in crm2013

    but it did not work

    as ” lookupItems = LookupObjects(…)” the LookupObjects(…) always return null

    1. Hi ice,

      I have not had the chance to look at compatibility on CRM 2013. If you have found a solution, please leave a comment.

      Cheers,
      Sy

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s