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

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

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 May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans