Hi,
I am a newbie to CRM and I am trying to get this code to work. This plugin is supposed filter the queues view based on the roles assigned to the user.
I am getting an error in the foreach loop for the ConditionExpression. Below is the error message.
"Collection was modified; enumeration operation may not execute."
**************************************************************
public class HideQueues : IPlugin
{
public void Execute(IPluginExecutionContext context)
{
string strErrorMsg = String.Empty;
List roles = new List();
try
{
if ((context.IsExecutingInOfflineMode == true) || (context.Mode != 0) || (context.PrimaryEntityName != EntityName.queue.ToString()) || (context.MessageName != "RetrieveMultiple") || (context.Stage != 10))
{
return;
}
// Check that the InputParameters property bag is not NULL
if (context.InputParameters.Properties != null)
{
foreach (PropertyBagEntry item in context.InputParameters.Properties)
{
// Check that a QueryExpression object exists in the property bag
if (item.Name == "Query")
{
QueryExpression objQueryExpression = (QueryExpression)item.Value;
// Get each ConditionExpression from the QueryExpression
foreach (ConditionExpression objConditionExpression in objQueryExpression.Criteria.Conditions)
{
//Is the ConditionExpression the one we want to replace 'queuetypecode = 1'
if (objConditionExpression.AttributeName == "queuetypecode" && objConditionExpression.Operator == ConditionOperator.Equal && objConditionExpression.Values[0].ToString() == "1")
{
//delete the ConditionExpression
objQueryExpression.Criteria.Conditions.Remove(objConditionExpression);
roles = CheckUserRoles(context);
//Replace it with a new FilterExpression
objQueryExpression.Criteria.Filters.Add(NewFilterExpression(context, roles));
}
}
}
}
}
}
private List CheckUserRoles(IPluginExecutionContext context)
{
ICrmService service = context.CreateCrmService(true);
XmlDocument objXmlDoc = new XmlDocument();
List roles = new List();
// Fetch users who are in the business unit of the Sales Engineer and have the system user role AE
StringBuilder sb = new StringBuilder();
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
sb.Append("");
string result = service.Fetch(sb.ToString());
objXmlDoc.LoadXml(result);
XmlNodeList nl = objXmlDoc.GetElementsByTagName("result");
foreach (XmlNode n in nl)
{
XmlNode name = n["name"];
XmlNode systemuserid = n["aa.systemuserid"];
if (name != null)
{
if (name.InnerText == "Team Leader")
roles.Add(name.InnerText);
if (name.InnerText == "Application Engineers")
roles.Add(name.InnerText);
}
}
return roles;
}
}
***********************************************************
After searching for this error I found that the code breaks when it tries to delete the ConditionExpression on which it loops through. I don't know how to fix this code. Any help is greatly appreciated. Thanks!!
Raj.
*This post is locked for comments
I have the same question (0)