- 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-08-2018 05:17 PM
Robert use sys id of comapny 2 record instead of compnay 2 displayname
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2018 07:20 AM
Hi Kunal,
As I wrote in my example above, I did...but I see that in the list of things I tried above, I mistyped the dot-walk to Name. I'll correct that in my example above. That being said, I typically use sys_ID on Reference fields when scripting.
Regards,
Robert
- 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:25 AM
No worries and THANK YOU!!!
Incorrect: cs.setValue.company('Company 2 sys_ID');
Correct: cs.setValue('company','Company 2 sys_ID');
And I knew this too, which makes it especially irritating...