BPM Pop up message, how to deal with null variable?

,

I would like help getting this pop up message working. It gives a ‘NullReferenceException’ error when the part does not have a memo record. Shouldn’t it return and empty string if there is no memo record?
Should I make the pop up message using a custom code block instead of trying to use the widgets?

Db.Memo.FirstOrDefault(m => m.Company == callContextClient.CurrentCompany 
&& m.Key1 == ttRcvDtlRow.PartNum 
&& m.CategoryID == "RCVPOPUP" 
&& m.RelatedToFile == "PART").MemoText ?? string.Empty


image

Hi @YGT,
may i ask why using [set argument/Variable] widget for this code block ? if i were you i would use Custom Code so i can validate against null value and then cast what ever value i got to your argument or to any CallContext variable

Sometimes if you want to reuse the data you are retreiving (generally in a condition, logic and/or messages or any combo of those) it’s nice to only get the data once, store it then use it in those different locations. This is an approach I use quite often. That being said I have had a heck of a time trying to get the linq null stuff to work with Epicor in any of it’s various normally C# accepted ways. I’m not sure why that is but when I need to handle these generally the one way that seems to work 100% of the time is, unfortunately, storing the query to a variable and checking for null. I think it has to do with DB being a database context object vs something a little more static where the null stuff usually works.

var query = Db.Memo.Where(your stuff).FirstOrDefault();
if(query != null) { 
   blah
}

Not ideal but seems foul proof.

1 Like

thanks @jgiese.wci, so what stopping us from retrieve the data as per this approach ( i use it as well) then cast it to variable array rather than add it on the C# of the argument variable which is not desined to deal with multiple line of codes

I still get the error message ‘NullReferenceException’ when I try this custom code. Did I write it correctly? I am new to c#.

var rcvRow = ttRcvDtl.FirstOrDefault();
var Message = Db.Memo.Where(m => m.Company == callContextClient.CurrentCompany
&& m.Key1 == rcvRow.PartNum
&& m.CategoryID == "RCVPOPUP" 
&& m.RelatedToFile == "PART").FirstOrDefault().MemoText;

if(Message != null )
  {
  this.PublishInfoMessage(Message,
                          Ice.Common.BusinessObjectMessageType.Information,             Ice.Bpm.InfoMessageDisplayMode.Individual, "", "");
  };

instead of tacking MemoText on the end try moving it to a select

.Select(x => x.MemoText).FirstOrDefault();
1 Like

That worked. Thank you for your help!

1 Like