View Rule to Apply to New Records Only

apjohn2
Mega Sage

Hi all! Looking for help w/ an Advanced View Rule (seems it has to be advanced, from what I've seen) such that if a user starts a new record in the Change Request table, the view defaults to the "Default view". (Our issue is very similar to this, but I'm not finding a viable answer there or elsewhere.)

A friend told me about View Rules and helped me create one, but it has no conditions. Really the only condition I have is that the record showing is a new record (i.e., if the URL contains 'sys_id=-1'). We don't want to force the view if the user pulls up an existing record, primarily because I don't have a good reason to do that--the problem is limited to new records. 🙂

I'm brand-spanking-new to GlideURI and don't have a handle on it yet. I expect that's what I'll need, but am not sure how to set it up in the Advanced Script. The latest version of what I've tried is below (not working).

(function overrideView(view, is_list) {
  var url = gs.action.getGlideURI().getMap();
  var sysId = url.get('sys_id');
  if (sysId === -1) {
    answer = '';  // set the new view to answer
  }
})(view, is_list);

Thanks in advance for any help here. Let me know if more details are needed.

Thanks!

 

1 ACCEPTED SOLUTION

Muralidharan BS
Mega Sage
Mega Sage

HI Aaron,

I tried on Incident table and it worked for me, here are the screens, 

 

view rule

find_real_file.png

 you can also do sysid is empty

Incident record

find_real_file.png

View solution in original post

3 REPLIES 3

Muralidharan BS
Mega Sage
Mega Sage

HI Aaron,

I tried on Incident table and it worked for me, here are the screens, 

 

view rule

find_real_file.png

 you can also do sysid is empty

Incident record

find_real_file.png

Anil Lande
Kilo Patron

Hi,

Instead you can use current.isNewRecord()  to check whether it is new record or existing one.

 

(function overrideView(view, is_list) {
  if (current.isNewRecord()) {
    answer = '';  // set the new view to answer
  }
})(view, is_list);

 

 

Thanks,

Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Nicola Attico
ServiceNow Employee
ServiceNow Employee

non advanced with condition "sys_id is undefined" seems to work for me as well