Looking up Company.CountryNum in BPM custom code

Since I couldn’t figure out how to set a variable from a query using any of the standard widgets, I tried to do it using the custom code widget.

{
    var companyCountryNum = Db.Company.Where(r => r.Company1 == Session.CompanyID).Select(r => r.CountryNum).FirstOrDefault();
}

I get a syntax error if I use Company.Company in my Where clause, but not if I use Company.Company1. I am getting Company.CountryNum = 0 as an output, even though it should be 8 (USA). Company1 seems strange, since it’s not actually the field name in the table as far as I can see…but I’ve seen it referenced like that before.

Could you describe more on what are you trying to achieve? What syntax error you get ?

I am trying to look up Company.CountryNum so I can compare it to the Customer.CountryNum and update a flag based on the result.

This is the syntax error when I try using Company.Company:
image

I believe Company field is renamed as Company1. Not sure why. But I was able to get this working with a condition widget first
image
and then custom code widget with this code:


I don’t have any boolean field to test but my messages fired correctly. Give it a try.

It’s not working for me, as it is not passing the first IF statement. When I add

var vCompCountry = CompanyCountryNum.CountryNum;

right before the first IF and display the result in a message, I get 0.

It is hard to tell. Are you using variable vCompCountry created in the BPM or in the custom code?. Am I over-seeing anything? I believe we cannot user variable directly.

I am not sure what the end goal is here but anyway. I noticed you used code so here you are (BPM on Customer.Update.Pre):

// get added or updated customer
var cst = (from cstrows in ttCustomer where (cstrows.RowMod=="U" || cstrows.RowMod=="A") select cstrows).FirstOrDefault();
if (cst!=null)
{
  // get company
  var cmy = (from cmyrows in Db.Company where (cmyrows.Company1==cst.Company) select cmyrows).FirstOrDefault();
  if (cmy.CountryNum!=cst.CountryNum)
  {
    // your logic here...
    
  }
}

Another thing - when you’re typing the code in Epicor’s code editor, you can use CTRL+SPACE for autocomplete. Handy if you don’t know the exact name of the fields.

This worked! I think using the Company value from the ttCustomer table was the trick. Thank you @Dragos and @itsme for all of your help.

The end goal is to set the International Shipment flag on the Customer and Customer Ship To, so that it can be checked against in APR to print the appropriate documentation (e.g. commercial invoice).

1 Like