How to make attachment 'read-only' on Service Portal ("g_form.disableAttachments" not working)?

Aki18
Tera Contributor

I created the following Client Script to make attachment 'read-only' when "u_state" is NOT "2".

function onLoad() {
	var state = g_form.getValue('u_state');
	if (state != '2')  {
		g_form.disableAttachments();
	}
}

It works properly for Classic (Platform) UI, but NOT for Service Portal...

Is this method NOT compatible with SP? If so, is there any alternative way to make attachment 'read-only' based on the condition above?

 

Best Regards,

Aki

1 ACCEPTED SOLUTION

@Aki18 TRY THIS 

if (current.table_name == "custom_table_name") {
    var gr = new GlideRecord('custom_table_name');
    gr.addQuery('sys_id', current.table_sys_id);
    gr.addEncodedQuery('u_state!=2');
    gr.query();
    if (gr.next()) {
        answer = false;
    } else {
        answer = true;
    }
}

Also try to put some logs in if loop to check if its going inside for loop 

Thanks

View solution in original post

17 REPLIES 17

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Aki18 ,

Can you try below script :-

function onLoad() {
var state = g_form.getValue('u_state');
	if (state != '2')  {
        var attachmentButton = top.document.getElementsByTagName("sp-attachment-button");
        //console.log(attachmentButton);
        attachmentButton[0].parentNode.hidden = true;
}
}

Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

Hi @Gunjan Kiratkar ,

Thank you so much for your reply! Looks like it disabled the "Clip" icon for adding new attachment, but the user can still remove existing attachment by clicking on "x".

Do you have any ideas on how to disable "x" icon as well to prevent users from deleting the attachment?

Aki18_0-1668676710415.png

 

Hi @Aki18 ,

All you need to do is restrict write access to the record when the state is closed. Restricting the edit access, automatically remove the attachment capability.

 

Update Write ACL on your table to restrict edit access when the state is 2.


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

Hi @Gunjan Kiratkar ,

My requirement is like when "u_state" is NOT "2", make all the field (including attachment) read-only for itil users, but only 1 custom field is allowed to edit as an exception.

How can I achieve this using ACL?

 

I created