Forum Discussion

sajju's avatar
sajju
Contributing User
3 years ago
Solved

Getting Validation exception error while trying to post a service bill using .Net SDK API

Hi Guys,

 

I am trying to post a purchase bill service using .Net SDK. Getting a validation exception error. So far I was unable to figure out why. Can anybody guide me on this? Here's the code that I am using.

 

//Create new invoice of type Purchase(Bill)/Service
var billService = new ServiceBillService(GlobalConfig.ConfigurationLocal);
var serviceBill = new ServiceBill();
var supplierLink = new SupplierLink();
Guid supplierGuid = new Guid(supplierGuidString); // Ensure UID is of existing supplier

//Add supplier to invoice, add invoice number and date
supplierLink.UID = supplierGuid;
serviceBill.Supplier = supplierLink;
serviceBill.SupplierInvoiceNumber = master.InvoiceNo;
serviceBill.Date = DateTime.ParseExact(master.Date, "d", null);

//'Create list of invoice lines
var lines = new List<ServiceBillLine>();

 

foreach (var d in details)
{
//Add Line details
var line = new ServiceBillLine();
line.Type = InvoiceLineType.Transaction;

var accountLink = new AccountLink();
accountLink.UID = d.AccountUID;

var jobLink = new JobLink();
jobLink.UID = d.JobUID;

var taxCodeLink = new TaxCodeLink();
taxCodeLink.UID = d.TaxUID;

line.Account = accountLink;
line.Description = d.Description;
line.Job = jobLink;
line.TaxCode = taxCodeLink;
line.Total = d.Amount;

//Add line details to list of invoice line
lines.Add(line);

}

//Add service lines to service bill
serviceBill.Lines = lines;

//Push new invoice details (service bill) through to cf using insert method from the bill service
billService.Insert(GlobalConfig.CompanyFile, serviceBill, GlobalConfig.CompanyFileCredentials);

 

What am I doing here wrong?

 

Thanks

Sajju

 

  • I don't think those fields are required but you could try.

    My guess is that it it something to do with the line.Total. I think this is restricted to 2 decimal places.

     

    I haven't tried this with validation errors but I think if you cast your exception to ApiValidationException then you can access a collection of errors which may give you more information. Something like this:

     

    System.Collections.Generic.IList<MYOB.AccountRight.SDK.Error> ErrList = ((ApiValidationException)(TheException)).Errors;

4 Replies

Replies have been turned off for this discussion
  • Hi sajju,

     

    The InnerException may explain what the validation error is. Have a look at:

     

    catch (ApiCommunicationException ex) //Catch database exception
    {
        string myValErrStr= ex.InnerException.Message;
        return;
    }

     

     

  • sajju's avatar
    sajju
    Contributing User
    3 years ago

    InnerException.Message is "The remote server returned an error: (400) Bad Request.".

     

    Wonder whether we need to provide the following also for a service line

    //line.UnitCount = 0;
    //line.UnitOfMeasure = "";
    //line.UnitPrice = 0;
    //line.UnitPriceForeign = 0;
    //line.DiscountPercent = 0;

     

    Please clarify.

  • John_Dinning's avatar
    John_Dinning
    Trusted User
    3 years ago

    I don't think those fields are required but you could try.

    My guess is that it it something to do with the line.Total. I think this is restricted to 2 decimal places.

     

    I haven't tried this with validation errors but I think if you cast your exception to ApiValidationException then you can access a collection of errors which may give you more information. Something like this:

     

    System.Collections.Generic.IList<MYOB.AccountRight.SDK.Error> ErrList = ((ApiValidationException)(TheException)).Errors;

  • sajju's avatar
    sajju
    Contributing User
    3 years ago

    Thanks for your suggestion - 

    System.Collections.Generic.IList<MYOB.AccountRight.SDK.Error> ErrList = ((ApiValidationException)(ex)).Errors;

     

    Really helped me to pin point the cause - Freight Tax Code is required. Once this data was provided posting was through.

     

    I also corrected the decimal place for the line total field. Again many thanks.

     

    Cheers

    Sajju

     

Looking for something else?

Search the Community Forum for answers or find your topic and get the conversation started!

Community home

Level up your skills using MYOB software.

 

Find help guides and technical solutions for all MYOB products here

Explore video tutorials for our software at the MYOB Academy here

 

Dig into MYOB Academy for free courses, learning paths and live events to help build your business with MYOB.