Business Rules

MohdF
Tera Contributor

Hi, I am creating a business rule where I want to copy the attachment from incident table to assigned problem table, and I selected before insert and update business rule.

This is my script..

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

    var prb = new GlideRecord('problem');
    prb.addQuery('parent', current.getUniqueValue());
    prb.query();
    if (prb.next()) {
        GlideSysAttachment.copy('incident', current.getUniqueValue(), 'problem', prb.current.getUniqueValue());
    }

})(current, previous);

I do not what I am doing wrong but this is not working.

Please help.

 

Thanks 

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

'Current' refers to the record and table that the Business Rule is running on.  Out of the box, you can relate a problem to an incident using the problem_id field on the incident table.  If you are using the parent field on the problem table to identify incidents, then your script is fine to start with, but be sure to add to the Filter Condition Parent changes so that it doesn't copy every time the incident is updated.  In this case, the fourth parameter in the copy function of GlideSysAttachment to identify the problem record should just be prb.getUniqueValue() 

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

    var prb = new GlideRecord('problem');
    prb.addQuery('parent', current.getUniqueValue());
    prb.query();
    if (prb.next()) {
        GlideSysAttachment.copy('incident', current.getUniqueValue(), 'problem', prb.getUniqueValue());
    }

})(current, previous);

 

View solution in original post

4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

'Current' refers to the record and table that the Business Rule is running on.  Out of the box, you can relate a problem to an incident using the problem_id field on the incident table.  If you are using the parent field on the problem table to identify incidents, then your script is fine to start with, but be sure to add to the Filter Condition Parent changes so that it doesn't copy every time the incident is updated.  In this case, the fourth parameter in the copy function of GlideSysAttachment to identify the problem record should just be prb.getUniqueValue() 

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

    var prb = new GlideRecord('problem');
    prb.addQuery('parent', current.getUniqueValue());
    prb.query();
    if (prb.next()) {
        GlideSysAttachment.copy('incident', current.getUniqueValue(), 'problem', prb.getUniqueValue());
    }

})(current, previous);

 

Thank you so much Brad, it worked.

Good news - you are welcome!

Soni Tushar
Tera Guru

Hello @MohdF ,

 - After Update Business Rule

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

    // Ensure that the incident has an associated problem record
    if (current.problem_id && current.problem_id.changes()) {
        var problemSysId = current.problem_id.sys_id;

        var attachmentGr = new GlideRecord('sys_attachment');
        attachmentGr.addQuery('table_sys_id', current.sys_id);
        attachmentGr.query();
        
        // Copy each attachment from the incident to the problem
        while (attachmentGr.next()) {
            GlideSysAttachment.copy('incident', current.sys_id, 'problem', problemSysId);
        }
    }
})(current, previous);

 

If you found my response helpful, please consider marking it as "Helpful" or "Accept Solution." Thank you!