web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Update field after Creation of Lead on Post operation using plugin

(0) ShareShare
ReportReport
Posted on by

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Generic SQL error.Detail: e0547166-cb71-475a-a68e-fa104f1006be -2147204784 Generic SQL error. 2018-03-17T06:11:03.8851939Z false

Hi,

I have created a plugin in which Lead create on post operation , i want to check whether the account exist or not depending on website url.

If account exist then update parentaccountid field i.e existing account but it shows me an error SQL server error.

*This post is locked for comments

I have the same question (0)
  • Arpit Shrivastava Profile Picture
    7,518 User Group Leader on at

    Could you please place your code here to investigate?

    Thanks

  • Community Member Profile Picture
    on at

    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

               IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

               IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

               try

               {

               if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

               {

                   // Obtain the target entity from the input parameters.                

                   Entity Lead = new Entity("lead");

                   string emailid = null;

                   int index;

                   string domain;

                   string[] domains = new string[10];

                   string[] publicdomains = { "aol", "att", "comcast", "facebook", "gmail", "gmx", "googlemail", "google", "hotmail", "hotmail", "mac", "me", "mail", "msn", "live", "sbcglobal", "verizon", "yahoo", "yahoo", "email", "games", "gmx", "hush", "hushmail", "icloud", "inbox", "lavabit", "love", "outlook", "pobox", "rocketmail", "safe-mail", "wow", "ygm", "ymail", "zoho", "fastmail", "yandex", "bellsouth", "charter", "comcast", "cox", "earthlink", "juno", "btinternet", "virginmedia", "blueyonder", "freeserve", "live", "ntlworld", "o2", "orange", "sky", "talktalk", "tiscali", "virgin", "wanadoo", "bt", "sina", "qq", "naver", "hanmail", "daum", "nate", "yahoo", "yahoo", "yahoo", "yahoo", "yahoo", "yahoo", "hotmail", "live", "laposte", "yahoo", "wanadoo", "orange", "gmx", "sfr", "neuf", "free", "gmx", "hotmail", "live", "online", "t-online", "web", "yahoo", "mail", "rambler", "yandex", "ya", "list", "hotmail", "live", "skynet", "voo", "tvcablenet", "telenet", "hotmail", "live", "yahoo", "fibertel", "speedy", "arnet", "hotmail", "gmail", "yahoo", "live", "yahoo", "hotmail", "live", "hotmail", "prodigy", "msn" };

                   bool found = false;

                   Entity ent = (Entity)context.InputParameters["Target"];

                   Lead = service.Retrieve("lead", ent.Id, new ColumnSet(true));

                   if(Lead.LogicalName != "lead")

                       {

                           return;

                       }              

                   if(Lead.Attributes.Contains("emailaddress1"))

                   {

                       emailid = Lead.Attributes["emailaddress1"].ToString();

                       index = emailid.IndexOf("@");

                       domain = emailid.Substring(index + 1, emailid.Length - index - 1);

                       domains = domain.Split('.');

                       foreach (var item in publicdomains)

                       {

                           if(domains[0] == item)

                           {                              

                               found = true;

                               break;

                           }

                       }

                   }

                   else

                   {

                       emailid = string.Empty;

                   }

                       if (emailid != string.Empty)

                       {

                           QueryExpression checkcontact = new QueryExpression

                           {

                               EntityName = "contact",

                               ColumnSet = new ColumnSet("contactid", "ownerid", "parentcustomerid")

                           };

                           ConditionExpression checkemailid = new ConditionExpression();

                           checkemailid.AttributeName = "emailaddress1";

                           checkemailid.Operator = ConditionOperator.Equal;

                           checkemailid.Values.Add(emailid);

                           checkcontact.Criteria.AddCondition(checkemailid);

                           EntityCollection retrievecontact = service.RetrieveMultiple(checkcontact);

                           if (retrievecontact.Entities.Count > 0)

                           {

                               foreach (var item in retrievecontact.Entities)

                               {

                                   EntityReference entref = (EntityReference)item.Attributes["ownerid"];

                                   var id = entref.Id;

                                   Lead.Attributes["ownerid"] = new EntityReference("systemuser", id);

                                   EntityReference accountdetails = (EntityReference)item.Attributes["parentcustomerid"];

                                   var accountid = accountdetails.Id;

                                   Lead.Attributes.Add("parentaccountid", new EntityReference("account",accountid));

                                   Lead.Attributes.Add("parentcontactid", new EntityReference("contact", (Guid)item.Attributes["contactid"]));

                                   service.Update(Lead);

                               }

                           }

                           else

                           {

                               QueryExpression checkaccount = new QueryExpression

                               {

                                   EntityName = "account",

                                   ColumnSet = new ColumnSet("accountid", "ownerid")

                               };

                               ConditionExpression checkdomain = new ConditionExpression();

                               checkdomain.AttributeName = "new_domain";

                               checkdomain.Operator = ConditionOperator.Equal;

                               checkdomain.Values.Add(domains[0]);

                               checkaccount.Criteria.AddCondition(checkdomain);

                               if (found == false)

                               {

                                   EntityCollection retrieveaccount = service.RetrieveMultiple(checkaccount);                                

                                   if (retrieveaccount.Entities.Count > 0)

                                   {

                                       foreach (var item in retrieveaccount.Entities)

                                       {

                                           EntityReference entref = (EntityReference)item.Attributes["ownerid"];

                                           var id = entref.Id;

                                           Lead.Attributes["ownerid"] = new EntityReference("systemuser", id);

                                           Lead.Attributes.Add("parentaccountid", new EntityReference("account", (Guid)item.Attributes["accountid"]));

                                           service.Update(Lead);                                                                            

                                       }

                                   }

                               }

                           }

                       }

                   }

               }

               catch(InvalidPluginExecutionException ex)

               {

                   throw new InvalidPluginExecutionException(ex.Message);

               }

               catch(Exception ex)

               {

                   throw new Exception(ex.Message);

               }

           }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans