Friday, 16 August 2013

EP: AX Lookup: using SetMarkedRows(IEnumerable)

// Get the roles converted into keys
IEnumerable<IAxViewRowKey> viewKeys = getViewDataKeys(e.LookupControl.LookupDataSetViewMetadata);

lookup.SetMarkedRows(viewKeys);

--------------------------------------------------------------------------------
///


    /// Get the view datakeys based on the provided Segments
    /// Written by Mallik
    private List getViewDataKeys(DataSetViewMetadata viewMetaData)
    {
        DataSetViewRow row;
        string Roles = "";
        String lSegments;
        String[] roles = Roles.Split('-');
        IAxaptaRecordAdapter axRecord;
        TableMetadata tableMetaData = MetadataCache.GetTableMetadata(TableMetadata.TableNum("smmBusRelSegmentGroup"));
        List viewDataKeys = new List();
        try
        {
            // Retrieve the current row.
            row = AvaAnnouncementsDS.GetDataSet().DataSetViews["AvaAnnouncements"].GetCurrent();
            Roles = row.GetFieldValue("Segments").ToString();
            roles = Roles.Split('-');
            // For each Segment
            foreach (string role in roles)
            {
                using (axRecord = AxSession.AxaptaAdapter.CreateAxaptaRecord("smmBusRelSegmentGroup"))
                {
                    // Try to retrieve a list of the items.
                    axRecord.ExecuteStmt("select * from %1");//where smmBusRelSegmentGroup.Segments==" + role);

                    while (axRecord.Found)
                    {
                        lSegments = axRecord.GetField("SegmentId").ToString();
                        if (lSegments.Trim() == role.Trim())
                        {
                            // Create dictionary
                            Dictionary dict = new Dictionary();
                            dict.Add("SegmentId", lSegments);
                            // Create indexes
                            List index = new List(1);
                            index.Add(tableMetaData.DefaultUniqueIndex);
                            // Get viewdatakey  Add Item to list
                            viewDataKeys.Add((IAxViewRowKey)AxViewDataKey.CreateFromDictionary(viewMetaData, dict, index.ToArray()));
                            break;
                        }//if end
                        axRecord.Next();
                    }//  while end
                }//using end
            }//for end
        }
        catch (System.Exception ex)
        {
            AxExceptionCategory exceptionCategory;

            // Determine whether the exception can be handled.
            if (AxControlExceptionHandler.TryHandleException(this, ex, out exceptionCategory) == false)
            {
                // The exception was fatal and cannot be handled. Rethrow it.
                throw;
            }
        }
        return viewDataKeys;
    }

How to Disable “Advanced Filter or Sort” and Enforce Custom Filters on Any D365FO Form

 In Dynamics 365 Finance and Operations, users can apply filters through the “Advanced filter or sort” feature found under the Options tab...