workflow

Prati123
Tera Contributor

I have created workflow and created inbound email to get attachment from the user and the attachment should be copied to demand. I created BR for this part is working fine but after checking attachment in demand it should trigger approval but it seems to be workflow is stuck. 

Prati123_0-1751894803029.png

 

BR:sys_email

After insert, update

condition: subject contains INC

(function executeRule(current, previous /*null when async*/ ) {
    var attGR = new GlideRecord('sys_attachment');
    attGR.addEncodedQuery('table_sys_id=' + current.getUniqueValue());
    attGR.query();

    if (attGR.next()) {
        var gsa = new GlideSysAttachment();
        gsa.copy('sys_email', attGR.table_sys_id, 'dmn_demand', current.instance.sys_id);
       
    }
})(current, previous);
 
Wait for condition in workflow:
(function checkAttachments(current) {
    var attachmentGR = new GlideRecord('sys_attachment');
   
    // Query for attachments related to the current record
    attachmentGR.addQuery('table_sys_id', current.sys_id.toString());
    attachmentGR.addQuery('sys_created_by', 'system'); // Additionally check if created by 'system'
    attachmentGR.query();
 
    // Set 'answer' to true if any attachments are found
    answer = attachmentGR.hasNext(); // Returns true if an attachment exists
 
})(current);
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Prati123 

wait for condition only works if workflow is waiting on current record.

your workflow wait script is checking sys_attachment but workflow is on other table (Demand)

you need to broadcast update to that workflow so that it check the wait for condition script again

use this in your business rule

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

    var attGR = new GlideRecord('sys_attachment');
    attGR.addEncodedQuery('table_sys_id=' + current.getUniqueValue());
    attGR.query();
    if (attGR.next()) {
        var gsa = new GlideSysAttachment();
        gsa.copy('sys_email', attGR.table_sys_id, 'dmn_demand', current.instance.sys_id);

        var record = new GlideRecord('dmn_demand');
        record.get(current.instance.sys_id);
        new Workflow().broadcastEventToCurrentsContexts(record, 'update', null);
    }

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

3 REPLIES 3

Mark Manders
Mega Patron

You could have done this with an inbound flow. The attachment could be copied to the record and you could just proceed from there, including the approval step. No need for any scripts.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Ankur Bawiskar
Tera Patron
Tera Patron

@Prati123 

wait for condition only works if workflow is waiting on current record.

your workflow wait script is checking sys_attachment but workflow is on other table (Demand)

you need to broadcast update to that workflow so that it check the wait for condition script again

use this in your business rule

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

    var attGR = new GlideRecord('sys_attachment');
    attGR.addEncodedQuery('table_sys_id=' + current.getUniqueValue());
    attGR.query();
    if (attGR.next()) {
        var gsa = new GlideSysAttachment();
        gsa.copy('sys_email', attGR.table_sys_id, 'dmn_demand', current.instance.sys_id);

        var record = new GlideRecord('dmn_demand');
        record.get(current.instance.sys_id);
        new Workflow().broadcastEventToCurrentsContexts(record, 'update', null);
    }

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar , I updated BR after attaching file it is triggering random approval from the workflow. It should be sequential and for the demand it it attaching multiple attachment. like 1from the system, 2from the user who sent it (all are same attachment), Do I need to update workflow(update/remove wait for condition)? And this time when system spending for approval it should send mail to approves for approve/reject it with the attachment present on current record.