Purchase Order - Cannot set unit cost programmatically

I am trying to create purchase orders through coding, everything works except that the unit cost never gets saved! Here is my code snippet:

try
            {
                bo.GetNewPOHeader(ds);
                ds.Tables["POHeader"].Rows[0]["VendorNum"] = vendorNum;
                ds.Tables["POHeader"].Rows[0]["ShipViaCode"] = shipViaCode;
                bo.Update(ds);
                
                poNum = int.Parse(ds.Tables[0].Rows[0]["PONum"].ToString());

                for (int itr = 0; itr < dtLines.Rows.Count; itr++)
                {
                    bo.GetNewPODetail(ds, poNum);
                    ds.Tables["PODetail"].Rows[ds.Tables["PODetail"].Rows.Count - 1]["PartNum"] = dtLines.Rows[itr]["Part"].ToString().Trim();
                    ds.Tables["PODetail"].Rows[ds.Tables["PODetail"].Rows.Count - 1]["CalcOurQty"] = decimal.Parse(dtLines.Rows[itr]["Qty"].ToString());
                    ds.Tables["PODetail"].Rows[ds.Tables["PODetail"].Rows.Count - 1]["CalcVendQty"] = decimal.Parse(dtLines.Rows[itr]["Qty"].ToString());
                    ds.Tables["PODetail"].Rows[ds.Tables["PODetail"].Rows.Count - 1]["cf_ShopOrderNum_C1380_c"] = int.Parse(dtLines.Rows[itr]["ShopOrderNum"].ToString());

                    ds.Tables["PODetail"].Rows[ds.Tables["PODetail"].Rows.Count - 1]["UnitCost"] = 1;
                    ds.Tables["PODetail"].Rows[ds.Tables["PODetail"].Rows.Count - 1]["DocUnitCost"] = 1;
                    ds.Tables["PODetail"].Rows[ds.Tables["PODetail"].Rows.Count - 1]["DocScrUnitCost"] = 1;
                    bo.Update(ds);
                }
            }
            catch(Exception e)
            {
                _log.Log(e.Message);
                _log.Log(e.ToString());
                errMessage = string.Format("{0}{1}{1}{2}", e.Message, Environment.NewLine, e.ToString());
            }

Any idea what I am missing? As you can see I have tried three different fields and have also tried them individually but no luck.

@Samm Please format your code with ``` before and after.

I set DocUnitCost, UnitCost and also OverridePriceList = true when I am changing POs

“Please format your code with ``` before and after.”

You mean to put the unit cost inside double quotes?

No. The code snippit you posted needs the three grave accent to look proper on the post.

Didn’t know that! Fixed it. Thanks!

I tried that and still doesn’t work. Unit cost or cost won’t update and remain at 0.

@Samm Is this following a trace you did? The code of mine I use to change existing costs all of the time. Is it possible you have to make the new line and then set its cost after it exists?

Good idea! I am going to give this a try.

Unfortunately that doesn’t work either.

I figured it out:


            string message = string.Empty;
            bo.ChangeUnitPriceConfirmOverride(ds, out message);
            ds.Tables["PODetail"].Rows[ds.Tables["PODetail"].Rows.Count - 1]["CurrencySwitch"] = false;
            ds.Tables["PODetail"].Rows[ds.Tables["PODetail"].Rows.Count - 1]["DocScrUnitCost"] = 100;
            bo.ChangeUnitPrice(ds);
            bo.Update(ds);