business rule

Prati123
Tera Contributor

I have created after insert business rule on sys_email table(condition: subject contains dmn). Basically when when end user reply to system mail with attachment that attachment should be copied to the demand record. Currently as per below script we are able to copy attachment from sys_attachment to demand but multiple attachment getting attached. I mean the same attachment 3 times. I want it to copy single attachment to demand. if end user send multiple email with attachment it should attach latest attachment to the demand.

Prati123_0-1752042218053.png

 

(function executeRule(current, previous) {

    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);

 

        // Trigger the workflow via event

        gs.eventQueue('dmn_demand.attachment.added', current, current.sys_id, gs.getUserID());

    }

})(current, previous);

1 ACCEPTED SOLUTION

Fabian Kunzke
Kilo Sage
Kilo Sage

Hey,

I want to start with: This should be an inbound action instead of a business rule! The sys_email table is one of high traffic and should be kept free of any business rules. Especially because we have inbound email actions for these exact use cases.

 

Alternatively you could also use a flow to archive the same thing: Move Email Attachments to Record action

Outside of that i would assume you BR might have an incorrect trigger and thus triggers more than once (again, solve this by using an inbound action instead).

 

Hope this helps.

Fabian

View solution in original post

6 REPLIES 6

Fabian Kunzke
Kilo Sage
Kilo Sage

Hey,

I want to start with: This should be an inbound action instead of a business rule! The sys_email table is one of high traffic and should be kept free of any business rules. Especially because we have inbound email actions for these exact use cases.

 

Alternatively you could also use a flow to archive the same thing: Move Email Attachments to Record action

Outside of that i would assume you BR might have an incorrect trigger and thus triggers more than once (again, solve this by using an inbound action instead).

 

Hope this helps.

Fabian

Rafael Batistot
Tera Sage

@Prati123 

Could you adjust your GlideRecord 

var attGR = new GlideRecord('sys_attachment');
    attGR.addQuery('table_sys_id', current.getUniqueValue());
    attGR.orderByDesc('sys_created_on');
    attGR.setLimit(1); // most recently 
    attGR.query();


Let me know if it's work for you