servicenowbusiness-rulesservicenow-rest-api

Business Rule Cut out "To" and "CC" into "BCC" when send Email in Servicenow


i want to add a Business Rule in ServiceNow. When i add a new record to the sys_email table with "To"(direct) or "CC"(copied) i want before insert into the table a rule who copy the "to" or "cc" filled fields into the "bcc" filed after the submit and delete the "to" and "cc" entrys. This is my code so far which does not change something. Iam rly new to ServiceNow maybe someone can help me?

 (function executeRule(current, previous /*null when async*/) {

var gr = new GlideRecord('sys_email');

//gr.newRecord();

gr.addQuery('direct','current.direct');
gr.addQuery('copied','current.copied');
gr.query();
while(gr.next()) 
{
 gr.blind_copied = current.direct +', '+ current.copied;
 gr.update();
}


  })(current, previous);

Solution

  • To do this, make sure to use a before business rule on insert and update for the sys_email table. You are able to alter the fields in the current record before it is inserted.

    (function executeRule(current, previous /*null when async*/) {
    
        // create variables for use from current record
        var to = current.direct;
        var cc = current.copied;
        var bcc += to + '; ' + cc;
    
        // update the current record
        current.direct = '';
        current.copied = '';
        current.blind_copied = bcc;
    
    })(current, previous);