BPM throwing an exception

BPM runtime caught an unexpected exception of ‘NullReferenceException’ type.

We’ve defined a BPM to send mail to the approver when the buyer enters the PO more than the amount of his limit.

the BPM looks like this.

and the custom code is as below

Erp.Tables.POHeader POHeader;
Erp.Tables.PurAgent PurAgent;
Erp.Tables.Vendor Vendor;
string approval = string.Empty;
string strLineDesc = string.Empty;
string strVendorName = string.Empty;
var POH = (from POHeader1 in ttPOHeader where POHeader1.Company == Session.CompanyID orderby POHeader1.PONum descending select POHeader1).FirstOrDefault();
if (POH != null)
{
     POHeader = (from POHeaderRow in Db.POHeader
                  where POHeaderRow.Company == Session.CompanyID
                     && POHeaderRow.PONum == POH.PONum
                  select POHeaderRow).FirstOrDefault();
    if (POHeader != null)
    {
        /*Get Line Description BEGIN*/
        foreach (var ttPODetailResults in (from ttPODetailRow in Db.PODetail
                                           where ttPODetailRow.Company == Session.CompanyID
                                              && ttPODetailRow.PONUM == POHeader.PONum
                                           select ttPODetailRow))
        {
            if (String.IsNullOrEmpty(strLineDesc))
            {
                strLineDesc = Convert.ToString(ttPODetailResults.POLine) + ". " + ttPODetailResults.LineDesc + ", Qty: " + String.Format("{0:0}",ttPODetailResults.OrderQty) + ", Unit Price: " + String.Format("{0:0.00}",ttPODetailResults.UnitCost) + ", Cost: " + String.Format("{0:0.00}",ttPODetailResults.OrderQty * ttPODetailResults.UnitCost);
            }
            else
            {
                strLineDesc = strLineDesc + "\n" + Convert.ToString(ttPODetailResults.POLine) + ". " + ttPODetailResults.LineDesc + ", Qty: " + String.Format("{0:0}", ttPODetailResults.OrderQty) + ", Unit Price: " + String.Format("{0:0.00}", ttPODetailResults.UnitCost) + ", Cost: " + String.Format("{0:0.00}", ttPODetailResults.OrderQty * ttPODetailResults.UnitCost);
            }
        }
        /*Get Line Description END*/
    
        /*Get Supplier Name BEGIN*/
        Vendor = (from VendorRow in Db.Vendor
                  where VendorRow.Company == Session.CompanyID
                     && VendorRow.VendorNum == POHeader.VendorNum
                  select VendorRow).FirstOrDefault();
        if (Vendor != null)
        {
            strVendorName = Vendor.Name;
        }
        /*Get Supplier Name END*/
    
             
        approval = POHeader.ApprovalStatus;
        var ReqPurAgent = (from PurAgentRow in Db.PurAgent
                    where PurAgentRow.Company == Session.CompanyID
                       && PurAgentRow.BuyerID == POHeader.BuyerID
                    select PurAgentRow).FirstOrDefault();
        var currentPurAgent = (from PurAgentRow in Db.PurAgent
        where PurAgentRow.Company == Session.CompanyID
           && PurAgentRow.BuyerID == approval
        select PurAgentRow).FirstOrDefault();
        if (ReqPurAgent != null)
        {
            if (ReqPurAgent.EMailAddress != null)
            {
                if (POH != null)
                {
                    if (strLineDesc != null)
                    {
                        var subject = string.Format("{0} Purchase Order : {1} Ordered By: {2} is {3} ",Session.CompanyID, POHeader.PONum, ReqPurAgent.Name, POHeader.ApprovalStatus);
                        var mailer = GetMailer(async: true);
                        var message = new Ice.Mail.SmtpMail();
                        //message.SetFrom("rishir@jaivel.com");
                        message.SetFrom(currentPurAgent.EMailAddress);
                        message.SetTo(ReqPurAgent.EMailAddress);
                        message.SetSubject(subject);
                        message.SetBody(subject + "\nSupplier: " + strVendorName + "\n\n" + strLineDesc);
                        mailer.Send(message);                                
                    } /*End strLineDesc*/
                } /*End if POApvMsg*/
            } /*End PurAgent.EmailAddress*/
        } /*End PurAgent*/
    } /*End foreach*/
 }

the error i got is as below:

Can anyone help us to solve the problem?

Thanks in advance
Rishi

There is no null check on the currentPurAgent variable. Could this be null?
You’re comparing the BuyerID with ‘approval’. ‘approval’ is the ApprovalStatus on the PoHeader. This does not seem right.

var currentPurAgent = (from PurAgentRow in Db.PurAgent
        where PurAgentRow.Company == Session.CompanyID
           && PurAgentRow.BuyerID == approval
        select PurAgentRow).FirstOrDefault();

This does solved that null exception problem, but it is not sending out the email to the aprover.

Can you help me out with that too?
Here’s the updated code.

Erp.Tables.POHeader POHeader;
Erp.Tables.PurAgent PurAgent;
Erp.Tables.Vendor Vendor;
string approval = string.Empty;
string strLineDesc = string.Empty;
string strVendorName = string.Empty;
var POH = (from POHeader1 in ttPOHeader where POHeader1.Company == Session.CompanyID orderby POHeader1.PONum ascending select POHeader1).FirstOrDefault();
if (POH != null)
{
POHeader = (from POHeaderRow in Db.POHeader where POHeaderRow.Company == Session.CompanyID && POHeaderRow.PONum == POH.PONum select POHeaderRow).FirstOrDefault();
if (POHeader != null)
{
/Get Line Description BEGIN/
foreach (var ttPODetailResults in (from ttPODetailRow in Db.PODetail where ttPODetailRow.Company == Session.CompanyID && ttPODetailRow.PONUM == POHeader.PONum select ttPODetailRow))
{
if (String.IsNullOrEmpty(strLineDesc))
{
strLineDesc = Convert.ToString(ttPODetailResults.POLine) + “. " + ttPODetailResults.LineDesc + “, Qty: " + String.Format(”{0:0}”,ttPODetailResults.OrderQty) + “, Unit Price: " + String.Format(”{0:0.00}",ttPODetailResults.UnitCost) + “, Cost: " + String.Format(”{0:0.00}",ttPODetailResults.OrderQty * ttPODetailResults.UnitCost);
}
else
{
strLineDesc = strLineDesc + “\n” + Convert.ToString(ttPODetailResults.POLine) + “. " + ttPODetailResults.LineDesc + “, Qty: " + String.Format(”{0:0}”, ttPODetailResults.OrderQty) + “, Unit Price: " + String.Format(”{0:0.00}", ttPODetailResults.UnitCost) + “, Cost: " + String.Format(”{0:0.00}", ttPODetailResults.OrderQty * ttPODetailResults.UnitCost);
}
}
/Get Line Description END/

    /*Get Supplier Name BEGIN*/
    Vendor = (from VendorRow in Db.Vendor
              where VendorRow.Company == Session.CompanyID
                 && VendorRow.VendorNum == POHeader.VendorNum
              select VendorRow).FirstOrDefault();
    if (Vendor != null)
    {
        strVendorName = Vendor.Name;
    }
    /*Get Supplier Name END*/  approval = POHeader.ApprovalStatus;
    var ReqPurAgent = (from PurAgentRow in Db.PurAgent
                where PurAgentRow.Company == Session.CompanyID
                   && PurAgentRow.BuyerID == POHeader.BuyerID
                select PurAgentRow).FirstOrDefault();
    var currentPurAgent = (from PurAgentRow in Db.PurAgent
    where PurAgentRow.Company == Session.CompanyID
       && PurAgentRow.BuyerID == POHeader.BuyerID
    select PurAgentRow).FirstOrDefault();
    if (ReqPurAgent != null)
    {
        if (ReqPurAgent.EMailAddress != null)
        {
            if (POH != null)
            {
                if (strLineDesc != null)
                {
                    var subject = string.Format("{0} Purchase Order : {1} Ordered By: {2} is {3} ",Session.CompanyID, POHeader.PONum, ReqPurAgent.Name, POHeader.ApprovalStatus);
                    var mailer = GetMailer(async: true);
                    var message = new Ice.Mail.SmtpMail();
                    //message.SetFrom("rishir@jaivel.com");
                    message.SetFrom(currentPurAgent.EMailAddress);
                    message.SetTo(ReqPurAgent.EMailAddress);
                    message.SetSubject(subject);
                    message.SetBody(subject + "\nSupplier: " + strVendorName + "\n\n" + strLineDesc);
                    mailer.Send(message);                                
                } /*End strLineDesc*/
            } /*End if POApvMsg*/
        } /*End PurAgent.EmailAddress*/
    } /*End PurAgent*/
} /*End foreach*/

}

There’s a poorly commented comment here:

Assuming that is not the issue, slap a Messagebox in there to debug and make sure your getting expected values for each of the variables that you use to build the message. Read out the to/from addresses, body, and subject, as well as the variables used to build the body. Like vendor name, line description.
Good Luck!

Can I assume you are using the PO Approve form?
If so, I don’t think the Method Erp.PO.Update will catch changes to the field POHeader.ApproveStatus? Might try the method Erp.POAprMsg.Update or… a Data Directive instead?

When it’s not obvious from a trace, I often add BPM(s) but without any conditions… just messageboxes with fields I’m interested in (and the RowMod).

1 Like

I’m using the POHeader form.
It’s from data directive, so PO. Update will not be there.

Using that message box is good idea, but again i’m using data directive.

But i’ll try using the method directive too.