Help with syntax in BPM Custom Code

,

Hello. We have a Standard Data Directive on OrderHed that uses Custom Code to access the Customer table for the simple purposes of sending an email to the salesperson.

It is seemingly very simple code, but I don’t know enough about the syntax to modify it in the way I need. Here is the code:

var MyCustomerRec = (from row in ttOrderHed
join MyCustomerRecRow in Db.Customer
on row.CustNum equals MyCustomerRecRow.CustNum
select MyCustomerRecRow).FirstOrDefault();

var MyUserRec = (from row in ttOrderHed
join MyUserRecRow in Db.UserFile
on row.EntryPerson equals MyUserRecRow.DcdUserID
select MyUserRecRow).FirstOrDefault();

var MyTermsRec = (from row in ttOrderHed
join MyTermsRecRow in Db.Terms
on row.TermsCode equals MyTermsRecRow.TermsCode
select MyTermsRecRow).FirstOrDefault();

if(MyCustomerRec != null)
{
callContextBpmData.Character01 = MyCustomerRec.Name;
callContextBpmData.Character02 = MyUserRec.EMailAddress;
callContextBpmData.Character04 = MyUserRec.Name;
callContextBpmData.Character03 = MyTermsRec.Description;
}

What I need to add is a way to specify the Company of the Customer record. We are multi-company so the same CustNum can exist in both Companies. This sometimes results in the wrong Customer Name being pulled. I’ve tried:

var MyCustomerRec = (from row in ttOrderHed
join MyCustomerRecRow in Db.Customer
on row.CustNum equals MyCustomerRecRow.CustNum
and row.Company equals MyCustomerRecRow.Company
select MyCustomerRecRow).FirstOrDefault();

which threw a lot of errors, and

var MyCustomerRec = (from row in ttOrderHed
join MyCustomerRecRow in Db.Customer
on row.CustNum equals MyCustomerRecRow.CustNum
where MyCustomerRecRow.Company = '[COMPANY]'
select MyCustomerRecRow).FirstOrDefault();

which only threw the error “Too many characters in character literal” so I’m hoping that one is closer to correct?

Any guidance on how to proceed would be much appreciated. Thank you in advance.

Double Quotes are needed here.
where My CustomerRecRow.Company = "Company"

Thank you for the reply. When I did that, I got the following error:

“Range variable ‘MyCustomerRecRow’ cannot be assigned to – it is read only”

Thoughts?

So, you can only have one comparison in linq query syntax. (which is why I usually use lambda). So in order to do that, you will need to create objects to compare to.

var MyCustomerRec = (from row in ttOrderHed
join MyCustomerRecRow in Db.Customer
on 
new {row.CustNum , row.Company}
equals
new {MyCustomerRecRow.CustNum, MyCustomerRecRow.Company}
select MyCustomerRecRow).FirstOrDefault();

You could do what you were trying to do, which is return all of the rows, then in the where clause remove anything where the company doesn’t match, but that’s generally like bringing every kind of milk to the register and then deciding which one to buy in front of the cashier. lol.

var MyCustomerRec = (from row in ttOrderHed
join MyCustomerRecRow in Db.Customer
on row.CustNum equals MyCustomerRecRow.CustNum
where MyCustomerRecRow.Company == row.Company
select MyCustomerRecRow).FirstOrDefault();
1 Like

Creating the objects worked perfectly, of course! Thank you so much. I’ll even shop for milk differently now! Haha.

1 Like