I want to insert a new record to a table , with the value interchanged between two reference field .

Servicenow12
Tera Contributor

I want to insert a new record to a table , with the value interchanged between two reference field 

Eg : Screenshot (A)

In my related incident table ( embedded list in Incident form  where I am adding existing incident ), once a record is inserted with Parent and incident Ticket Number and Link type , plese refer screenshot A

I want to insert another record where the Parent value  copies Incident Ticket Number value , Incident ticket number copies  Parent field value 

and Linky type if preceeding then get converted to Successive and Vice Versa, See Screenshot (B)

Below is the Before insert and Update  BR i wrote on Incident table :

(function executeRule(current, previous /*null when async*/) {
    
var gr = new GlideRecord('u_related_incident_emb');
gr.addQuery('parent', current.number);
gr.query();
if(gr.next())
    
{
    gr.initialize();
    gr.u_parent == gr.u_incident_ticket_number;
    gr.u_incident_ticket_number == gr.u_parent;
    gr.insert();
}


})(current, previous);

 

Can somebody advise what is wrong or the correct approach ?

1 ACCEPTED SOLUTION

Hi 

Sorry to mention this but the complete logic was provided by me and you marked someone's response as correct. there could be some changes but still the logic matters.

even below code works without using setValue/getValue.

(function executeRule(current, previous /*null when async*/) {
    
var gr = new GlideRecord('u_related_incident_emb');
gr.addQuery('parent', current.number);
gr.query();
if(gr.next())
    
{
    var inc = gr.u_incident_ticket_number.toString();
    var par = gr.u_parent.toString();
    var type = gr.u_link_type;
    gr.initialize();
    gr.u_parent =inc;
    gr.u_incident_ticket_number =par;
    if(type == 'succesisve') 
            gr.u_link_type = 'preceeding';
   else
             gr.u_link_type = 'succesisve';
    gr.insert();
}


})(current, previous);

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

14 REPLIES 14

Rahul Talreja
Mega Sage
Mega Sage

Hi,

Please try changing BR to an After BR rule and your new record will be added after the first record is inserted.

I wanted to confirm that you wanted to insert the record in same table? or a different table.

Regards,
Rahul

Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul

Same table only 

In after insert business rule you need not to query the table, you can directly initialize as below.

var gr = new GlideRecord('u_related_incident_emb');
gr.initialize();
gr.u_parent = current.getValue('u_incident_ticket_number');
gr.u_incident_ticket_number = current.getValue('u_parent');
gr.insert();

Regards,
Rahul

Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul

The problem i am mainly facing is to copy the value of Parent to Incident Ticket Number and Vise Versa ..as these both are reference field .

How to copy the refernce field to another reference field at server side?