We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

insertOrUpdate()

Not applicable

Hello fellow devs!

Any guidelines how to narrow down and filter the insertOrUpdate() ?

My point is - if I query the incident table , with a filter , only the first filter(field) is taken into account. Example:

 

var user = new GlideQuery('incident').insertOrUpdate({'short_description':'blah', state : 1, 'description': '222'}).orElse(null);

 

So, the second field is not taken into account as a filer for the query. Any clue how can I do that ?

 

Thanks!

Joro

1 ACCEPTED SOLUTION

-O-
Kilo Patron

Oh and to answer your question, you need to resort to a "classic" update, if you want to filter by something else then sys_id.

View solution in original post

4 REPLIES 4

Tony Chatfield1
Kilo Patron

Hi, perhaps a syntax issue as state is not encapsulated in 'quotes' so would not be seen as a field name.

Can you explain your business requirement\use case?

I would be extremely reluctant to deploy any insert\update functionality like this in any table based on 'text' fields.

 

The parameter is a JavaScript object. Whether the properties are written with or without single quotes or double quotes has absolutely no consequence. It only matters in a JSON string (that keys and string values are enclosed specifically in double-quotes).

-O-
Kilo Patron

Actually that is not how it works. If any of the changes is for a primary key field (basically sys_id) than it will do an update. If not sys_id is present, it will do an insert. This is what the comments for the method claim:

* Updates an existing record (just like update), however instead of requiring
* where calls, it uses the primary key(s) in the recordValues object passed
* in. If the primary key(s) isn't there, insertOrUpdate will insert a new
* record instead.

Internally what it does is to generate a "pure" insert or update GlideQuery plan and execute it. If sys_id is in the recordValues object, it generates an update plan (turns the sys_id recordValue into a where clause) and executes that, otherwise it generates an insert plan and executes that.

-O-
Kilo Patron

Oh and to answer your question, you need to resort to a "classic" update, if you want to filter by something else then sys_id.