gawri 7/31/2024 12:48:30 AM

Access to the operation has been denied. Microsoft.Dynamics.Com mon.Policy

Hi,

I am creating a .NET API when i encounter the error and in exception console I see the following:



CODE: 

 [HttpPost]
        [Route("Create")]
        public IHttpActionResult CreateInvoice([FromBody] PayablesInvoiceModel invoiceModel)
        {
           
            if (!ModelState.IsValid)
            {
                
                return BadRequest(ModelState);
            }
 
            try
            {
                DynamicsGPClient wsDynamicsGP = new DynamicsGPClient();
                // Create a context with which to call the web service
                Context context = new Context();
                // Specify which company
                CompanyKey companyKey = new CompanyKey();
                companyKey.Id = (-1);
                // Set up the context
                context.OrganizationKey = companyKey;
               
                
                
                PayablesInvoice payinv = new PayablesInvoice();
                PayablesDocumentKey payInvKey = new PayablesDocumentKey();
                payinv.Key = payInvKey;
                
                payinv.BatchKey = invoiceModel.batchId;
                payinv.Description = invoiceModel.description;
                payinv.VendorDocumentNumber = invoiceModel.vendorDocumentNumber;
                payinv.PONumber = invoiceModel.PONumber;
                payinv.PurchasesAmount = invoiceModel.PurchasesAmount;
                payinv.VendorKey = invoiceModel.VendorId;
                payinv.MiscellaneousAmount = invoiceModel.MiscellaneousAmount;
                payinv.FreightAmount = invoiceModel.FreightAmount;
             
                Policy payablesInvoicePolicy;
                
                
                payablesInvoicePolicy = wsDynamicsGP.GetPolicyByOperation("CreatePayablesInvoice", context);
                
                // Create the Invoice
                wsDynamicsGP.CreatePayablesInvoice(payinv, context, payablesInvoicePolicy );
                wsDynamicsGP.Close();
                return Created(new Uri(Request.RequestUri + "/"), invoiceModel);
            }
            catch (Exception ex)
            {
               
                // Log exception details here as needed
                return InternalServerError(ex); // Return 500 error code
            }
        }
 
Version: All
Section: .NET Development, APIs and Webhooks, Dynamics GP, Web Services


Table Definition Quick Links
All Tables
SOP Tables
RM Tables
GL Tables
POP Tables
HR Tables
PM Tables
UPR Tables
IV Tables
Olympic Tables
3