ckrusen
(Calvin Krusen)
September 18, 2020, 1:50pm
1
The following topic about pulling data from a DB table was most excellent.
https://www.epiusers.help/t/simple-one-liner-c-to-retrieve-data-from-a-table/44627
How would one go about SETTING data in a table?
And before you jump down my throat about bypassing the business logic and direct manipulation, I’d only use this on UD fields. And yes I understand there may be some UD fields that inherit business logic (like an Orderhed UD field becoming R/O then the order is closed), but that’s a risk I’ll keep in mind.
josecgomez
(Jose C Gomez)
September 18, 2020, 1:57pm
2
Can’t really do it in one line unless you like really ugly code… but
Db.QuoteHed.Where( r =>r.Company == MyCompany && r.QuoteNum == QuoteToFind).FirstOrDefault()?.CustomField_c="larry";
Db.Validate();
2 Likes
ckrusen
(Calvin Krusen)
September 18, 2020, 2:01pm
3
A one-ish-liner is just as good!
1 Like
ckrusen
(Calvin Krusen)
September 18, 2020, 2:06pm
4
I assumed there’s no protection form doing something stupid like using that to edit a system table.
Would territory security be maintained? I assume that happens at a lower level, like how it works with BAQs. Or is this LINQ code so “low level” that it would bypass that territory security too?
josecgomez
(Jose C Gomez)
September 18, 2020, 2:07pm
5
Nope territory security goes out the window when you drop down into EF. (for the most part)
1 Like
ckrusen
(Calvin Krusen)
October 22, 2020, 2:05pm
6
@josecgomez - Is that question mark supposed to be there?
Finally got around to trying this, and I get an error message stating:
The left-hand side of an assignment must be a variable, property, or indexer
If I remove the '?'
it passes the syntax check.
Doug.C
(Doug Crabtree)
October 22, 2020, 2:11pm
7
If FirstOrDefault() returns null then you will be trying to write data to a null object which will result in an error.
If it will never be null, then remove the '?'
ckrusen
(Calvin Krusen)
October 22, 2020, 2:29pm
8
But the BPM Custom Code won’t pass syntax check with the '?'
in there.
Should I test if the record exists before setting it? Something like?
row = Db.UD05.Where( ...).FirstOrDefault();
if(row != null){
Db.UD05.Where( ...).FirstOrDefault().Character01 = "Hello World!";
Db.Validate();
}
If the record didn’t exist, and the line:
Db.UD05.Where( ...).FirstOrDefault().Character01 = "Hello World!";
Db.Validate();
were executed, what would happen? Nothing? Or would the record be created?
Doug.C
(Doug Crabtree)
October 22, 2020, 2:36pm
9
I’m not the most efficient coder, but since you’re trying to assign a value to the result, then I would check it first to see if it’s null.
If I was reading from it, then I would use the '?.'
null conditional operator.
josecgomez
(Jose C Gomez)
October 22, 2020, 3:09pm
11
Yes it is supposed to be there though maybe your version of Epicor is a little outdated you can replace that with an if statement that checks if its null then assigns it.
C# operators that you use to access type members or null-conditionally access type members. These operators include the dot operator - `.`, indexers - `[`, `]`, `^` and `..`, and invocation - `(`, `)`.
1 Like
jkane
(John Kane)
October 22, 2020, 5:15pm
12
string oneLiner = "I ate a clock yesterday, it was very time-consuming."
1 Like
jkane
(John Kane)
October 22, 2020, 5:16pm
13
Sorry. Couldn’t stop myself.
1 Like
ckrusen
(Calvin Krusen)
October 22, 2020, 5:31pm
14
“I see” said the blind man. as he picked up his hammer and saw.
But he still went back for seconds.
2 Likes