Make form read only in native view But editable in workspace

Prajwal G Vaish
Giga Guru

Hi Team,
In the form on Native view have created a button which leads to workspace to the same record. I require the form to be read only in native view but should be editable in workspace view.
Please let me know How can we achieve This.
Thank you.

1 ACCEPTED SOLUTION

Prajwal G Vaish
Giga Guru

I have used the Below Code and its working fine.

function onLoad() {
    //Type appropriate comment here, and begin script below
    var url = top.location.href; // getting the URL Link
    var pathname = new URL(url).pathname; // getting the Path workspace : now/risk/, Native :now/nav/ui/.
    pathname = pathname.split('/');
    var ur = pathname[2];// getting if its a workspace or native
    if ( ur == 'nav') {
        var fields = g_form.getEditableFields();
        for (var x = 0; x < fields.length; x++) {
            g_form.setReadOnly(fields[x], true);
        }
    }else{
        fields = g_form.getEditableFields();
        for ( x = 0; x < fields.length; x++) {
           g_form.setReadOnly(fields[x], false);
        }
    }
}

 

 

Regards,
Prajwal

View solution in original post

7 REPLIES 7

Ahmmed Ali
Mega Sage

You can check below thread to check if you are in workspace or not, then based on that write script to make your form field readonly.

 

https://www.servicenow.com/community/now-platform-forum/how-to-determine-if-you-are-in-workspace-in-... 

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

Ankur Bawiskar
Tera Patron
Tera Patron

@Prajwal G Vaish 

you can use onLoad client script and check the view and use this script to make all fields readonly

function onLoad(){

	if(g_form.getView() != 'workspace'){
		var fields = g_form.getEditableFields();
		for (var x = 0; x < fields.length; x++) {
			g_form.setReadOnly(fields[x], true);
		}
	}
}

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,

Thank you for your response. g_form.getView() is giving undefined. But thank you for providing an idea.

@Prajwal G Vaish 

it should give the view name when the client script runs in workspace view

what did you debug?

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