Monday, 30 January 2012

Customising RetrieveMulitple Request

I was working on a task where I was creating a dialog, this dialog shows list of records in a drop down control and once I select a value in drop down and then enter some value in the text box, I apply the formula for the value enter for the selected record, also on the form user is asked if he want to do the task again for another record. Selecting Yes recrusivly calls the same dialog and exclude the one which i have already applied formulla on.

Solution:
To achieve this on the custom entity create a filter attribute and add some string to differenciate the request which needs to be changed.
e.g. new_retrievemultiplefilter

In dialog create a crm queury and pass all the neccesary conditions along with the specified attribute with the value e.g. "custom entity needs custom filter"



Plugin: CustomEntityRetrieveMulitple
Stage: Pre transaction


IOrganizationService crmService = localContext.OrganizationService;

            object query = localContext.PluginExecutionContext.InputParameters["Query"];
            if (query.GetType() == typeof(FetchExpression))
            {
                FetchExpression fe = (FetchExpression)localContext.PluginExecutionContext.InputParameters["Query"];
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(fe.Query);

                XmlNode retrievemultiplefilterCondtion = doc.SelectSingleNode("fetch/entity/filter/condition[@attribute='new_retrievemultiplefilter']");

                if (retrievemultiplefilterCondtion != null)
                {
                    string value = retrievemultiplefilterCondtion.Attributes["value"].Value;
                    if (string.Equals(value, " custom entity needs custom filter ", StringComparison.InvariantCultureIgnoreCase))
                    {
                        XmlNode retrievemultiplefilterNode = retrievemultiplefilterCondtion.ParentNode;
                        retrievemultiplefilterNode.RemoveChild(retrievemultiplefilterCondtion);

                        XmlNode impCondition1 = doc.SelectSingleNode("fetch/entity/link-entity/filter/condition[@attribute='new_value1']");
                        XmlNode impCondition2 = doc.SelectSingleNode("fetch/entity/link-entity/filter/condition[@attribute=' new_value2']");
                     
                        string val1= null, val2 = null;

                        if ( impCondition1  != null && stepIdCondition != null)
                        {
                            val1=  impCondition1.Attributes["value"].Value;
                            val2 =  impCondition2 .Attributes["value"].Value;
                        }

                        XmlNode  new_customentity2Node = doc.SelectSingleNode("fetch/entity/link-entity[@name='new_customentity2']");

                        XmlNode entityNode =  new_customentity2Node.ParentNode;
                        entityNode.RemoveChild( new_customentity2Node );

                        EntityCollection records = Utility.GetFilteredRecords(crmService, val1,val2);
                        if (records.Entities.Count > 0)
                        {
                            XmlNode notInCondition = doc.CreateNode(retrievemultiplefilterCondtion.NodeType, retrievemultiplefilterCondtion.Name, retrievemultiplefilterCondtion.NamespaceURI);
                            XmlAttribute attribute = doc.CreateAttribute("attribute");
                            attribute.Value = "new_customid";
                            notInCondition.Attributes.Append(attribute);

                            XmlAttribute operatorAttribute = doc.CreateAttribute("operator");
                            operatorAttribute.Value = "not-in";
                            notInCondition.Attributes.Append(operatorAttribute);

                            retrievemultiplefilterNode.AppendChild( notInCondition );

                            foreach (var  record  in  records  .Entities)
                            {
                                XmlNode valueNode = doc.CreateNode(retrievemultiplefilterNode.NodeType, "value", retrievemultiplefilterNode.NamespaceURI);
                                valueNode.InnerText = ((EntityReference) record  ["new_customid"]).Id.ToString();
                                 notInCondition .AppendChild( valueNode  );                              
                            }
                        }
                        localContext.PluginExecutionContext.InputParameters["Query"] = new FetchExpression(doc.OuterXml);
                    }
                }
            }

No comments:

Post a Comment