Mail script to add field from a different table

annasc
Tera Contributor

Hello,

I'm trying to add a mail script to my email notification but need help with the script. 

My email notification is on the "sn_vdr_risk_asmt_vdr_tiering_assessment" table.

I need help with the mail script to get the value from the field "TPRM-01.01" and ADD it on to my email notification.

This field "TPRM-01.01" (u_ucla_ociso_tprm_triage_tprm_01_01) is on the "sn_vdr_risk_asmt_ucla_ociso_tprm_triage" table.

Please kindly help me with the mail script.  Thank you.

 

1 ACCEPTED SOLUTION

Hi,

It is working based on your query that you provided because you are not giving any condition and using while Loop so will return all the values. If you need single value use If Loop.

Please add condition grrisk.addQuery(); then it will display the data.

You need to filter the data. Then Use like below

template.print("TPRM-01:01"+grrisk.getDisplayValue('u_ucla_ociso_tprm_triage_tprm_01_01'));

 

Hope you understood it!!

If it Please Mark ✅ Correct/Helpful, Thanks!! 

 

Regards

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

View solution in original post

6 REPLIES 6

Sagar Pagar
Tera Patron

Hi,

You can access the all fields of [sn_vdr_risk_asmt_vdr_tiering_assessment] table using current object in email scripts.

 

For example-

if it is reference field - currect.u_ucla_ociso_tprm_triage_tprm_01_01.getDisplayValue();

if it is String field - currect.u_ucla_ociso_tprm_triage_tprm_01_01.toString();

 

 

Feel free to mark helpful & correct!

Thanks,
Sagar Pagar

The world works with ServiceNow

Pavankumar_1
Mega Patron

Hi,

If form has a field with reference table of sn_vdr_risk_asmt_ucla_ociso_tprm_triage you can get it without code. Expand the reference field and click on the field which need.

Refer below screenshot here I expanded the caller field and clicked on the  active field to get it.

Caller Active: ${caller_id.active} it will populate automatically. 

find_real_file.png

If you have specific conditions to get the value then You can try below email script to get the required value and give conditions on addQuery if it has any relation.

 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {


    var grrisk = new GlideRecord('sn_vdr_risk_asmt_ucla_ociso_tprm_triage');
    // grrisk.addEncodedQuery('add your encoded query here if you have any');
    //grrisk.addQuery('parent',sys_id); // you can add the reference fileds if it has paent child relationship.ex: //parent, current sysid;
    grrisk.query();
    while (grrisk.next()) {
        template.print(grrisk.u_ucla_ociso_tprm_triage_tprm_01_01); //use this if the target field is string 
        template.print(grrisk.getDisplayValue('u_ucla_ociso_tprm_triage_tprm_01_01')); //use this if the target field is reference field
    }

 

})(current, template, email, email_action, event);

 

Hope it helps!!

If it Please Mark ✅ Correct/Helpful, Thanks!! 

 

Regards

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Hi Pavankumar,
 
It's a choice field.  I tried the script you provided, and it's getting ALL the values.  Can you help me display the Choice field name + the selected answer only?  The email should only display "TPRM-01:01  Protection Level 4".  See sample below:
 
 
find_real_file.png
 
Currently, it's showing all these values (see below).  I've also pasted the script.  Your assistance would be greatly appreciated.  Thank you so much for your time!
 
Displaying all these values:
 
Protection Level 2Protection Level 4Protection Level 4nullnullProtection Level 4Protection Level 4nullnullProtection Level 4Protection Level 4Protection Level 4Protection Level 2Protection Level 4Protection Level 4Protection Level 4nullProtection Level 4Protection Level 4Protection Level 4Protection Level 2nullProtection Level 3Protection Level 4Protection Level 4Protection Level 4Protection Level 2Protection Level 2nullProtection Level 4Protection Level 2Protection Level 4Protection Level 4Protection Level 3nullProtection Level 2Protection Level 4nullnullProtection Level 2Protection Level 2nullProtection Level 4Protection Level 1Protection Level 2nullProtection Level 4Protection Level 4Protection Level 2nullProtection Level 2Protection Level 4Protection Level 4Protection Level 4nullProtection Level 1Protection Level 2nullnullProtection Level 4Protection Level 2Protection Level 1nullnullProtection Level 4Protection Level 2Protection Level
 
Here is the script:
 
var grrisk = new GlideRecord('sn_vdr_risk_asmt_ucla_ociso_tprm_triage');
   â€‚ 
    grrisk.query();
      
    while (grrisk.next()) {
              
        template.print(grrisk.getDisplayValue('u_ucla_ociso_tprm_triage_tprm_01_01')); /
       

Hi,

It is working based on your query that you provided because you are not giving any condition and using while Loop so will return all the values. If you need single value use If Loop.

Please add condition grrisk.addQuery(); then it will display the data.

You need to filter the data. Then Use like below

template.print("TPRM-01:01"+grrisk.getDisplayValue('u_ucla_ociso_tprm_triage_tprm_01_01'));

 

Hope you understood it!!

If it Please Mark ✅ Correct/Helpful, Thanks!! 

 

Regards

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar