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

SunilKumar_P
Giga Sage

Hi @Community Alums,

 

Can you try gr1.cwd_number = gr.sys_id as its a reference field.

 

Regards,
Sunil

 

Community Alums
Not applicable

Hi @SunilKumar_P ,

 

Thanks for the reply.

 

I tried your suggestion, it's not working.

Hi @Community Alums, Can you try changing the addQuery as below? you have it as course_number.sys_id

 

 Reference type fields returns sys_id's so its not required to mention the sysId in the query method.

 

    gr.addQuery('course_number''535e228e478bfd1088119ac5536d43b7');

 

Regards,

Sunil

 

Tai Vu
Kilo Patron
Kilo Patron

Hi @Community Alums 

Let's give my below adjustment a try.

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

    var grCWD = new GlideRecord('x_infte_diversity_course_week_detail');
    grCWD.addQuery('course_number', current.getUniqueValue()); //current record trigger the rule
    grCWD.query();
    while (grCWD.next()) {
        var grCourseTask = new GlideRecord('x_infte_diversity_course_task_for_registered_user');
        grCourseTask.initialize();
        grCourseTask.cwd_number = grCWD.getUniqueValue(); //or grCWD.sys_id
        grCourseTask.week_number = grCWD.week_number;
        grCourseTask.description = grCWD.week_description;
        grCourseTask.short_description = grCWD.agenda;
        grCourseTask.insert();
    }

})(current, previous);

 

Cheers,

Tai Vu