Getting a BPM caught an exception error

Hello. I was wondering if anybody can help me or point me the right direction. I have a PreProcess BPM on the Sales Order Update method. There is no error when checking the syntax but I get an error when adding a new line. It does not error out when updating a line though. Please note that I am not a C# expert so please bear with me.

here is the error:
Server Side Exception

BPM runtime caught an unexpected exception of ‘TargetException’ type.
See more info in the Inner Exception section of Exception Details.

Exception caught in: Epicor.ServiceModel

Error Detail

Description: BPM runtime caught an unexpected exception of ‘TargetException’ type.
See more info in the Inner Exception section of Exception Details.
Program: CommonLanguageRuntimeLibrary
Method: CheckConsistency
Original Exception Type: TargetException
Framework Method: A001_CustomCodeAction
Framework Line Number: 0
Framework Column Number: 0
Framework Source: A001_CustomCodeAction at offset 2886 in file:line:column :0:0

Server Trace Stack: at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
at System.Data.Objects.ELinq.QueryParameterExpression.TryGetFieldOrPropertyValue(MemberExpression me, Object instance, Object& memberValue)
at System.Data.Objects.ELinq.QueryParameterExpression.TryEvaluatePath(Expression expression, ConstantExpression& constantExpression)
at System.Data.Objects.ELinq.QueryParameterExpression.EvaluateParameter(Object[] arguments)
at System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable1 forMergeOption) at System.Data.Objects.ObjectQuery1.GetResults(Nullable1 forMergeOption) at System.Data.Objects.ObjectQuery1.System.Collections.Generic.IEnumerable.GetEnumerator()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable1 source) at Epicor.Customization.Bpm.BO5B00D8B998964100AE61CD470885B103.UpdatePreProcessingDirective_BelowMargin_D94BA72837444D6F9C4C8AF811DE51E2.A001_CustomCodeAction() at Epicor.Customization.Bpm.BO5B00D8B998964100AE61CD470885B103.UpdatePreProcessingDirective_BelowMargin_D94BA72837444D6F9C4C8AF811DE51E2.ExecuteCore() at Epicor.Customization.Bpm.DirectiveBase3.Execute(TParam parameters) in c:_Releases\ICE\3.1.600.0\Source\Server\Internal\Lib\Epicor.Customization.Bpm\DirectiveBase.Generic.cs:line 129

Client Stack Trace

at Epicor.ServiceModel.Channels.ImplBase`1.ShouldRethrowNonRetryableException(Exception ex, DataSet[] dataSets)
at Erp.Proxy.BO.SalesOrderImpl.MasterUpdate(Boolean lCheckForOrderChangedMsg, Boolean lcheckForResponse, String cTableName, Int32 iCustNum, Int32 iOrderNum, Boolean lweLicensed, Boolean& lContinue, String& cResponseMsg, String& cCreditShipAction, String& cDisplayMsg, String& cCompliantMsg, String& cResponseMsgOrdRel, SalesOrderDataSet ds)
at Erp.Adapters.SalesOrderAdapter.MasterUpdate(Boolean lCheckForOrderChangedMsg, Boolean lcheckForResponse, String cTableName, Int32 iCustNum, Int32 iOrderNum, Boolean lweLicensed, Boolean& lContinue, String& cResponseMsg, String& cCreditShipAction, String& cDisplayMsg, String& cCompliantMsg, String& cResponseMsgOrdRel)
at Erp.UI.App.SalesOrderEntry.Transaction.Update()

Inner Exception

Non-static method requires a target.

and here is the code:

Erp.Tables.PartCost PartCost;
Erp.Tables.OrderHed OrderHed;

decimal SC = 0; //Standard Cost
decimal TOC = 0; //Total Order Cost
decimal NM = 0;
decimal V1 = 0;
decimal V2 = 0;
bool bFailedGM = false;
bool bOrderFailed = false;
bool BelowMargin = false;
bool BelowMarginApproved = false;

foreach (var ttOrderDtl_iterator in (from ttOrderDtl_Row in ttOrderDtl
where (string.Equals(ttOrderDtl_Row.RowMod, IceRow.ROWSTATE_ADDED, StringComparison.OrdinalIgnoreCase) || string.Equals(ttOrderDtl_Row.RowMod, IceRow.ROWSTATE_UPDATED, StringComparison.OrdinalIgnoreCase))
select ttOrderDtl_Row))

{   var ttOrderDtlRow = ttOrderDtl_iterator;

        OrderHed = (from OrderHed_Row in Db.OrderHed
                where string.Compare(OrderHed_Row.Company, ttOrderDtlRow.Company, true) == 0
                && OrderHed_Row.OrderNum == ttOrderDtlRow.OrderNum
                select OrderHed_Row).FirstOrDefault();
                
        PartCost = (from PartCost_Row in Db.PartCost
                where string.Compare(PartCost_Row.Company, ttOrderDtlRow.Company, true) == 0
                && PartCost_Row.PartNum == ttOrderDtlRow.PartNum
                && ((PartCost_Row.StdBurdenCost 
                   + PartCost_Row.StdLaborCost 
                   + PartCost_Row.StdMaterialCost 
                   + PartCost_Row.StdMtlBurCost 
                   + PartCost_Row.StdSubContCost) > (ttOrderDtlRow.TotalPrice /ttOrderDtlRow.SellingQuantity)) 
                select PartCost_Row).FirstOrDefault();   
        
        SC = (PartCost.StdBurdenCost + PartCost.StdLaborCost + PartCost.StdMaterialCost + PartCost.StdMtlBurCost + PartCost.StdSubContCost);
        NM = (SC - (ttOrderDtlRow.TotalPrice /ttOrderDtlRow.SellingQuantity));  
        TOC = SC * ttOrderDtlRow.SellingQuantity; 

   if (PartCost != null)

    { 
             /* InfoMessage.Publish("TOC = " + TOC , Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual);
                InfoMessage.Publish("OrdBasedPrice = " + ttOrderDtlRow.OrdBasedPrice , Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual);
                InfoMessage.Publish("OrdBasedPrice/Qty = " + (ttOrderDtlRow.OrdBasedPrice / ttOrderDtlRow.SellingQuantity) , Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual);
                InfoMessage.Publish("NM = " + NM , Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual);
                InfoMessage.Publish("V2/V1 = " + V2 + "/ " + V1, Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual);
                InfoMessage.Publish("Total = " + ttOrderDtlRow.TotalPrice , Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual);
                InfoMessage.Publish("SC-OBP = " + (SC - TOC) , Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual);
             
                bFailedGM=false;
                bBelowMarginApproved=false; 
                InfoMessage.Publish("BMApproved: " + (Convert.ToBoolean(ttOrderDtlRow["BMApproved_c"])), Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual);
                                 InfoMessage.Publish("BelowMargin: " + (Convert.ToBoolean(ttOrderDtlRow["BelowMargin_c"]) + " OrderHold: " + OrderHed.OrderHeld), Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual);
                InfoMessage.Publish("BelowMargin: " + (Convert.ToBoolean(ttOrderDtlRow["BelowMargin_c"]) + " OrderHold: " + OrderHed.OrderHeld), Ice.Common.BusinessObjectMessageType.Information,         Ice.Bpm.InfoMessageDisplayMode.Individual); 
                InfoMessage.Publish("BelowMargin: " + (Convert.ToBoolean(ttOrderDtlRow["BelowMarginApproved_c"]) + "OrderHold: " + OrderHed.OrderHeld), Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual);
              */         
      if ((NM   > 100) && (Convert.ToBoolean(ttOrderDtlRow["BMApproved_c"]) == false))
        
        {   
            if (ttOrderDtlRow.RMANum < 1)
                {
                ttOrderDtlRow["BelowMargin_c"] = true; 
                InfoMessage.Publish("Warning:  Order has been put on hold.  Line: " + ttOrderDtlRow.OrderLine + " is below margin.  Please have your manager approve the line to release it from Order Hold.",  Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual); 
                OrderHed.OrderHeld = true; 
                          /*  MailMessage mail = new MailMessage("NoReply.E10@dovertwg.com", "juy@dovertwg.com, cstclair@dovertwg.com");
                            SmtpClient client = new SmtpClient();
                            client.Port = 25;
                            client.DeliveryMethod = SmtpDeliveryMethod.Network;
                            client.UseDefaultCredentials = false;
                            client.Host = "smtp.dovercorporation.com";
                            mail.Subject = "Negative margin on Sales Order# " + ttOrderDtlRow.OrderNum ;
                            mail.Body = "Company: " + ttOrderDtlRow.Company + Environment.NewLine + "Sales order #: " + ttOrderDtlRow.OrderNum + Environment.NewLine + "Order Line #: "  + ttOrderDtlRow.OrderLine + Environment.NewLine + "Part #: " + ttOrderDtlRow.PartNum + Environment.NewLine + "has a negative margin of more than $100." + Environment.NewLine + Environment.NewLine + "Thank you." + Environment.NewLine + Environment.NewLine + "---------------------------------------------------------------------------------------" + Environment.NewLine + "*****PLEASE DO NOT REPLY TO THIS EMAIL. IT DOES NOT GET REVIEWED*****" + Environment.NewLine + "BPM.Update.SalesOrder.BelowMargin" ;
                            client.Send(mail);  */  
                }
              else
                    { 
                    ttOrderDtlRow["BelowMargin_c"] = true;
                    ttOrderDtlRow["BMApproved_c"] = true;
                    ttOrderDtlRow["BMReason_c"] = "Service/RMA";
                    OrderHed.OrderHeld = false;
                    }
        }
            else  
                {
                OrderHed.OrderHeld = false;
                ttOrderDtlRow["BelowMargin_c"] = true;
                }
    }
              
        else
    {
        OrderHed.OrderHeld = false;
      //  InfoMessage.Publish("Not Below Cost", Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual);
        ttOrderDtlRow["BelowMargin_c"] = false;
        ttOrderDtlRow["BMApproved_c"] = false;
        ttOrderDtlRow["BMReason_c"] = "";
    }

}

Please help. Thank you.

My guess is that you have some nulls you arent checking. You are doing a query on DB for added rows…but they wont exist. Then you try to access null vars.

Wrap you accessors in null checks:

if(OrderHed != null)
{
...
}

if(PartCost != null)
{
SC = (PartCost.StdBurdenCost + PartCost.StdLaborCost + PartCost.StdMaterialCost + PartCost.StdMtlBurCost + PartCost.StdSubContCost);
        NM = (SC - (ttOrderDtlRow.TotalPrice /ttOrderDtlRow.SellingQuantity));  
        TOC = SC * ttOrderDtlRow.SellingQuantity; 

}

1 Like