locked
MS CRM 4.0 - Plugin Error while updating lead record - "Server is unable to process the request" RRS feed

  • Question

  • Hi, I've been trying to update the lead record through following code, however 'Server is unable to process the request' is being thrown.

    BusinessEntityCollection collLead = crmService.RetrieveMultiple(myExpression);
    
    
    
            if (collLead.BusinessEntities.Count > 0)
    
            {
    
             foreach (BusinessEntity be in collLead.BusinessEntities)
    
             {
    
              lead de = new lead();
    
              de.leadid = ((lead)be).leadid;
    
              //lead def = (lead)crmService.Retrieve(EntityName.lead.ToString(), ((lead)be).leadid.Value, new ColumnSet(new string[] { "donotbulkemail", "donotemail", "donotfax", "donotphone", "donotpostalmail", "donotsendmm" }));
    
              
               de.donotbulkemail = new CrmBoolean(true);
    
               de.donotemail = new CrmBoolean(true);
    
               de.donotfax = new CrmBoolean(true);
    
               de.donotphone = new CrmBoolean(true);
    
               de.donotpostalmail = new CrmBoolean(true);
    
               de.donotsendmm = new CrmBoolean(true);
    
               de.jobtitle = "Test";//.address1_line1 = "Street1 address changed.";
    
               crmService.Update(de);
    
             }}

             
    BusinessEntityCollection is returning proper results and all lead results are being populated as expected. But when I try to update the resulted collection of lead record, code is throwing the above exception.

    Kindly let me know if I am missing something in above code snippet.
    Early response is highly appreciated.

    Thanks,
    Abhi


    • Edited by Abhi D Friday, June 17, 2011 6:56 PM
    Friday, June 17, 2011 6:36 PM

Answers

  • Here is the explanation for an error - you are trying to get custom attributes using RetrieveMultiple method so the solution is to replace following code:

    BusinessEntityCollection coll = crmService.RetrieveMultiple(query);

    with

    RetrieveMultipleRequest request = new RetrieveMultipleRequest();
    request.ReturnDynamicEntities = true;
    request.Query = query;

    RetrieveMultipleResponse response = null;
    try
    {
        response = crmService.Execute(request) as RetrieveMultipleResponse;
    }
    catch(SoapException e)
    {
        throw new Exception(e.Detail.InnerText);
    }

    BusinessEntityCollection coll = response.BusinessEntityCollection;


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter
    Saturday, June 18, 2011 5:28 AM
    Moderator

All replies

  • Can you post the entire error code (with server traces if available).

    Gonzalo | gonzaloruizcrm.blogspot.com

    Friday, June 17, 2011 6:54 PM
    Moderator
  • Please find the code as given below:

    public class UpdateLeads : IPlugin
      {
        #region IPlugin Members
    
        public void Execute(IPluginExecutionContext context)
        {
          List<Owner> leadOwnerList = new List<Owner>();
    
          DynamicEntity dEntity = (DynamicEntity)context.InputParameters.Properties["Target"];
    
          try
          {
            string sWeb = string.Empty; ;
            if (dEntity.Properties.Contains("websiteurl"))
            {
              // Prepare website URL without "http://"
              if (dEntity.Properties["websiteurl"].ToString().Contains("http://"))
              {
                string str = dEntity.Properties["websiteurl"].ToString().Replace("//", "@");
                string[] sWebsite = str.Split('@');
                sWeb = sWebsite[1].ToString();
              }
    
              ICrmService crmService = context.CreateCrmService(true);
    
              ConditionExpression accConditionExpr = new ConditionExpression();
              accConditionExpr.AttributeName = "websiteurl";
              accConditionExpr.Operator = ConditionOperator.Equal;
              accConditionExpr.Values = new object[] { dEntity.Properties["websiteurl"] };
    
              ConditionExpression accConditionExpr1 = new ConditionExpression();
              accConditionExpr1.AttributeName = "websiteurl";
              accConditionExpr.Operator = ConditionOperator.Equal;
              accConditionExpr1.Values = new object[] { sWeb };
    
              FilterExpression filter1 = new FilterExpression();
              filter1.FilterOperator = LogicalOperator.Or;
              filter1.Conditions.Add(accConditionExpr);
              filter1.Conditions.Add(accConditionExpr1);
    
              QueryExpression query = new QueryExpression("account");
              query.ColumnSet = new ColumnSet(new string[] { "websiteurl","customertypecode","cyb_state" });
              query.Criteria.Filters.Add(filter1);
    
              BusinessEntityCollection coll = crmService.RetrieveMultiple(query);
    
              //Throw exception with message if account with same website URL doesn't exist.
              if (coll.BusinessEntities.Count == 0)
                throw new InvalidPluginExecutionException("Account doesn't exist for the current lead record. Kindly create new Account with same website URL and link this record under 'Lead Reference' on the Account.");
    
              //If account exists, check 'Relationship Type' and 'State' and mark all contact methods to 'Do not Allow' for all leads with same website URL
              if (coll.BusinessEntities.Count == 1)
              {
                foreach (BusinessEntity bAcc in coll.BusinessEntities)
                {
                  TargetRetrieveAccount trAcc = new TargetRetrieveAccount();
                  trAcc.EntityId = ((account)bAcc).accountid.Value;
    
                  RetrieveRequest trRequest = new RetrieveRequest();
                  trRequest.ColumnSet = new ColumnSet(new string[] { "websiteurl", "customertypecode", "cyb_state" });
                  trRequest.ReturnDynamicEntities = true;
                  trRequest.Target = trAcc;
    
                  RetrieveResponse trResponse = (RetrieveResponse)crmService.Execute(trRequest);
                  DynamicEntity deAccount = (DynamicEntity)trResponse.BusinessEntity;
                  string strState = string.Empty;
                  string strRelType = string.Empty;
                  if(deAccount.Properties.Contains("cyb_state"))
                    strState = ((Picklist)deAccount.Properties["cyb_state"]).name;
                  if(deAccount.Properties.Contains("customertypecode"))
                    strRelType = ((Picklist)deAccount.Properties["customertypecode"]).name;
    
                  if (strState == string.Empty || strRelType == string.Empty)
                    return;
                  //account acc = (account)crmService.Retrieve(EntityName.account.ToString(), ((account)bAcc).accountid.Value, new AllColumns());
    
                  if ((strState.ToLower() == "active" && strRelType.ToLower() == "customer") || strState.ToLower() == "active" || strRelType.ToLower() == "customer")
                  {
                    ConditionExpression leadConditionExpr = new ConditionExpression();
                    leadConditionExpr.AttributeName = "websiteurl";
                    leadConditionExpr.Values = new object[] { dEntity.Properties["websiteurl"] };
    
                    QueryExpression myExpression = new QueryExpression("lead");
                    //myExpression.ColumnSet = new ColumnSet(new string[] { "address1_line1", "websiteurl", "ownerid", "donotbulkemail", "donotemail", "donotfax", "donotphone", "donotpostalmail", "donotsendmm" });
                    myExpression.ColumnSet = new AllColumns();
                    myExpression.Criteria.Conditions.Add(leadConditionExpr);
    
                    BusinessEntityCollection collLead = crmService.RetrieveMultiple(myExpression);
    
                    if (collLead.BusinessEntities.Count > 0)
                    {
                      foreach (BusinessEntity be in collLead.BusinessEntities)
                      {
                        lead de = new lead();
                        de.leadid = ((lead)be).leadid;
                        //lead def = (lead)crmService.Retrieve(EntityName.lead.ToString(), ((lead)be).leadid.Value, new ColumnSet(new string[] { "donotbulkemail", "donotemail", "donotfax", "donotphone", "donotpostalmail", "donotsendmm" }));
    
    
                        Guid currentRec = new Guid(context.OutputParameters.Properties["id"].ToString());
                        if (de.leadid.Value.ToString() != currentRec.ToString() && de.leadid.Value != Guid.Empty)
                        {
                          de.donotbulkemail = new CrmBoolean(true);
                          de.donotemail = new CrmBoolean(true);
                          de.donotfax = new CrmBoolean(true);
                          de.donotphone = new CrmBoolean(true);
                          de.donotpostalmail = new CrmBoolean(true);
                          de.donotsendmm = new CrmBoolean(true);
                          de.jobtitle = "Test";//.address1_line1 = "Street1 address changed.";
                          crmService.Update(de);
                          
                        }
                        
                      }
                      
                    }
                  }
                }
              }
            }
          }
          catch (Exception ex)
          {
            throw new Exception(ex.Message);
          }
        }
    
        #endregion
      }
    

     

    Friday, June 17, 2011 7:00 PM
  • oops I mean the error code, not the plugin code...and any traces you can get can be helpful

    Gonzalo | gonzaloruizcrm.blogspot.com

    Friday, June 17, 2011 8:11 PM
    Moderator
  • Here is the explanation for an error - you are trying to get custom attributes using RetrieveMultiple method so the solution is to replace following code:

    BusinessEntityCollection coll = crmService.RetrieveMultiple(query);

    with

    RetrieveMultipleRequest request = new RetrieveMultipleRequest();
    request.ReturnDynamicEntities = true;
    request.Query = query;

    RetrieveMultipleResponse response = null;
    try
    {
        response = crmService.Execute(request) as RetrieveMultipleResponse;
    }
    catch(SoapException e)
    {
        throw new Exception(e.Detail.InnerText);
    }

    BusinessEntityCollection coll = response.BusinessEntityCollection;


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter
    Saturday, June 18, 2011 5:28 AM
    Moderator
  • Hi Andriy,
    Got the root cause of the error! Brief details are as follow: I've a plugin having 3 different classes. Sometime back, I modified the name of one of the class and was working on different class. So, while updating the lead record through above given code, CRM Service was throwing SOAP Exception that system cannot load the plugin with the classname (which was changed by mistake).

    Anyways, the problem has been resolved. Thanks for your fruitful reply. Since I am newbie to CRM dev, facing lots of issues and you've always provided me with the right solution.

    Thanks,
    Abhi

    Saturday, June 18, 2011 10:14 AM