Passing fields from REQ to sysapproval

Jake Golden
Tera Expert

Hello,

 

I am looking for some help on trying to pass information from the sc_request table to the sysapproval_approver table.

I have a requested_date field that is generated in my catalog and mapped to my request table. On my Approval I have the Summary of Request, which provides just a few items from the REQ. I have had no luck in changing this summary to providing more field values.

 

Another route I have investigated was using a business rule to push info from the REQ to the Approval.

I have been successful in creating a business rule to write information from my approval table to the REQ. But no luck the other direction. 

sys_approval to REQ (works)

    var gr = new GlideRecord('sc_request');
  gr.get(current.document_id);
  gr.u_test= "xxxxxxx";
  gr.update();

REQ to sys_approval (does not work)

    var gr = new GlideRecord('sysapproval_approver');
  gr.get(current.sys_id);
  gr.u_test2 = "xxxxxxx";
  gr.update();

With the code above, it appears it cannot find the REQ associated with Approval and it creates a new REQ with the gr.u_test2 value. I have tried current.number as well, no luck.     

 

1 ACCEPTED SOLUTION

chrisperry
Giga Sage

Hi there,

In your REQ to sys_approval business rule, you are telling ServiceNow to try and retrieve a sysapproval_approver record w/ a non-existent sys_id, because the sys_id you are passing is of the REQ record, not the sysapproval_approver record. You can update your .get() query as below to properly query the sysapproval_approver table based on REQ sys_id.

Additionally, to follow best practices, you should add an if () condition around your get() to ensure the update only happens if ServiceNow actually finds the record, otherwise you will see adverse results like a new REQ getting inserted as you mentioned.

Try updating your code as below:

var gr = new GlideRecord('sysapproval_approver');
if (gr.get('document_id', current.getUniqueValue())) {
    gr.u_test2 = "xxxxxxx";
    gr.update();
}

If this answer is helpful please mark correct and helpful!

Regards,

Christopher Perry

If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry

View solution in original post

4 REPLIES 4

chrisperry
Giga Sage

Hi there,

In your REQ to sys_approval business rule, you are telling ServiceNow to try and retrieve a sysapproval_approver record w/ a non-existent sys_id, because the sys_id you are passing is of the REQ record, not the sysapproval_approver record. You can update your .get() query as below to properly query the sysapproval_approver table based on REQ sys_id.

Additionally, to follow best practices, you should add an if () condition around your get() to ensure the update only happens if ServiceNow actually finds the record, otherwise you will see adverse results like a new REQ getting inserted as you mentioned.

Try updating your code as below:

var gr = new GlideRecord('sysapproval_approver');
if (gr.get('document_id', current.getUniqueValue())) {
    gr.u_test2 = "xxxxxxx";
    gr.update();
}

If this answer is helpful please mark correct and helpful!

Regards,

Christopher Perry

If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry

@christopherperry That worked perfectly, thank you.

In the code if I wanted to call upon a specific field from the REQ how would that look?

So we have gr.u_test2 = "xxxxx"; 

Which returns a hard value. But would like to know how to change it dynamically. Not sure on the proper configuration after the = 

Still very much so a novice, so thank you again on the help you already provided. 

 

No worries at all, you gotta start somewhere on your ServiceNow journey 🙂

Sure so you could set specific fields from the REQ like below:

var gr = new GlideRecord('sysapproval_approver');
if (gr.get('document_id', current.getUniqueValue())) {
    gr.u_test2 = current.getValue('req_field_name_here'); //replace with the specific REQ field name you want to use
    gr.update();
}

If this answer is helpful please mark correct and helpful!

Regards,

Christopher Perry

If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry

Awesome, this should help out with many different tasks and understanding whats going on. thank you!