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
Is there a stack trace? Does the call make it to CRM and then error in a plugin or is it client side?
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,
Added TransactionCurrencyId entity reference to the opportunity, still getting the same error.
Hello,
Try to populate TransactionCurrencyId lookup. You use money field without mentioned lookup populated. This could lead to issue.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,134 Super User 2024 Season 2
Martin Dráb 229,928 Most Valuable Professional
nmaenpaa 101,156