Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot create product price levels when creating price level

I'm trying to create a Price Level Object in Dynamics CRM through the api.

While I'm creating the Price Level Object (Price List) I want also to create Product Price Levels (Price List Items) with a so called deep insert but without success so far.

I'm following this guide.

This is a link to the price level entity type.

There is collection-valued navigation property for product price levels and is called price_level_product_price_levels

If I follow the tutorial I need to compose the following request

[POST] /pricelevels

The authorization headers are set correctly, and the content body is:

{
    "name": "MA - 2019W24",
    "paymentmethodcode": 1,
    "price_level_product_price_levels": [{
            "quantitysellingcode": 1,
            "amount": 89,
            "amount_base": 89,
            "pricingmethodcode": 1,
            "[email protected]": "/products(ef43bf1f-e605-e911-a818-000d3a2cd4e8)",
            "[email protected]": "/uoms(942b3c4c-e405-e911-a818-000d3a2cd4e8)"
        }
    ]
}

The error message in the response is:

pricelevel With Id = c0184273-ed8c-e911-a83b-000d3a2dd73b Does Not Exist

The UUID changes with every request. The stacktrace included in the response is:

at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.CreateInternal(Entity entity, InvocationContext invocationContext, CallerOriginToken callerOriginToken, WebServiceType serviceType, Boolean checkAdminMode, Dictionary`2 optionalParameters)
at Microsoft.Crm.Extensibility.OData.CrmODataExecutionContext.CreateOrganizationResponse(Entity entity)
at Microsoft.Crm.Extensibility.OData.CrmODataServiceDataProvider.CreateEdmEntity(CrmODataExecutionContext context, String edmEntityName, EdmEntityObject entityObject, Boolean isUpsert)
at Microsoft.Crm.Extensibility.OData.EntityController.PostEntitySetImplementation(String& entitySetName, EdmEntityObject entityObject)
at Microsoft.PowerApps.CoreFramework.ActivityLoggerExtensions.Execute[TResult](ILogger logger, EventId eventId, ActivityType activityType, Func`1 func, IEnumerable`1 additionalCustomProperties)
at Microsoft.Xrm.Telemetry.XrmTelemetryExtensions.Execute[TResult](ILogger logger, XrmTelemetryActivityType activityType, Func`1 func)
at lambda_method(Closure , Object , Object[] )
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()

I inspected the navigation properties for the account entity and for price level. The ones in the account are related properties and price_level_product_price_levels is a referential property.

If I try to execute the tutorial request in the first link it works.

Bulk create of Product Price Levels is not supported in Dynamics CRM.

I already tried batch actions but because I'm with java and using google-http-api. I find it pretty hard to meet the complex and specific contract for the batch action. Also information about such solution is really limited.

My goal is to create at least all the product price levels within the same request. Because else I might need to do like 20 request that create Product Price Levels in the crm.

like image 750
VasilenL Avatar asked Jun 11 '19 12:06

VasilenL


People also ask

What is price list in MS CRM?

Price lists are a feature within Dynamics CRM which works with quotes or sales order entities. A price list is selected to determine the price associated to the selected product. Each product within Microsoft Dynamics CRM can have multiple price lists offering considerable flexibility.


1 Answers

Batching works. Though I agree it is not very straight forward.

https://learn.microsoft.com/en-us/powerapps/developer/common-data-service/webapi/execute-batch-operations-using-web-api

Here is the modified snipped (note I use javascript, not java, but should be easy to convert).

var payload1 = {
    "name": "NEW - 01",
    "paymentmethodcode": 1,
    "pricelevelid": "7e3876b5-9066-4df4-9bb5-5cf276ccbad7"
};

var payload2 = {
    "[email protected]": "/pricelevels(7e3876b5-9066-4df4-9bb5-5cf276ccbad7)",
    "[email protected]": "/products(7687b3fb-b48e-e911-a82b-000d3a112148)",
    "[email protected]": "/uoms(43e8aed3-5631-4ee2-9cea-093637817cbb)"
};

var headers = {
  'Content-Type': 'multipart/mixed;boundary=batch_123456',
  'Accept': 'application/json',
  'Odata-MaxVersion': '4.0',
  'Odata-Version': '4.0'
};

var data=[]
data.push('--batch_123456');
data.push('Content-Type: multipart/mixed;boundary=changeset_BBB456');
data.push('');
data.push("--changeset_BBB456");
data.push("Content-Type: application/http");
data.push("Content-Transfer-Encoding:binary");
data.push("Content-ID: 1");
data.push('');
data.push('POST /api/data/v9.0/pricelevels HTTP/1.1');
data.push('Content-Type:application/json;type=entry');
data.push('');
data.push(JSON.stringify(payload1));
data.push("--changeset_BBB456");
data.push("Content-Type: application/http");
data.push("Content-Transfer-Encoding:binary");
data.push("Content-ID: 2");
data.push('');
data.push('POST /api/data/v9.0/productpricelevels HTTP/1.1');
data.push('Content-Type:application/json;type=entry');
data.push('');
data.push(JSON.stringify(payload2));
data.push('--changeset_BBB456--');
data.push('');
data.push('--batch_123456');


var payload = data.join('\r\n');

$.ajax({method:"POST",url:"/api/data/v9.0/$batch", data:payload, headers: headers });
like image 150
Ondrej Svejdar Avatar answered Oct 18 '22 02:10

Ondrej Svejdar