- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2018 04:23 PM
Hi all,
I've scripted to reference fields in various Business Rules and Client Scripts (using sys_ID's) and am now trying to do this in a Background Script, but have been unsuccessful so far - though I've done this with Choice and String fields in the past with no issues. I'm only having trouble putting values in a Reference field.
All I'm trying to do with this script is to have it query for Cases where the Company is one company (this part is successful), then populate these fields with another company.
Here's the script I'm using:
var cs = new GlideRecord('sn_customerservice_case');
cs.addQuery('company.name','=','Company 1');
cs.query();
gs.print('cs Query: ' + cs.getEncodedQuery() + ' = ' + cs.getRowCount());
while(cs.next())
{
cs.setWorkflow(false);
cs.autoSysField(false);
gs.addInfoMessage(cs.number);
cs.company.name = 'Company 2';
cs.update();
}
Any thoughts about what I'm doing wrong? I get the right results from the query, just can't put the data in the field…
Line 17 is the line I am working on...these are the things I've tried to far:
- cs.company.name = 'Company 2';
- cs.company.name = "Company 2";
- cs.company = 'Company 2 sys_ID';
- cs.setValue.company.name('Company 2');
- cs.setValue.company.name("Company 2");
- cs.setValue.company('Company 2 sys_ID');
Thank you,
Robert
p.s. please don't post links to general SN documentation pages (or SNGuru) unless they address this specific issue.
I've already been to these pages )and many others):
- https://www.servicenowguru.com/scripting/gliderecord-query-cheat-sheet/#comment-105156
- Populate record producer data and redirect users
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2018 10:14 AM
Robert,
sry for my ignorance i didn't saw your earlier comment
use below script in business rule:
if (current.company.name == 'company1')
current.company = sys_id of company 2
Using Client script:
- var cs = new GlideRecord('sn_customerservice_case');
- cs.addQuery('company.name','=','Company 2');
- cs.query();
if (g_form.getValue('company.name') == 'company1')
g_form.setValue('company',cs.sys_id);
for background:
- var company2 = '';
- var cs2 = new GlideRecord('sn_customerservice_case');
- cs2.addQuery('company.name','=','Company 2');
- cs2.query();
- if (cs2.next())
- company2 = cs2.sys_id;
- var cs = new GlideRecord('sn_customerservice_case');
- cs.addQuery('company.name','=','Company 1');
- cs.query();
- while(cs.next())
- {
- cs.setWorkflow(false);
- cs.autoSysField(false);
- g_form.setValue('company',company2);
- cs.update();
- }
Follow the step properly and you should get the result.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2018 10:27 AM
Will g_form will work in background script??
I think f_form is used for client side scripting
Please mark my response as correct and helpful if it helped solved your question.
-Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2018 11:34 AM
Good call out Prateek...I actually didn't even notice that. I just updated the syntax at the end in my own script which didn't use g_form and just used the variable for the new GlideRecord.
var.setValue('company','Company 2 sys_ID');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2018 11:40 AM
Great that you figured it out. Cheers Mate!
Please mark my response as correct and helpful if it helped solved your question.
-Thanks