davidyack / Xrm.Tools.CRMWebAPI

This is an API helper for working with the Common Data Service (CDS) Web API
MIT License
143 stars 73 forks source link

Unable to execute custom bound Action on the product entity #84

Closed awisey closed 4 years ago

awisey commented 5 years ago

I use the following code on the standard sdk dll's to execute a custom action on the product entity and pass a few input parameters.

OrganizationRequest request = new OrganizationRequest("new_GetProductBuyPrice"); request["Target"] = new EntityReference("product", new Guid(ProductID)); request["Account"] = new EntityReference("account", new Guid(AccountID)); request["Currency"] = new EntityReference("transactionalcurrency", new Guid(CurrencyID)); request["Qty"] = 1.00m;

OrganizationResponse response = Xrm.XrmSvc.Execute(request); UnitBuy = Math.Round(((Money)response.Results["BuyPrice"]).Value, 2); DiscountReason = response.Results.Contains("DiscountReason") ? response.Results["DiscountReason"].ToString() : string.Empty;

How can I use your wrapper to execute my action?

Kind regards Andrew

davidyack commented 5 years ago

You would use the ExecuteAction method some examples are here https://github.com/davidyack/Xrm.Tools.CRMWebAPI/wiki/Action-Examples

awisey commented 5 years ago

Hi David,

Thank you for your quick response.

I had a look at that page and have since tried a few ways of getting my action to execute but still have had no success.

My Action Definition is as follows: ActionDefinition

When I try: ` Guid ProductId = new Guid("04bcd05c-68b7-e811-810f-00155d800805"); Guid AccountId = new Guid("86EED9E7-9C7F-DF11-A68D-0024E86C3D84"); Guid CurrencyId = new Guid("ADD66B0F-F875-DE11-B40F-0024E86C3D84");

            var actionParams = new
            {
                Account = string.Format("/accounts({0})", AccountId),
                Currency = string.Format("/transactioncurrencies({0})", CurrencyId),
                Qty = 1.00m
            };
            var response = await _crmClient.API.ExecuteAction("new_GetProductBuyPrice", "products", ProductId, actionParams);`

I get the following error: "Xrm.Tools.WebAPI.Results.CRMWebAPIException: Request message has unresolved parameters."

When I try: ` Guid ProductId = new Guid("04bcd05c-68b7-e811-810f-00155d800805"); Guid AccountId = new Guid("86EED9E7-9C7F-DF11-A68D-0024E86C3D84"); Guid CurrencyId = new Guid("ADD66B0F-F875-DE11-B40F-0024E86C3D84");

            var actionParams = new
            {
                Target = string.Format("/products({0})", AccountId),
                Account = string.Format("/accounts({0})", AccountId),
                Currency = string.Format("/transactioncurrencies({0})", CurrencyId),
                Qty = 1.00m
            };
            var response = await _crmClient.API.ExecuteAction("new_GetProductBuyPrice", actionParams);`

I get the following error: Xrm.Tools.WebAPI.Results.CRMWebAPIException: Resource not found for the segment 'new_GetProductBuyPrice'

I have you got any idea what I could try next?

Kind regards Andrew

awisey commented 5 years ago

So the following code seems to trigger the action. The only thing now i can't seem to pass through is the EntityReference parameters.

` Guid ProductId = new Guid("04bcd05c-68b7-e811-810f-00155d800805"); Guid AccountId = new Guid("86EED9E7-9C7F-DF11-A68D-0024E86C3D84"); Guid CurrencyId = new Guid("ADD66B0F-F875-DE11-B40F-0024E86C3D84");

            dynamic AccountRef = new Dictionary<String, object>();
            AccountRef["@odata.type"] = "Microsoft.Dynamics.CRM.account";
            AccountRef["@account.id"] = AccountId.ToString();

            dynamic CurrencyRef = new Dictionary<String, object>();
            CurrencyRef["@odata.type"] = "Microsoft.Dynamics.CRM.transactioncurrency";
            CurrencyRef["@account.id"] = CurrencyId.ToString();

            var actionParams = new
            {
                Account = AccountRef,
                Currency = CurrencyRef,
                Qty = 1.00m
            };
            var response = await _crmClient.API.ExecuteAction("Microsoft.Dynamics.CRM.new_GetProductBuyPrice", "products", ProductId, actionParams);

` How can i pass these through to the action correctly as they seem to be null when the action triggers?

awisey commented 5 years ago

Finally Figured it out, so for anyone who manages to stumble pass this post, passing in a entityreference as a parameter you just need the id column lowercase and the type as follows:

dynamic AccountRef = new Dictionary<String, object>(); AccountRef["@odata.type"] = "Microsoft.Dynamics.CRM.account"; AccountRef["accountid"] = AccountId.ToString();

            dynamic CurrencyRef = new Dictionary<String, object>();
            CurrencyRef["@odata.type"] = "Microsoft.Dynamics.CRM.transactioncurrency";
            CurrencyRef["transactioncurrencyid"] = CurrencyId.ToString();

            var actionParams = new
            {
                Account = AccountRef,
                Currency = CurrencyRef,
                Qty = 1.00m
            };
            var response = await _crmClient.API.ExecuteAction("Microsoft.Dynamics.CRM.new_GetProductBuyPrice", "products", ProductId, actionParams);