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

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested Answer

An unexpected error occurred while creating contact record

(0) ShareShare
ReportReport
Posted on by 20

Hello All,

We are experiencing a strange error in the below mentioned code

var record = new Contact
{

FirstName = DataGenerator.RandomName(),
LastName = DataGenerator.RandomName(),
BirthDate = birthdate.AddDays(1),

Custom_Attribute1= XXXXX

}

var createRequest = new CreateRequest
{
Target = record,
};

record.Id = ((CreateResponse)context.OrganizationService.Execute(createRequest)).id;

The error occurrs intermittently and it is

An unexpected error occurred. NullReferenceException

at the above highlighted line, some time the record is created without any error.

Is this because some of the attributes missing when creating the record?

We are using Dynamics 365 on premise Version 1612 (8.2.28.11) (DB 8.2.28.11) on-premises.

There are some plugins on OnCreate Pre and Post Operation of the Contact, and the aove mentioned code is in the Web API which is used by some application.

If anyone has some inputs with such similar issue, please let me know.

Regards,

Abhi.

I have the same question (0)
  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at
    RE: An unexpected error occurred while creating contact record

    Hi,

    Please check Plugin trace log if any error logged due to your custom plugin on Contact Entity.

    I believe error is coming from your custom sync plugin. Otherwise your code looks good to me.

    Also, please change your last line of code as below -

    var response = ((CreateResponse)context.OrganizationService.Execute(createRequest));

    record.Id = response.id;

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • Suggested answer
    Nya Profile Picture
    29,060 on at
    RE: An unexpected error occurred while creating contact record

    Hi Abhi,

    Here is an example of using CreateRequest with IOrganizationService.Execute.

    CreateRequest request = new CreateRequest()
    { Target = account };
    var id = ((CreateResponse)svc.Execute(request)).id;

  • Abhi R Profile Picture
    20 on at
    RE: An unexpected error occurred while creating contact record

    Tried to create manually the Contact it works some times without error

    some times unandled exception occurs

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: An unexpected error occurred.Detail:

    <OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance" xmlns="schemas.microsoft.com/.../Contracts">

     <ActivityId>da1bbf7e-3240-4012-b2f5-9e6b298063fc</ActivityId>

     <ErrorCode>-2147220970</ErrorCode>

     <ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic" />

     <Message>An unexpected error occurred.</Message>

     <Timestamp>2022-01-18T10:04:58.9840761Z</Timestamp>

     <ExceptionRetriable>false</ExceptionRetriable>

     <ExceptionSource i:nil="true" />

     <InnerFault i:nil="true" />

     <OriginalException i:nil="true" />

     <TraceText i:nil="true" />

    </OrganizationServiceFault>

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 365 | Integration, Dataverse, and general topics

#1
#ManoVerse Profile Picture

#ManoVerse 101

#2
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 66 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans