Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Generic sql error

(0) ShareShare
ReportReport
Posted on by 155

I have a requirement where in a contact is associated to an office(account),firm(custom entity) via lookup. On contact entity, we have a field that stores the office,firm identification number. As soon as the contact records is created (or) office/firm identification numbers gets updated, we have plugin that fires and checks if the identification number exist then gets the office/firm guid and fills the lookup on contact entity. If the identification number doesn't exist, then it creates a new record on office/firm entity and gets the guid to fill contact lookup. Registered my plugin on both create and update messages.

My code works good when it is created via UI by user but fails when multiple contact records are loaded via SSIS. Plugin is succeeding for few and fails for others. Trace logs doesn't give much information except Generic SQL error.

Here is my code. Please suggest me if there is any mistake in the code.

try
                {
                    // Get a reference to the Organization service.
                    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                    if (entity.Attributes.Contains("st_firmidentificationnumber") || entity.Attributes.Contains("st_officeidentificationnumber"))
                    {
                        if (entity.Attributes.Contains("st_firmidentificationnumber"))
                        {
                            QueryExpression query = new QueryExpression
                            {
                                EntityName = "st_firm",
                                ColumnSet = new ColumnSet("st_firmname")
                            };
                            query.Criteria.AddCondition("st_firmidentificationnumber", ConditionOperator.Equal, entity.Attributes["st_firmidentificationnumber"]);

                            EntityCollection result = service.RetrieveMultiple(query);
                            tracingService.Trace("result count : {0}", result.Entities.Count.ToString());
                            if (result.Entities.Count == 1)
                            {
                                tracingService.Trace("matching firm found");
                                entity["st_firmid"] = new EntityReference("st_firm", result.Entities[0].Id); }

                            else if (result.Entities.Count == 0)
                            {
                                tracingService.Trace("matching firm not found");
                                Entity Firm = new Entity("st_firm");
                                Firm["st_firmidentificationnumber"] = entity.Attributes["st_firmidentificationnumber"].ToString();
                                Guid Firmid = service.Create(Firm);
                                entity["st_firmid"] = new EntityReference("st_firm", Firmid);
                            }
                            
                        }

                        if (entity.Attributes.Contains("st_officeidentificationnumber"))
                        {
                            QueryExpression query = new QueryExpression
                            {
                                EntityName = "account",
                                ColumnSet = new ColumnSet("name")
                            };
                            query.Criteria.AddCondition("st_officeidentificationnumber", ConditionOperator.Equal, entity.Attributes["st_officeidentificationnumber"]);

                            EntityCollection result = service.RetrieveMultiple(query);
                            tracingService.Trace("result count : {0}", result.Entities.Count.ToString());
                            if (result.Entities.Count == 1)
                            {
                                tracingService.Trace("matching office found");
                                entity["parentcustomerid"] = new EntityReference("account", result.Entities[0].Id);
                            }

                            else if (result.Entities.Count == 0)
                            {
                                tracingService.Trace("matching office not found");
                                Entity Office = new Entity("account");
                                Office["st_officeidentificationnumber"] = entity.Attributes["st_officeidentificationnumber"].ToString();
                                Guid Officeid = service.Create(Office);
                                entity["parentcustomerid"] = new EntityReference("account", Officeid);
                            }
                           
                        }

                        service.Update(entity);
                        tracingService.Trace("Update Complete");
                    }
                }


Here is the trace log :

Error Message:

Unhandled Exception: Microsoft.Xrm.Sdk.InvalidPluginExecutionException: An error occured in PostCrdNumberUpdateOfficeOrFirmDetails plugin.
   at STCRM.Plugins.PostCrdNumberUpdateOfficeOrFirmDetails.Execute(IServiceProvider serviceProvider)
   at Microsoft.Crm.Asynchronous.EventOperation.InvokePlugin(AsyncExecutionContext context, IPlugin pluginInstance)
Inner 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: 
<OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance" xmlns="schemas.microsoft.com/.../Contracts">
  <ErrorCode>-2147204784</ErrorCode>
  <ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic" />
  <Message>Generic SQL error.</Message>
  <Timestamp>2018-10-16T16:26:13.8141024Z</Timestamp>
  <ExceptionRetriable>false</ExceptionRetriable>
  <ExceptionSource i:nil="true" />
  <InnerFault>
    <ErrorCode>-2147204784</ErrorCode>
    <ErrorDetails xmlns:d3p1="schemas.datacontract.org/.../System.Collections.Generic" />
    <Message>Generic SQL error.</Message>
    <Timestamp>2018-10-16T16:26:13.8141024Z</Timestamp>
    <ExceptionRetriable>false</ExceptionRetriable>
    <ExceptionSource i:nil="true" />
    <InnerFault>
      <ErrorCode>-2147204784</ErrorCode>
      <ErrorDetails xmlns:d4p1="schemas.datacontract.org/.../System.Collections.Generic" />
      <Message>Generic SQL error.</Message>
      <Timestamp>2018-10-16T16:26:13.8141024Z</Timestamp>
      <ExceptionRetriable>false</ExceptionRetriable>
      <ExceptionSource i:nil="true" />
      <InnerFault>
        <ErrorCode>-2147220970</ErrorCode>
        <ErrorDetails xmlns:d5p1="schemas.datacontract.org/.../System.Collections.Generic" />
        <Message>System.Data.SqlClient.SqlException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #B364B63B</Message>
        <Timestamp>2018-10-16T16:26:13.8141024Z</Timestamp>
        <ExceptionRetriable>false</ExceptionRetriable>
        <ExceptionSource i:nil="true" />
        <InnerFault i:nil="true" />
        <OriginalException i:nil="true" />
        <TraceText i:nil="true" />
      </InnerFault>
      <OriginalException i:nil="true" />
      <TraceText i:nil="true" />
    </InnerFault>
    <OriginalException i:nil="true" />
    <TraceText i:nil="true" />
  </InnerFault>
  <OriginalException i:nil="true" />
  <TraceText i:nil="true" />
</OrganizationServiceFault>
   at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Update(Entity entity, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType, Boolean checkAdminMode, Boolean checkForOptimisticConcurrency, Dictionary`2 optionalParameters)
   at Microsoft.Crm.Extensibility.InprocessServiceProxy.UpdateCore(Entity entity)


Thanks..

*This post is locked for comments

  • Joana Pinto Profile Picture
    740 on at
    RE: Generic sql error

    Did you check the size of attribute "st_officeidentificationnumber"? Sometimes you have SQL Generic Error when you update a text field with a string that has a size superior to the one defined on the field. Like, if your string has 200 of length and the field is defined with a max of 100 length.

  • rath.amit38@gmail.com Profile Picture
    2 on at
    RE: Generic sql error

    Hi Itz_Me_Ram,

    As suggested by Shivam and Ben, can you please try to adjust the batch size and let us know the result.

    Thank you.

    Amit Kumar Rath

  • Community Member Profile Picture
    on at
    RE: Generic sql error

    Hi

    When you run your SSIS packages and if you have a plugin performing some operation on the records created through SSIS than this type of error will occur because the speed at which SSIS create record in CRM is faster than Plugin can take it even though plugin take it from memory once loaded.

    Ideally we should not run any Plugin or Workflow when SSIS packages are running because it will effect Perform of your CRM application.

    To avoid this I would recommend to adjust the batch size in SSIS package and do the changes in your plugin suggested by  @Ben Thompson.

    Hope this will help !!!

  • Suggested answer
    Ben Thompson Profile Picture
    6,350 on at
    RE: Generic sql error

    My immediate thought would be is the SSIS script sending in a number of records which have null values in the st_firmidentificationnumber or st_officeidentificationnumber fields as that would generate requests where SQL is looking for st_firmidentificationnumber = null rather than st_firmidentificationnumber  is null (and that would generate a SQL error).

    To resolve this you will need something like if ((entity.Attributes.Contains("st_firmidentificationnumber") && entity.Attributes.GetAttributeValue<entityReference>("st_firmidentificationnumber")!= null) || (entity.Attributes.Contains("st_officeidentificationnumber") && entity.Attributes.GetAttributeValue<entityReference>("st_officeidentificationnumber")!= null)) if all you if statements to exclude null records...

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
dkrishna Profile Picture

dkrishna 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans