Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to prevent records from being created/updated under certain cases.

emyrold
Giga Expert

We have a custom table where I have written (what I thought was a simple) BR to prevent a record from being created if:

- for every field_01 + field_02 combination, there needs to be a unique field_03 value.

e.g. field_01 = appName, field_02 = environment and field_03 = profileName

There should always be only one profileName for every appName and environment combination.

The table has many other columns per record, but these are the key fields.

The issue I have, is my (before) BR (with both insert and update checked) works when I create new records.   And when I update an existing record where I change profileName frome "Joe" to "Sally" (assuming Sally does not already exist).   However, if I now go and change any other value of other fields on the record, it prevents the update because profileName already exists.   The only work-a-round for now until I fix this logic is that when I want to change one of the other field values, I have to temporarily change profileName to something unique while I make the save and then again rename it back to the original profileName again.

So is there a way to do this in one BR or do I need two, one for insert and one for update?

var tla = current.u_tla;

var env = current.u_environment;

var profile = current.u_profile;

var hostGR = new GlideRecord('u_cdm_host_migration');

hostGR.addQuery('u_tla', tla);

hostGR.addQuery('u_environment', env);

hostGR.addQuery('u_profile', profile);

hostGR.query();

if (hostGR.next()) {

  current.u_profile.setError('Profile Name: ' +profile+ ' already exists for this TLA and Environment');

  current.setAbortAction(true);

}

1 ACCEPTED SOLUTION

Tanaji Patil
Tera Guru

Hi emyrold,



From your explanation what I usderstand is there should always be only one combination of 'u_tla' + 'u_environment' + 'u_profile'. While creating a new record the uniqueness must be checked. While updating an existing record, if this combination is touched then again check the uniqueness. But if the combination of these three fields is not touched then there is no need of checking the uniqueness and let them update the record.



Please use below modified code and let me if you need anything else. If you want any change in the combination you can simply change the conditions in '(tla.changes() || env.changes() || profile.changes())'.



var tla = current.u_tla;  


var env = current.u_environment;  


var profile = current.u_profile;  



if(!current.isValidRecord() || (tla.changes() || env.changes() || profile.changes()))


{


var hostGR = new GlideRecord('u_cdm_host_migration');  


hostGR.addQuery('u_tla', tla);  


hostGR.addQuery('u_environment', env);  


hostGR.addQuery('u_profile', profile);  


hostGR.query();  


 


if (hostGR.next())


{  


  current.u_profile.setError('Profile Name: ' +profile+ ' already exists for this TLA and Environment');  


  current.setAbortAction(true);  


}


}


View solution in original post

11 REPLIES 11

Hi emyrold,



Ya thats great! We should always avoid modifying OOB scripts. So its better to create a new UI Action.


Thanks for the info.



Regards,


Tanaji


Hi emyorld,



If your issue is solved please consider marking the correct answer:)



Thanks,


Tanaji


Hi Tanaji,



I cannot seem to find a way to mark your reply as the correct answer... been looking for a while now... will continue to look but any suggestions would be great...


Hi emyrold,



Under the replies you may find 'Mark Correct' & 'Mark Helpful' buttons.



Thanks,


Tanaji


Hi Tanaji,



I actually had to send an email to the community support to change my post to a question-post... those buttons were not showing at all.   However, now they are... So I will go and mark your post.   Thanks so much for your help!   -Erik