Business rule issue

Gopal14
Tera Contributor

Hello,

 

 

I want to create a new field called Attachment and type is True/False on Case table.  If a case has an attachment, it should mark the field as true and if not, it should remain false. 

 

Below is my field

 

Gopal14_0-1748407873437.png

 

Business Rule:

 

Gopal14_1-1748407913813.png

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

	if (current.table_name !== 'sn_customerservice_case') {
        return;
    }

    var caseId = current.table_sys_id;

    var attachmentGR = new GlideRecord('sys_attachment');
    attachmentGR.addQuery('table_name', 'sn_customerservice_case');
    attachmentGR.addQuery('table_sys_id', caseId);
    attachmentGR.query();

    var hasAttachments = attachmentGR.hasNext();
  
    var caseGR = new GlideRecord('sn_customerservice_case');
    if (caseGR.get(caseId)) {
        if (caseGR.u_attachment !== hasAttachments) {
            caseGR.u_attachment = hasAttachments;
            caseGR.update();
        }
    }
})();

 

I have written after insert delete BR,  above is my code, I have attached a attachment in one of the case record in case table.

 

I have added that field in list view, always it is showing as false

 

 

 

 

1 ACCEPTED SOLUTION

@Gopal14 

try to exclude the current sysId in query

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

    var caseId = current.table_sys_id;
    var attachmentRec = new GlideRecord('sys_attachment');
    attachmentRec.addQuery('table_sys_id', caseId);
    attachmentRec.addQuery('sys_id', '!=', current.sys_id); // exclude the current one
    attachmentRec.setLimit(1);
    if (!attachmentRec.hasNext()) {
        var caseGR = new GlideRecord('sn_customerservice_case');
        if (caseGR.get(caseId)) {
            caseGR.u_attachment = false;
            caseGR.update();
        }
    }

})();

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

13 REPLIES 13

SuyashJ41731830
Tera Contributor

Hi
@Gopal14 
Try this

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

// Only act on attachments related to Case table
if (current.table_name !== 'sn_customerservice_case') {
return;
}

var caseId = current.table_sys_id;

// Check if any attachments exist for this case
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_name', 'sn_customerservice_case');
attachmentGR.addQuery('table_sys_id', caseId);
attachmentGR.query();

var hasAttachments = attachmentGR.hasNext();

// Update the Case record's u_attachment field
var caseGR = new GlideRecord('sn_customerservice_case');
if (caseGR.get(caseId)) {
if (caseGR.u_attachment != hasAttachments) {
caseGR.u_attachment = hasAttachments;
caseGR.update();
}
}

})();

Regards,
Suyash

Chaitanya ILCR
Kilo Patron

HI @Gopal14 ,

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

	if (current.getValue('table_name') != 'sn_customerservice_case') { //better add this in the BR condition it self
        return;
    }

    var caseId = current.getValue('table_sys_id');

    var attachmentGR = new GlideRecord('sys_attachment');
    attachmentGR.addQuery('table_name', 'sn_customerservice_case');
    attachmentGR.addQuery('table_sys_id', caseId);
    attachmentGR.query();

    var hasAttachments = attachmentGR.hasNext();
  
    var caseGR = new GlideRecord('sn_customerservice_case');
    if (caseGR.get(caseId)) {
        if (caseGR.u_attachment != hasAttachments) {
            caseGR.u_attachment = hasAttachments;
            caseGR.update();
        }
    }
})();

try this

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

@Chaitanya ILCR  

 

I have updated the code as per your suggestion,

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

	if (current.getValue('table_name') !== 'sn_customerservice_case') {
        return;
    }

    var caseId = current.getValue('table_sys_id');

    var attachmentGR = new GlideRecord('sys_attachment');
    attachmentGR.addQuery('table_name', 'sn_customerservice_case');
    attachmentGR.addQuery('table_sys_id', caseId);
    attachmentGR.query();

    var hasAttachments = attachmentGR.hasNext();
  
    var caseGR = new GlideRecord('sn_customerservice_case');
    if (caseGR.get(caseId)) {
        if (caseGR.u_attachment !== hasAttachments) {
            caseGR.u_attachment = hasAttachments;
            caseGR.update();
        }
    }
})();

I have added a attachment for one of the case record

Gopal14_0-1748410340909.png

 

 

In the list of the case attachment field is showing as false.

 

Gopal14_1-1748410418203.png

 

Hi @Gopal14 ,

update the BR like this

 

ChaitanyaILCR_0-1748414313915.png

 

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

    var caseGr = new GlideRecord('sn_customerservice_case');

    if (caseGr.get(current.table_sys_id)) {
        var operation = current.operation();

        if (operation == 'insert') {
            caseGr.u_attachment = true;
            caseGr.update();
        } else {
            var attGr = new GlideRecord('sys_attachment');
            attGr.addEncodedQuery('table_name=sn_customerservice_case^table_sys_id=' + current.table_sys_id);
            attGr.query();
            caseGr.u_attachment = attGr.hasNext();
            caseGr.update();
        }
    }


})(current, previous);

 

 

alternative approach

 

if you can create the BR in Global scope here is the simple script

 

ChaitanyaILCR_0-1748413761586.png

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

	// Add your code here
	var caseGr = new GlideRecord('sn_customerservice_case');
	if(caseGr.get(current.table_sys_id)){
		caseGr.u_attachment = caseGr.hasAttachments();
		caseGr.update();
	}

})(current, previous);

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya