Simple one-liner (c#) to SET data in a table

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.

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

A one-ish-liner is just as good!

1 Like

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?

Nope territory security goes out the window when you drop down into EF. (for the most part)

1 Like

@josecgomez - Is that question mark supposed to be there?

image

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.

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 '?'

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?

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.

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.

1 Like
string oneLiner = "I ate a clock yesterday, it was very time-consuming."
1 Like

Sorry. Couldn’t stop myself.

1 Like

“I see” said the blind man. as he picked up his hammer and saw.

But he still went back for seconds.

2 Likes