Skip to main content

Notifications

Announcements

No record found.

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.

  • Abhi R Profile Picture
    Abhi R 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>

  • Suggested answer
    Nya Profile Picture
    Nya 29,056 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;

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 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/

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,391 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans