JScript eksempelkode: Replace Filtered Lookup on Add Existing button

Multiselect list

One of my colleagues (Casper Schau, Logica) made ​​me aware of the following article which I have based this article on, and extended the code with new elements: CRM 2011: Filtered Lookup for Add Existing button for Many-to-many Relationship...

Link to Unmanaged SolutionSearchDemo_1_0_0_0.zip (25,55 kb)
 

Objective

Replace the default filter, and replace the filter by code, when clicking the button Add Existing button.

Steps

  1. Select the sub form.
  2. Click the button Add Existing....
  3. Use the custom made (coded) filter, who shows only the records who has the same Account as this record (A Store (sample)).
  4. Default filter when not coded, and the filtered look result when no Account is selected.

 

Prerequisites

Create the following entities and fields:

Element Name
First Entity new_firstentity

  

Name new_name Primary Text Field
Account new_account Lookup field (N:1 Relation)

 

Element Name
Second Entity new_secondentity   
Name new_name Primary Text Field
Account new_account Lookup field (N:1 Relation)
Subgrid   N:N Relation between First Entity and Second Entity

 

Code and Ribbon setup

Do the following:

  1. Create a Solution and create the above entities (First Entity and Second Entity), containing the fields etc.
    When creating the Lookup Fields (Account) the entity Account will automatically be added to the Solution
  2. Create a JScript Webresource to the Solution, and name it new_ReplaceFilteredLookup.js.
  3. Add the following code to the Webresource:
    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(crmFormSubmit.crmFormSubmitObjectType.value, crmFormSubmit.crmFormSubmitId.value, params.gridTypeCode, lookupItems, IsNull(roleOrd) || roleOrd == 2, "", relName);
        }
    }
    
    function replaceAddExistingButtonViewForAccount(gridTypeCode, gridControl) {
        var accountText = getAccountName();
        replaceAddExistingButtonView({
            gridTypeCode: gridTypeCode,
            gridControl: gridControl,
            fetchXml: "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
    					  "<entity name='new_firstentity'>" +
    						"<attribute name='new_firstentityid' />" +
    						"<attribute name='new_name' />" +
    						"<attribute name='new_account' />" +
    						"<order attribute='new_name' descending='false' />" +
    						accountText +
    					  "</entity>" +
    					"</fetch>",
            layoutXml: "<grid name='resultset' object='1' jump='name' select='1' icon='1' preview='1'>" +
    					  "<row name='result' id='new_firstentityid'>" +
    						"<cell name='new_name' width='150' />" +
    						"<cell name='new_account' width='150' />" +
    					  "</row>" +
    					"</grid>",
            name: "Custom Search View"
        });
    }
    
    function getAccountName() {
        // Get a lookup value 
        var lookupItem = new Array();
        lookupItem = Xrm.Page.getAttribute("new_account").getValue();
    
        if (lookupItem != null) {
            var filter = "<filter type='and'>" +
                         "<condition attribute='new_accountname' operator='like' value='%" + lookupItem[0].name + "%' />" +
                         "</filter>";
            return filter;
        } else {
            return "";
        }
    }
     
  4. Export the Solution.
  5. Edit the file customization.xml from the Solution, and add the following to the <RibbonDiffXml> of the entity First Entity:
    <RibbonDiffXml>
    <CustomActions />
    <Templates>
        <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates>
    </Templates>
    <CommandDefinitions>
        <CommandDefinition Id="Mscrm.AddExistingRecordFromSubGridAssociated">
        <EnableRules>
            <EnableRule Id="Mscrm.AppendPrimary" />
            <EnableRule Id="Mscrm.AppendToPrimary" />
            <EnableRule Id="Mscrm.EntityFormIsEnabled" />
        </EnableRules>
        <DisplayRules>
            <DisplayRule Id="Mscrm.AddExisting" />
            <DisplayRule Id="Mscrm.ShowForManyToManyGrids" />
            <DisplayRule Id="Mscrm.AppendPrimary" />
            <DisplayRule Id="Mscrm.AppendToPrimary" />
            <DisplayRule Id="Mscrm.AppendSelected" />
            <DisplayRule Id="Mscrm.AppendToSelected" />
            <DisplayRule Id="Mscrm.CanWriteSelected" />
        </DisplayRules>
        <Actions>
            <JavaScriptFunction FunctionName="replaceAddExistingButtonViewForAccount" Library="$webresource:new_ReplaceFilteredLookup.js">
            <CrmParameter Value="SelectedEntityTypeCode" />
            <CrmParameter Value="SelectedControl" />
            </JavaScriptFunction>
        </Actions>
        </CommandDefinition>
    </CommandDefinitions>
    <RuleDefinitions>
        <TabDisplayRules />
        <DisplayRules />
        <EnableRules />
    </RuleDefinitions>
    <LocLabels />
    </RibbonDiffXml>
     
  6. Insert the file customization.xml into the Solution, and import the Solution.

Bliv medlem af CRMUG DK

De regionale afdelinger i CRMUG giver mulighed for a mødes med andre medlemmer af CRMUG i dit lokalområde. Hermed får du mulighed for at mødes ansigt til ansigt og vidensdele med andre brugere af Microsoft Dynamics CRM i dit område.

Læs mere den Danske afdeling af CRMUG...

Om Henrik Jensen

Jeg har altid fingeren på pulsen når det drejer sig om Microsofts produkter & teknologier, og især når det handler om Microsoft Dynamics CRM.

Henrik Jensen

Jeg har arbejdet professionelt i IT-branchen mere end 25 års, deraf mere end 18 år med CRM-systemer, og samtidig indehaver af mere end 50 Microsoft-certificeringer.

Mobil: +45 20 300 300
E-mail: hj@easyconsult.dk

EASYConsult ApS

Læs mere...

Downloads, værktøjer, installation og JScript

Downloade komponenter til MS CRM:
    • Microsoft Dynamics CRM 4.0
    • Microsoft Dynamics CRM 2011
    • Microsoft Dynamics CRM 2013
    • Microsoft Dynamics CRM 2015
    • Microsoft Dynamics CRM 2016

Liste over Opdateringspakker (Rollups):
    • Microsoft Dynamics CRM 4.0
    • Microsoft Dynamics CRM 2011
    • Microsoft Dynamics CRM 2013
    • Microsoft Dynamics CRM 2015
    • Microsoft Dynamics CRM 2016

Værktøjer til MS CRM:
    • Liste med værktøjer til MS CRM

Krav og opsætninger til installation:
    • Krav og opsætninger MS CRM 2011
    • Krav og opsætninger MS CRM 2013
    • Krav og opsætninger MS CRM 2015
    • Krav og opsætninger MS CRM 2016

JScript eksempelkode:
    • JScript eksempelkode

Diverse værktøjer:
    • Sysinternals
    • PowerShell og MS CRM 2011

Visual Studio, SSIS og Kingswaysoft
    • Visual Studio 2017
    • Dataværktøjer (SSIS) 2017
    • Kingswaysoft

Månedsliste

Forbehold

Alt hvad du læser på denne blog er alene udtryk for mine egne holdninger og meninger, og kan ikke henføres til andet end som så.

De løsninger jeg fremstiller på denne blog er ikke nødvendigvis testet i et driftsmiljø. Hvis du gør brug af mine løsninger er det på eget ansvar.