Behavior of updateMultiple with reference fields in the query
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2016 03:55 PM
Hi All,
I am using the updateMultiple() function to perform a mass update of records in a table and am having a bit of trouble. I am hoping someone with more in-depth knowledge of how this function works can help me. The code is as follows:
01 function updateCIs() {
02 var gr = new GlideRecord(MY_TABLE);
03 gr.addEncodedQuery('u_company.nameLIKEAcme^u_descriptionLIKEfirewall')
04 gr.addQuery('u_state', 'pending');
05 gr.query();
06
07 gs.debug("Number of records to be updated: " + gr.getRowCount()); //This returns the correct number of records to be updated
08 gr.u_ci = '58d097aa6f7d120073ccf13f5d3ee499';
09 gr.updateMultiple();
10 }
When I execute this, the records are not updated even though the query is functioning correctly. The log statement in line 7 correctly gives me the number of records to be updated and no error messages are thrown in the log (that I can see).
After some experimentation I found that the issue lies when I am incorporating reference fields in my encoded query. If I replace line 3 with the following code the records are updated successfully:
03'' gr.addEncodedQuery('u_company=7b5f84456f71d20073ccf13f5d3ee4c7^u_descriptionLIKEfirewall')
I initially thought updateMultiple might not work on join queries and that the 03 query is performing a join, while 03'' might not be since it's just querying the sys_id directly. However I tried testing this manually, by navigate to the table through the UI, applying the 03 query, and then shift-selecting the CI field to perform a mass update. This worked.
It just appears I can't seem to update records via my script when trying to dot-walk to reference fields in the encoded query. My question is:
1. Why does one work and not the other?
2. And is there a way for me to re-formulate my encoded query so this does work?
Thanks,
Sid Sonrexa
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2016 09:03 PM
In regards your question, even if you dot walk within your encoded query you should be able to perform an updateMultiple() without issues. I just tried it on my developer (sandbox) Geneva instance and it worked fine.
Below is the code for reference. I hope this is helpful:
var gr = new GlideRecord ('incident');
gr.addEncodedQuery('assignment_group.name=Service Desk');
gr.query();
gr.short_description = "Update by updateMultiple";
gr.updateMultiple();
gs.print(gr.getRowCount());
Thanks,
Berny
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2016 09:04 PM
The output of the above query was that all incident records assigned to the ServiceDesk group had their short description updated to Update by updateMultiple.
Thanks,
Berny
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2016 09:55 PM
u_company is a custom table and thus if its not extending other tables then custom field names usually starts with u_name. I am just asking to verify the backend name of 'name' field. You may need to use u_company.u_name instead of u_compay.name