Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Copying string field value into reference field

Community Alums
Not applicable

Hi There,

 

Greetings!!!

 

I am copying records from Table A to Table B on condition If field of Table C is set to "Selected"

I have a requirement to copy string value "Number" which is on Table A to reference field "CWD Number" on Table B

I have written a business rule, it's copying the task from Table A to Table B but not copying the Number to CWD Number:

 

Business Rule:

Tejas12_0-1703670376463.png

 

Tejas12_1-1703670400503.png

 

Script: 

(function executeRule(current, previous /*null when async*/ ) {

    var gr = new GlideRecord('x_infte_diversity_course_week_detail');
    gr.addQuery('course_number.sys_id', '535e228e478bfd1088119ac5536d43b7');
    gr.query();
    while (gr.next()) {
        var gr1 = new GlideRecord('x_infte_diversity_course_task_for_registered_user');
        gr1.initialize();
        var str_value = gr.number;
        //gs.info("The Number is :" +str_value);
        gr1.cwd_number.setDisplayValue(str_value);
        //gs.info("Number is :"+gr.number);
        //gr1.cwd_number = gr.number;
        //gr1.setDisplayValue("cwd_number", number);
        gr1.week_number = gr.week_number;
        gr1.description = gr.week_description;
        gr1.short_description = gr.agenda;
        gr1.insert();
    }
})(current, previous);
 
Note: It's not copying some of the fields.
19 REPLIES 19

Community Alums
Not applicable

Hi @vipinmathew ,

 

1) You have created After BR on which table?

Ans: It's third table "x_infte_diversity_super30_registration" on which condition is checked, if that is true


2) course_number field in x_infte_diversity_course_week_detail table is referencing to which table? -

Ans: No, There is a text field "Number" on "x_infte_diversity_course_week_detail" table which needs to be copied to cwd_number which is reference field on "x_infte_diversity_course_task_for_registered_user" table refering to "x_infte_diversity_course_week_detail" table


3) cwd_number field in x_infte_diversity_course_task_for_registered_user table is referencing to which table? Ans: refering to "x_infte_diversity_course_week_detail" table

Great @Community Alums ,

 

One more query ,

  • '535e228e478bfd1088119ac5536d43b7' is of which record? Registration Record or Course Week record or Registered User ?

Community Alums
Not applicable

@vipinmathew,

 

It's an another reference field present on "x_infte_diversity_course_week_detail". That is being used to copy records belongs to that course.

Cool,

 

if cwd_number is refering to "x_infte_diversity_course_week_detail" table then why you want to set the NUMBER field value to cwd , Since you are querying the same table in the first glide record?.

 

var gr = new GlideRecord('x_infte_diversity_course_week_detail');
gr.addQuery('course_number.sys_id', '535e228e478bfd1088119ac5536d43b7');
gr.query();

gs.log(gr.getRowCount());
while (gr.next()) {
var gr1 = new GlideRecord('x_infte_diversity_course_task_for_registered_user');
gr1.initialize();

gr1.setValue('cwd_number',gr.getValue('sys_id'));

gr1.week_number = gr.week_number;
gr1.description = gr.week_description;
gr1.short_description = gr.agenda;
gr1.insert();

 

ps: make sure gr.getRowCount() is greater than ZERO 

 

Please let me know your comments

Happy to Help 🙂

 

Regards,

Vipin Mathew

GingerCat
Tera Contributor

Please try to run your script in background/ fix script by setting some hardcoded values. check if it is working there.