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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Cannot Create Opportunity, Nullable object must have a value.

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

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

  • Community Member Profile Picture
    on at

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

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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,

  • Daryl LaBar Profile Picture
    500 Most Valuable Professional on at

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

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 CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans