Create a new record on a different table on clicking a ui action

Prakash43
Tera Contributor

Hi, I have two custom table, table1 and table2 . Table 2 is child of table1.
I have a ui action "Fill Form" on table 1 on clicking of which i want a new record to be inserted in table 2 .
But it is also creating a new record in table 1 which i don't want 
How can i achieve this any help would be appreciated
Thank you.

Code which i am using on ui action:


var gr=new GlideRecord("table2");
{
gr.initialize();
gr.fields=current.fields;
gr.insert();
}

1 ACCEPTED SOLUTION

Hi Prakash

Try this code

var ans=new GlideRecord('u_filled'); //table 2 
    ans.initialize();
ans.u_owner = current.u_owner;
ans.u_list_field = current.u_list_field;
ans.u_choice_1 = current.u_choice_1;
ans.u_choice_2 = current.u_choice_2;
ans.u_choice_3 = current.u_choice_3;
ans.u_question_1 = current.u_question_1;
ans.u_question_2 = current.u_question_2;
ans.u_question_3 = current.u_question_3;
ans.u_question_4 = current.u_question_4;
    ans.insert();
    action.setRedirectURL(ans);

You will see child records on parent list too but when you open the record it will be in table2.


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

View solution in original post

7 REPLIES 7

Hi Ankur this is the complete script which i am using

var ans=new GlideRecord('u_filled');
{
    ans.initialize();
ans.u_owner = current.u_owner;
ans.u_list_field = current.u_list_field;
ans.u_choice_1 = current.u_choice_1;
ans.u_choice_2 = current.u_choice_2;
ans.u_choice_3 = current.u_choice_3;
ans.u_question_1 = current.u_question_1;
ans.u_question_2 = current.u_question_2;
ans.u_question_3 = current.u_question_3;
ans.u_question_4 = current.u_question_4;
    ans.insert();
    action.setRedirectURL(ans);
}

Hi,

are you sure there is no BR on u_filled table which is inserting data again into your current table

update script as this

var ans=new GlideRecord('u_filled');
ans.initialize();
ans.u_owner = current.u_owner;
ans.u_list_field = current.u_list_field;
ans.u_choice_1 = current.u_choice_1;
ans.u_choice_2 = current.u_choice_2;
ans.u_choice_3 = current.u_choice_3;
ans.u_question_1 = current.u_question_1;
ans.u_question_2 = current.u_question_2;
ans.u_question_3 = current.u_question_3;
ans.u_question_4 = current.u_question_4;
ans.insert();
action.setRedirectURL(ans);

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

kumaril1
Giga Expert

Hi Try below script

var gr = new GlideRecord("u_table2");
    gr.initialize();
    gr.setValue('u_tb1',current.u_t1);
    gr.setValue('u_tb2',current.u_t2);
    gr.insert();
    action.setRedirectURL(gr);