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

Voona Rohila
Kilo Patron
Kilo Patron

Hi 

Try this

Modify field names and values accordingly.

(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;
    var par = gr.u_parent;
    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

Record get inserted but the refernce value of parent and Incident number not getting copied to each other .

Only the link type is updated with Successive .

 

See attached screenshot Capture1 

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

Hi Rohila,

Yes , i was confused which one to mark as correct both helped me in some way  !

But logic was yours only  ..!! Thanks

 

ThankYou.


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