Having trouble populating a Reference Field from Background Script

Robert_Cartwrig
Tera Expert

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):

1 ACCEPTED SOLUTION

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:


  1. var cs = new GlideRecord('sn_customerservice_case');  
  2. cs.addQuery('company.name','=','Company 2');  
  3. cs.query();

if (g_form.getValue('company.name') == 'company1')


        g_form.setValue('company',cs.sys_id);




for background:



  1. var company2 = '';
  2. var cs2 = new GlideRecord('sn_customerservice_case');  
  3. cs2.addQuery('company.name','=','Company 2');  
  4. cs2.query();  
  5. if (cs2.next())
  6. company2 = cs2.sys_id;
  7. var cs = new GlideRecord('sn_customerservice_case');  
  8. cs.addQuery('company.name','=','Company 1');  
  9. cs.query();  
  10.  
  11. while(cs.next())  
  12. {  
  13.  
  14. cs.setWorkflow(false);  
  15. cs.autoSysField(false);  
  16. g_form.setValue('company',company2);
  17. cs.update();  
  18. }


Follow the step properly and you should get the result.




View solution in original post

7 REPLIES 7

Kunal Jha
Giga Expert

Robert use sys id of comapny 2 record instead of compnay 2 displayname


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


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:


  1. var cs = new GlideRecord('sn_customerservice_case');  
  2. cs.addQuery('company.name','=','Company 2');  
  3. cs.query();

if (g_form.getValue('company.name') == 'company1')


        g_form.setValue('company',cs.sys_id);




for background:



  1. var company2 = '';
  2. var cs2 = new GlideRecord('sn_customerservice_case');  
  3. cs2.addQuery('company.name','=','Company 2');  
  4. cs2.query();  
  5. if (cs2.next())
  6. company2 = cs2.sys_id;
  7. var cs = new GlideRecord('sn_customerservice_case');  
  8. cs.addQuery('company.name','=','Company 1');  
  9. cs.query();  
  10.  
  11. while(cs.next())  
  12. {  
  13.  
  14. cs.setWorkflow(false);  
  15. cs.autoSysField(false);  
  16. g_form.setValue('company',company2);
  17. cs.update();  
  18. }


Follow the step properly and you should get the result.




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...