CRM 2011 Filter Add Existing Button By Parent Field


This thing work with 1:N relationship and N:N relationship.

How does this thing work?

Let say u have entity A and entity B [A : B (1:N relationship)]. When u are in form view A u can navigate to sub entity B [Subgrid view for entity B] by clicking the related entity located at the left side. Now u are in sub entity B and u can either create new or add existing record in entity B.
Here the thing work. I wan to filter add existing button in entity B. Filter by the parent record field [Field in entity A]. The method i'm using is FetchXml and Javascript.

The problem now is:
1. I cannot create view filter by field. [Another view].
2. I don't wan the view to appear in the Entity B view.
3. How to generate the view using fetchXml.

Steps:
Step 1: Create a javascript function name it as CustomAddExistingView.js [That what i name my script]. The script library locate at $webresource: new_CustomAddExistingView.js. Add the script into CustomAddExistingView.js.

JavaScript Eg:



function replaceAddExistingButtonView(params) {


    var relName = params.gridControl.getParameter("relName"),
        roleOrd = params.gridControl.getParameter("roleOrd"),
        viewId = "{00000000-0000-0000-0000-000000000001}"; // a dummy view ID


    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(document.parentWindow.parent.crmFormSubmit.crmFormSubmitObjectType.value, document.parentWindow.parent.crmFormSubmit.crmFormSubmitId.value, params.gridTypeCode, lookupItems, IsNull(roleOrd) || roleOrd == 2, "", relName);
    }
}


function replaceAddExistingButtonViewForAccount(gridTypeCode, gridControl, primaryEntityName) {
    //get the entity name on which entity to work at
    var locateField = document.parentWindow.parent.Xrm.Page.data.entity.getEntityName();


    if (locateField == "vw_workorder") {
        //get parent field
        var CaseId = document.parentWindow.parent.Xrm.Page.data.entity.attributes.get("vw_caseid").getValue();
        replaceAddExistingButtonView({
            gridTypeCode: gridTypeCode,
            gridControl: gridControl,
            //make sure all is in name not scheme name for fetchXml and layoutXml
            fetchXml: "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'> " +
                      "<entity name='vw_items'>" +
                        "<attribute name='vw_itemtype' />" +
                        "<attribute name='vw_caseid' />" +
                        "<attribute name='vw_workorder' />" +
                        "<attribute name='vw_name' />" +
                        "<attribute name='vw_projectid' />" +
                        "<attribute name='vw_propertyid' />" +
                        "<attribute name='vw_category' />" +
                        "<attribute name='vw_area' />" +
                        "<attribute name='vw_element' />" +
                        "<attribute name='vw_defect' />" +
                        "<attribute name='vw_defectrectified' />" +
                        "<attribute name='createdon' />" +
                        "<order attribute='vw_itemtype' descending='false' />" +
                        "<filter type='and'>" +
                            "<condition attribute='vw_caseid' operator='eq' uiname='213' uitype='incident' value='" + CaseId[0].id + "' />" +
                            "<condition attribute='vw_workorder' operator='null'/>" +
                        "</filter>" +
                      "</entity>" +
                      "</fetch>",
            layoutXml: "<grid name='resultset' object='" + gridTypeCode + "' jump='vw_name' select='1' icon='1' preview='1'>" +
  "<row name='result' id='vw_itemsid'>" +
                        "<cell name='vw_itemtype' width='150' />" +
                        "<cell name='vw_caseid' width='100' />" +
                        "<cell name='vw_workorder' width='100' />" +
                        "<cell name='vw_name' width='200' />" +
                        "<cell name='vw_projectid' width='100' />" +
                        "<cell name='vw_propertyid' width='100' />" +
"<cell name='vw_category' width='100' />" +
"<cell name='vw_area' width='100' />" +
                        "<cell name='vw_element' width='100' />" +
"<cell name='vw_defect' width='100' />" +
                        "<cell name='vw_defectrectified' width='100' />" +
"<cell name='createdon' width='150' />" +
 "</row>" +
"</grid>",
            name: "Item Related To Case"
        });
    }
    else {
        replaceAddExistingButtonView({
            gridTypeCode: gridTypeCode,
            gridControl: gridControl,
            fetchXml: "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
                       "<entity name='vw_items'>" +
                        "<attribute name='vw_itemtype' />" +
                        "<attribute name='vw_caseid' />" +
                        "<attribute name='vw_workorder' />" +
                        "<attribute name='vw_name' />" +
                        "<attribute name='vw_projectid' />" +
                        "<attribute name='vw_propertyid' />" +
                        "<attribute name='vw_category' />" +
                        "<attribute name='vw_area' />" +
                        "<attribute name='vw_element' />" +
                        "<attribute name='vw_defect' />" +
                        "<attribute name='vw_defectrectified' />" +
                        "<attribute name='createdon' />" +
                        "<order attribute='vw_itemtype' descending='false' />" +
                        "<filter type='and'>" +
                          "<condition attribute='vw_caseid' operator='null' />" +
                        "</filter>" +
                      "</entity>" +
                      "</fetch>",
            layoutXml: "<grid name='resultset' object='" + gridTypeCode + "' jump='vw_name' select='1' icon='1' preview='1'>" +
  "<row name='result' id='vw_itemsid'>" +
                        "<cell name='vw_itemtype' width='150' />" +
                        "<cell name='vw_caseid' width='100' />" +
                        "<cell name='vw_workorder' width='100' />" +
                        "<cell name='vw_name' width='200' />" +
                        "<cell name='vw_projectid' width='100' />" +
                        "<cell name='vw_propertyid' width='100' />" +
"<cell name='vw_category' width='100' />" +
"<cell name='vw_area' width='100' />" +
                        "<cell name='vw_element' width='100' />" +
"<cell name='vw_defect' width='100' />" +
                        "<cell name='vw_defectrectified' width='100' />" +
"<cell name='createdon' width='150' />" +
 "</row>" +
"</grid>",
            name: "Item Not Related To Case"
        });
    }
}


End of Javascript


Step 2: Now it time to edit based on the xml. Create one new solution name Ribbon.
Step 3: At the ribbon solution, add existing entity u wan to edit the ribbon and save. Let say i wan to edit entity A ribbon then i add existing entity A into the ribbon solution.
Step 4: Export the solution out and extract the solution.
Step 5: From the ribbon extracted file open customizations.xml and edit it.
Step 6: Find <RibbonDiffXml> and edit the xml according to below:

XML eg:


<RibbonDiffXml>
        <CustomActions />
        <Templates>
          <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates>
        </Templates>
    <CommandDefinitions>
      //For Many to Many Relationship add existing
      <CommandDefinition Id="Mscrm.AddExistingRecordFromSubGridAssociated">
        <EnableRules>
          <EnableRule Id="Mscrm.AppendToPrimary" />
          <EnableRule Id="Mscrm.EntityFormIsEnabled" />
        </EnableRules>
        <DisplayRules>
          <DisplayRule Id="Mscrm.AddExisting" />
          <DisplayRule Id="Mscrm.ShowForManyToManyGrids" />
          <DisplayRule Id="Mscrm.AppendToPrimary" />
          <DisplayRule Id="Mscrm.AppendSelected" />
          <DisplayRule Id="Mscrm.CanWriteSelected" />
        </DisplayRules>
        <Actions>
          <JavaScriptFunction FunctionName="replaceAddExistingButtonViewForAccount" Library="$webresource:new_CustomAddExistingView.js">
            <CrmParameter Value="SelectedEntityTypeCode" />
            <CrmParameter Value="SelectedControl" />
<CrmParameter Value="PrimaryEntityTypeName" />
          </JavaScriptFunction>
        </Actions>
      </CommandDefinition>
      //For Many to Many Relationship add existing
      <CommandDefinition Id="Mscrm.AddExistingRecordFromSubGridStandard">
        <EnableRules>
          <EnableRule Id="Mscrm.AppendToPrimary" />
          <EnableRule Id="Mscrm.EntityFormIsEnabled" />
        </EnableRules>
        <DisplayRules>
          <DisplayRule Id="Mscrm.AddExisting" />
          <DisplayRule Id="Mscrm.ShowForOneToManyGrids" />
          <DisplayRule Id="Mscrm.AppendToPrimary" />
          <DisplayRule Id="Mscrm.AppendSelected" />
          <DisplayRule Id="Mscrm.CanWriteSelected" />
        </DisplayRules>
        <Actions>
          <JavaScriptFunction FunctionName="replaceAddExistingButtonViewForAccount" Library="$webresource:new_CustomAddExistingView.js">
            <CrmParameter Value="SelectedEntityTypeCode" />
            <CrmParameter Value="SelectedControl" />
<CrmParameter Value="PrimaryEntityTypeName" />
          </JavaScriptFunction>
        </Actions>
      </CommandDefinition>
    </CommandDefinitions>
        <RuleDefinitions>
          <TabDisplayRules />
          <DisplayRules />
          <EnableRules />
        </RuleDefinitions>
        <LocLabels />
      </RibbonDiffXml>

End of XML
*Note - Make sure that the library and the function name is correct.

Step 7: Zip the 3 xml file (Customization, Solution and [Content_Types]) into Ribbon.zip.
Step 8: Import back everything into the solution and publish all.

Finish ! Try it... anything just leave me a comment or message. Glad to help.^^

1 comments:

Unknown said...

For add existing button u can create to add existing for other entity. The problems is u need to get the gridTypeCode of the subgrid.