Dynamics CRM 2011 · Javascript · Ribbon

CRM 2011: Custom Ribbon ‘Add Existing’ Button

I just encountered a requirement where users want to be able to associate entity x existing records to entity y record. So the idea is to create a custom ribbon button which will open a lookup dialog with data already filtered and on selection associate those records to current record in opened form.

This is the code that I came up with

function openCustomLookupDialog(gridTypeCode) {
    var relName = "new_x_y";
    var roleOrd = -1;
    var viewId = "{1DFB2B35-B07C-44D1-868D-258DEEAB88E2}"; //Random GUID
  
    if (!IsNull(relName)) {
        var customView = {
            fetchXml: "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
                          "<entity name='new_x'>" +
                            "<attribute name='new_xid' />" +
                            "<attribute name='new_name' />" +
                            "<filter type='and'>" +
                              "<condition attribute='new_name' operator='eq' value='Test' />" +
                            "</filter>" +
                          "</entity>" +
                        "</fetch>",
            id: viewId,
            layoutXml: "<grid name='resultset' object='1' jump='new_name' select='1' icon='1' preview='1'>" +
                          "<row name='new_x' id='new_xid'>" +
                            "<cell name='new_name' width='300' />" +
                          "</row>" +
                        "</grid>",
            name: "Filtered Defects",
            recordType: gridTypeCode,
            Type: 0
        };

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

Follow my previous post to add the ribbon button.

Please note the Action JavacriptFunction must have parameter SelectedEntityTypeCode.

<CrmParameter Value="SelectedEntityTypeCode" />

Cheers – Sy

Advertisements

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