Updating TaxRate record

Hello,

I am having trouble writing code to update a TaxRate record in Quantify. My code inserts a new record just fine, but when trying to update, I run into a ‘Can not directly save a child object’ error.

I am confused because this exact same code works for upserting Customers and Vendors. Are TaxRate objects somehow different and cannot be updated the same way as BusinessPartner objects?

Thanks in advance for any assistance you can provide on this one.
Alex

Code snippet below. In the TaxCodeValidateAndSave() method, I attempt to run TaxRate.Save() and that is where the error throws.

//***** Try to Deserialize to Class object and Process Data *****
try
{
//***** Log on to Quantify *****
QuantHelper.QuantifyLogin();

            //***** Deserialize JObject to create class we can work with *****
            TaxCodeRootClass myDeserializedClass = jsonResult.ToObject<TaxCodeRootClass>();

            //***** Define fields *****
            string TaxCode = myDeserializedClass.TaxCodeData.tax_code;
            string TaxCodeDescription = myDeserializedClass.TaxCodeData.tax_code_description;
            string TaxCodeState = myDeserializedClass.TaxCodeData.tax_code_state;
            string TaxCodeRate = myDeserializedClass.TaxCodeData.tax_code_rate;

            //***** Query list of all Tax Rates to loop through and compare when Tax Code record is incoming ***** 
            TaxRateCollection myTaxCodeCollection = TaxRateCollection.GetTaxRateCollection(ActiveStatus.Both, false, Guid.Empty);

            //***** Loop through full list of Tax Rates and update matching record if one exists *****
            TaxRate inputTaxCode = TaxRate.NewTaxRate();
            foreach (TaxRate myTaxCode in myTaxCodeCollection)
            {
                if (myTaxCode.RefID == TaxCode)
                {
                    //***** Update existing TaxCode ***** 
                    myTaxCode.Name = TaxCodeState + " - " + TaxCodeDescription;
                    myTaxCode.Rate = Convert.ToDouble(TaxCodeRate) * 100;  // Need to multiple by 100 since WebApps passes in fraction and we need percent;
                    myTaxCode.RefID = TaxCode;

                    //***** Assign to TaxCode object for writing to database *****
                    inputTaxCode = myTaxCode;
                    break;
                }
                else
                {
                    continue;
                }
            }

            //***** If no matching record was found, fill out required fields and save new record *****
            if (inputTaxCode.IsNew)
            {
                inputTaxCode.Name = TaxCodeState + " - " + TaxCodeDescription;
                inputTaxCode.Rate = Convert.ToDouble(TaxCodeRate) * 100;  // Need to multiple by 100 since WebApps passes in fraction and we need percent
                inputTaxCode.RefID = TaxCode;
            }

            //***** Validate and save the TaxCode record ***** 
            TaxCodeResponse = TaxCodeValidateAndSave(inputTaxCode);
        }

Hi Alex.

In this case, you encountered this because the following assignment in the code makes inputTaxCode a child of the collection that does not allow direct save -

//***** Assign to TaxCode object for writing to database *****
inputTaxCode = myTaxCode;

Instead if the following was the case you should be able to save.

inputTaxCode = TaxRate.GetTaxRate(name or id);

Thanks for posting.

Thanks so much for the quick response - that makes sense and the alternative approach worked. I’m glad this was an easy one!

Alex