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

Announcements

News and Announcements icon
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,985 Moderator on at

    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,064 on at

    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

    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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Subra Profile Picture

Subra 128

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 99 Super User 2026 Season 1

#3
11manish Profile Picture

11manish 86

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans