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

Please let me know if above script working fine?

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

No, actually  I need to show the related incident for every incident . Suppose I have INC0010086 and the related incident attached to it is INC0010090 , then for INC0010090 also i need to show INC0010086 in Related Incidents.

So, i wrote BR after on Incident(current) now and i need to copy the Parent and incident ticket number from Related table from the record attached while creating incident INC0010086 and insert a new one to same table while reverisng the values. so that it will be shown in related list of another child incident INC0010090 .

Ok,
Try this.

var gr = new GlideRecord('u_related_incident_emb');
gr.addQuery('parent', current.number);
gr.query();
if(gr.next())
{
    var inc = gr.getValue('u_incident_ticket_number');
    var par = gr.getValue('u_parent');
    var type = gr.u_link_type;
    gr.initialize();
    gr.setValue('u_parent', inc);
    gr.setValue('u_incident_ticket_number', par);
    if(type == 'succesisve') 
            gr.u_link_type = 'preceeding';
   else
             gr.u_link_type = 'succesisve';
    gr.insert();
}


Regards,
Rahul

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

Hi,
If my answer helped you in solving your query please mark it helpful!
Regards,
Rahul

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

dmathur09
Kilo Sage
Kilo Sage

Hi Servicenow,

After initializing I can see you are comparing the two values using "==", you should assign the value using "=". Update the code and see if that works for you.

If you think my response is helpful for you? If yes, mark it as correct answer and close the loop so that it would help future readers as well.

Regards,
Deepankar Mathur