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

Voona Rohila
Kilo Patron
Kilo Patron

Hi prakash

try this

var gr=new GlideRecord("table2");
gr.initialize();
gr.field1=current.field1; //map all your fields .
gr.field2 = current.field2;
gr.insert();
action.setRedirectURL(gr); //use this line for redirection to table2 record.

 

 

 


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

Hi @Rohila Voona , thank you for your response but it still creating a record in table 1

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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

your script seems incomplete

Regards
Ankur

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