Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Cannot Create Opportunity, Nullable object must have a value.

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hi, 

Trying to create an opportunity using the Dynamics 365 v9 SDK. I can create other objects fine, like PriceLevel and Account, but cannot create an opportunity.

 using (var _serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null))
            { 
var _service = (IOrganizationService)_serviceProxy;
                _serviceProxy.EnableProxyTypes();

                var pricelevel2 = _service.Retrieve("pricelevel", Guid.Parse(price_level_id), new ColumnSet(true));
                var owner = _service.Retrieve("systemuser", Guid.Parse(system_user_id), new ColumnSet(true));
                var _account = _service.Retrieve("account", Guid.Parse(account_id), new ColumnSet(true));
               
                Opportunity newOpportunity = new Opportunity
                {
                    Id = Guid.NewGuid(),
                    OpportunityId = Guid.NewGuid(),
                    Name = "Example Opportunity",
                    PriceLevelId = pricelevel2.ToEntityReference(),
                    OwnerId = owner.ToEntityReference(),
                    CustomerId = _account.ToEntityReference(),
                    ParentAccountId = _account.ToEntityReference(),
                    EntityState = EntityState.Created,
                    IsRevenueSystemCalculated = false,
                    EstimatedValue = new Money(400.00m),
                    FreightAmount = new Money(10.00m),
                    DiscountAmount = new Money(0.10m),
                    DiscountPercentage = 0.20m
                };

                var _opportunityId = _serviceProxy.Create(newOpportunity);
}


I get an error "Nullable object must have a value.'" but I have checked the Opportunity object at https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/entities/opportunity#entity-properties and all SystemRequired objects should be filled in.

*This post is locked for comments

  • Daryl LaBar Profile Picture
    Daryl LaBar 500 Most Valuable Professional on at
    RE: Cannot Create Opportunity, Nullable object must have a value.

    Is there a stack trace?  Does the call make it to CRM and then error in a plugin or is it client side?

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Cannot Create Opportunity, Nullable object must have a value.

    I never liked early-binding.

    Here is almost the same code written using late-binding and it works fine for me:

               var opportunity = new Entity("opportunity")

               {

                   ["name"] = "Example Opportunity",

                   ["pricelevelid"] = priceLevel.ToEntityReference(),

                   ["customerid"] = account.ToEntityReference(),

                   ["parentaccountid"] = account.ToEntityReference(),

                   ["isrevenuesystemcalculated"] = false,

                   ["estimatedvalue"] = new Money(400m),

                   ["freightamount"] = new Money(10m),

                   ["discountamount"] = new Money(0.1m),

                   ["discountpercentage"] = 0.2m

               };

               var oppId = service.Create(opportunity);

    Regarding why your code doesn't work... try to remove following lines:

    Id = Guid.NewGuid(),

    OpportunityId = Guid.NewGuid(),

    and

    EntityState = EntityState.Created,

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Cannot Create Opportunity, Nullable object must have a value.

    Added TransactionCurrencyId entity reference to the opportunity, still getting the same error.

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Cannot Create Opportunity, Nullable object must have a value.

    Hello,

    Try to populate TransactionCurrencyId lookup.  You use money field without mentioned lookup populated. This could lead to issue.

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

Featured topics

Product updates

Dynamics 365 release plans