BPM to add QuoteHedMsc

Hi all! I’m trying to make a BPM to create a new Header Misc Charge in the Quote.

Here it is ‘my’ code:

string msg = '';
using (var svc = Ice.Assemblies.ServiceRenderer.GetService<Erp.Contracts.QuoteSvcContract>(this.Db))
            {
                Erp.Tablesets.UpdExtQuoteTableset ds = new Erp.Tablesets.UpdExtQuoteTableset(); // tableset for update data
                bool errorOccurred;
                
                Erp.Tablesets.QuoteHedRow qhrow = new Erp.Tablesets.QuoteHedRow // minimum required for QuoteHed
                {
                    Company = Session.CompanyID,
                    QuoteNum = quotenum
                };
                ds.QuoteHed.Add(qhrow);
                msg = "Adding Misc Charge " + quotenum;
                this.PublishInfoMessage(msg, Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual, "", "");
                Erp.Tablesets.QuoteHedMscRow mscrow = new Erp.Tablesets.QuoteHedMscRow // new values for header Misc Charge
                {
                    Company = Session.CompanyID,
                    QuoteNum = quotenum,
                    //QuoteLine = 1,
                    //QtyNum = 1,
                    SeqNum = 0,
                    MiscCode = "Extra verniciatura",
                    Description = "Extra verniciatura",
                    MiscAmt = 500,
                    DocMiscAmt = 500,
                    FreqCode = "E"
                };
                //mscrow.SetUDField<System.Boolean>("50",(bool)true);
                ds.QuoteHedMsc.Add(mscrow);
                
                try
                {
                    BOUpdErrorTableset boUpdateErrors = svc.UpdateExt(ref ds, true, true, out errorOccurred); // hold any returned errors
                    if (errorOccurred)
                    {
                        if (boUpdateErrors != null && boUpdateErrors.BOUpdError.Count > 0)
                        {
                            msg = msg + System.Environment.NewLine + boUpdateErrors.BOUpdError[0].ErrorText + " (" + boUpdateErrors.BOUpdError[0].TableName + ")";
                        }
                    }
                    else
                    {
                        Erp.Tablesets.QuoteTableset dsSO = svc.GetByID(quotenum);
                        if (dsSO != null)
                        {
                            this.dsHolder.Attach(dsSO);
                            if (dsSO.QuoteMsc.Count > 0)
                            {
                                msg = msg + System.Environment.NewLine + dsSO.QuoteMsc[0].MiscAmt.ToString();
                            }
                        }
                        msg = string.Empty; // remove error record if all completed successfully
                    }
                    this.PublishInfoMessage(msg, Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual, "", "");
                }
                catch (Exception e)
                {
                    msg = msg + System.Environment.NewLine + "Update Exception " + e.Message;
                    this.PublishInfoMessage(msg, Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual, "", "");
                }
            }

I tried to follow the steps in the other discussions but
this doesn’t work, every time it throws me a
“Adding Misc Charge xxxxxxx
There are no records in the table ds.QuoteHed (QuoteHedMsc)”
error.

I can’t understand why, can someone help me please?

Thanks!

Give RowMod = “A” a try.

I tried to add
RowMod = “A” in the Erp.Tablesets.QuoteHedMscRow but nothing changed

You need a row in the QuoteHed table in the dataset even though you’re not changing anything in it.

If your quote has already been created then try using the Erp.Quote.GetById to populate the QuoteTableSet

Once you have the table set add a new record using Erp.Quote.GetNewQuoteHedMsc

Populate fields with the relevant values (LineNum = 0 and qtyNum = 0) and set RowMod to “A”

image
Execute Erp.Quote.Update

I’ve done this in a BPM and it all works OK

Sorry I’m trying to undestand, could you be more specific please?

I tried to to this:

It doesn’t show any error but it’s not creating the misccharghe line

here is the working code modify as you required

string msg = string.Empty;
var ttQuoteHed_Row = (from row in ttQuoteHed select row).FirstOrDefault();
//where row.Added() || row.Updated() 
if (ttQuoteHed_Row != null)
{
    var quoteNum = ttQuoteHed_Row.QuoteNum;
    msg += quoteNum.ToString();
    using (var quoteSvc = Ice.Assemblies.ServiceRenderer.GetService<Erp.Contracts.QuoteSvcContract>(this.Db))
    {
      // This code adds / updates a header Misc Freight Charge        
      Erp.Tables.QuoteMsc OrderMsc;
      int quoteLine = 0;
      int qtyNum = 0;
      string sMiscCode = "FRGT";
      string sMiscDesc = "Freight";
      string sMiscFreq = "F";
      decimal dTotAmt = 110.0m;
      var soTs = new QuoteTableset();
        // Check if the Freight Record exists already
      var QuoteMsc_Row =  (from row in Db.QuoteMsc where row.QuoteNum == quoteNum && row.MiscCode == sMiscCode && (string.Compare(row.Company, Session.CompanyID, true) == 0 ) select row).FirstOrDefault(); 
      if ((QuoteMsc_Row == null))
      {
          quoteSvc.GetNewQuoteHedMsc(ref soTs, quoteNum, quoteLine, qtyNum); 
          var orderMscNew = soTs.QuoteHedMsc.FirstOrDefault();  
          orderMscNew.MiscCode = sMiscCode;
          orderMscNew.Description = sMiscDesc;
          orderMscNew.FreqCode = sMiscFreq;   
          quoteSvc.GetMiscChrgDefaults(ref soTs, "QuoteHedMsc"); 
          orderMscNew = soTs.QuoteHedMsc.FirstOrDefault();  
          orderMscNew.MiscAmt = dTotAmt;  
          orderMscNew.DocMiscAmt  = dTotAmt; 
          msg += "Added New";
      }
      quoteSvc.Update(ref soTs);
      this.dsHolder.Attach(soTs);
    }
}
this.PublishInfoMessage(msg,Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual,"","");
2 Likes

Hi! Thanks for the answer!
I modified the code (Only for the fields of the Misc Charge)
but it keeps sayin ‘There are no records in the table ds.QuoteHed’

string msg = string.Empty;
var ttQuoteHed_Row = (from row in ttQuoteHed select row).FirstOrDefault();
//where row.Added() || row.Updated() 
if (ttQuoteHed_Row != null)
{
    var quoteNum = ttQuoteHed_Row.QuoteNum;
    msg += quoteNum.ToString();
    using (var quoteSvc = Ice.Assemblies.ServiceRenderer.GetService<Erp.Contracts.QuoteSvcContract>(this.Db))
    {
      // This code adds / updates a header Misc Freight Charge        
      Erp.Tables.QuoteMsc OrderMsc;
      int quoteLine = 0;
      int qtyNum = 0;
      string sMiscCode = "EX";
      string sMiscDesc = "Extra vernicitura";
      string sMiscFreq = "E";
      decimal dTotAmt = 500;
      var soTs = new QuoteTableset();
        // Check if the Freight Record exists already
      var QuoteMsc_Row =  (from row in Db.QuoteMsc where row.QuoteNum == quoteNum && row.MiscCode == sMiscCode && (string.Compare(row.Company, Session.CompanyID, true) == 0 ) select row).FirstOrDefault(); 
      if ((QuoteMsc_Row == null))
      {
          quoteSvc.GetNewQuoteHedMsc(ref soTs, quoteNum, quoteLine, qtyNum); 
          var orderMscNew = soTs.QuoteHedMsc.FirstOrDefault();  
          orderMscNew.MiscCode = sMiscCode;
          orderMscNew.Description = sMiscDesc;
          orderMscNew.FreqCode = sMiscFreq;   
          quoteSvc.GetMiscChrgDefaults(ref soTs, "QuoteHedMsc"); 
          orderMscNew = soTs.QuoteHedMsc.FirstOrDefault();  
          orderMscNew.MiscAmt = dTotAmt;  
          orderMscNew.DocMiscAmt  = dTotAmt; 
          msg += "Added New";
      }
      quoteSvc.Update(ref soTs);
      this.dsHolder.Attach(soTs);
    }
}
this.PublishInfoMessage(msg,Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual,"","");

Where you are trying to call this code?

I did a customization (code) that calculates the price for the Misc Charge and put in a custom field QuoteHed.ExtraVern_c.
Then with the BPM Erp.Quote.Update (on the condition that when the value of ExtraVern_c is changed it starts) I need to create a Misc Charge.

Erp.Quote.Update pre or post processing?

I’m tryin Post-Processing

I use a pre-processing directive on the UD12.Update method.

Once you have created the new record you need to assign it values (as you have done). However, I note that you have not the RowMod to ‘A’

Code is one way, but using the ‘Invoke BO method’ widget can make things easier to work with and easier to maintain.

You need to add the data to the new record you have created

I use the ‘Update Table By Query’ widget to do this

.

The mapping is my last post

Thanks to @surendrapal I was able to solve it.

Thanks again man :smiley:

Hi @surendrapal, thank you for sharing the misc charge code. I have implemented and have it working to a degree. A couple of things that I have tried to do but without success is to modify the MiscAmt if the charge already exists or to delete the charge altogether if certain criteria on the quote are met. Could you possibly give me some help / advice?
Best regards
Adrian

Hi, I am trying to do the same thing as yourself but to add a charge and struggling, would you mind sharing what you have done such as I am presuming it is a post processing but are you having a condition then placing the code or invoking different methods then custom code?

I have managed to create the same on sales order entry but as this invokes masterupdate it was a lot easier to call the methods then use custom code.

To delete the carriage charge I had it based on a ud field being true,
invoke get by id,
custom code dsSalesOrder.OHOrderMsc.Where(row => row.MiscCode == "crr").FirstOrDefault().RowMod = "D";
invoke sales update
custom code dsSalesOrder.OHOrderMsc.Remove(dsSalesOrder.OHOrderMsc.Where(row => row.MiscCode == "crr").FirstOrDefault());
invoke sales update again

hope this helps

Hi Kirsty,
I have it working as I want it now. Thanks to @surendrapal
In my case, I want to apply a freight charge to quotes over a certain value but certain lines on the quote may be exempt from the charge. I used a customization on the quote to give me the value and if it was over the amount set I placed it in Freight_c, if not I placed zero in Freight_c. I then had a pre proc method directive with a condition Freight_c changes from any to another, invoking a post process directive with just the custom code below.
Hope this helps you
Adrian.

var ttQuoteHed_Row = ttQuoteHed.FirstOrDefault();
if (ttQuoteHed_Row == null)
  return; 
var quoteNum = ttQuoteHed_Row.QuoteNum;
decimal freightCharge = Convert.ToDecimal(ttQuoteHed_Row["Freight_c"]);
using (var quoteSvc = Ice.Assemblies.ServiceRenderer.GetService<Erp.Contracts.QuoteSvcContract>(this.Db))    
{      
  // This code adds / updates a header Misc Freight Charge            
  Erp.Tables.QuoteMsc OrderMsc;
  int quoteLine = 0;
  int qtyNum = 0;
  string sMiscCode = "CAHO";
  string sMiscDesc = "Carriage-Home";
  string sMiscFreq = "E";
  decimal dTotAmt = freightCharge;
  var soTs = new Erp.Tablesets.QuoteTableset();
  // Check if the Freight Record exists already
  var QuoteMsc_Row =  Db.QuoteMsc.With(LockHint.UpdLock).Where(row => row.QuoteNum == quoteNum && row.MiscCode == sMiscCode && (string.Compare(row.Company, Session.CompanyID, true)) == 0).FirstOrDefault();
  if ((QuoteMsc_Row == null))
  {
    quoteSvc.GetNewQuoteHedMsc(ref soTs, quoteNum, quoteLine, qtyNum);
    var orderMscNew = soTs.QuoteHedMsc.FirstOrDefault();
    orderMscNew.MiscCode = sMiscCode;
    orderMscNew.Description = sMiscDesc;
    orderMscNew.FreqCode = sMiscFreq;
    quoteSvc.GetMiscChrgDefaults(ref soTs, "QuoteHedMsc");
    orderMscNew = soTs.QuoteHedMsc.FirstOrDefault();
    orderMscNew.MiscAmt = dTotAmt;
    orderMscNew.DocMiscAmt  = dTotAmt;
    quoteSvc.Update(ref soTs);
  } 
  else 
  { 
    if(dTotAmt > Decimal.Zero)
    {
      QuoteMsc_Row.MiscAmt = dTotAmt;
      QuoteMsc_Row.DocMiscAmt  = dTotAmt;
      soTs = quoteSvc.GetByID(quoteNum);
    }
    else
    {      
      Db.QuoteMsc.Delete(QuoteMsc_Row);
      Db.Validate();
    }
  }
  this.dsHolder.Attach(soTs);
}

Hey I am also trying to do this with intermittent success. I have it adding the charge when I want it to. But it seems to delete the charge when I complete the tasks associated with processing the quote. Any idea what would make the charge disappear?

Let me clarify. All I have to do is hit Refresh on the quote and it loses the Misc. Charge that was just added. I must be missing something silly. Why isn’t it sticking? I am adding to QuoteHedMsc for what its worth.