Lower User Roles not allowed to create higher Role

Try this;

function createUser(u,p) {
  // can get User and Pin fed via function or 
  //use Data.Get to retrieve Program Settings
  var usr = (typeof u === 'undefined' || u=='' ? Data.Get("Username") : u);
  var pin = (typeof p === 'undefined' || p=='' ? Data.Get("PIN") : p);
  var grp = 'Employee';

  var q="SELECT [Id] FROM [UserRoles] WHERE [Name]='" + grp + "'";
  var grp = sql.Query(q).First;

  // more validation
  usr = (typeof usr === 'undefined' ? '' : usr);
  pin = (typeof pin === 'undefined' ? '' : pin);
  grp = (typeof grp === 'undefined' ? '' : grp);
  
  //check for existing user with that name
  var q="SELECT COUNT([Name]) AS [COUNT] FROM [Users] WHERE [Name]='" + usr + "'";
  var nameCount = sql.Query(q).First;

  //ceck for existing user with that pincode
  var q="SELECT COUNT([Name]) AS [COUNT] FROM [Users] WHERE [PinCode]='" + pin + "'";
  var pinCount = sql.Query(q).First;

  if (usr!='' && pin!='' && grp!='' && nameCount==0 && pinCount==0) {
		
		var addEmployeeAQ = dlg.AskQuestion('Add Employee Named: '+usr+' ?','OK,Cancel'); 
		
		if ( addEmployeeAQ == 'OK' ) {

			// run SQL to create the User
			q = "INSERT INTO [Users] ([PinCode], [Name], [UserRole_Id]) VALUES ('" + pin + "', '" + usr + "', '"+grp+"')";
			var r = sql.ExecSql(q);

			// create user entity
			api.Entity(usr).Create('Staff');
			dlg.AskQuestion('Employee named '+usr+' added.','OK');

		} else {
			dlg.AskQuestion('Adding User '+usr+' Aborted','OK');
		}
	} else {
	dlg.AskQuestion('User or PinCode Already in use.','OK');
	}
}

Have moved the add question inside the validated stage… no point in asking if its going to fail anyway :slight_smile:
Not tested…

Woot, thanks buddy.

Mucho love

Does it work ok?..

yep perfect mate, appreciate it.

Woohoo :smiley:

Just gave you some love :slight_smile:

lmao, you guys…!!!

What you laughing at?

kendash just liked about a million of my posts lol

Arrrrm you want some more? :stuck_out_tongue:

best not, else ill abuse the like system too lol

You have already hav’t you :stuck_out_tongue:

who me? :slight_smile: dot dot dot

Ive been playing a bit of a sad game with myself for a while now, limiting likes given to try and get a better ratio of received:given…
2.1k:2.4k at the moment.

Youve been helping me allot recently LOL :stuck_out_tongue:

lmao, best stop liking

Not not giving any just being more restricted. Always like where likes due :stuck_out_tongue:

oops weve gone off topic, this should be split into lounge lol

Thanks so much GreatShakesBar, it works very well, maybe you must put your post #127 as a Solution for the thread, so new people looking for the answer can see it at top of the thread.
Do you have another script for changing password in the Navigation Screen?

Thanks again and cheers!

It sounds stupid but I want to know if there is a way JUMP to post #127?
I know I am lazy. LOL

Like this:

https://forum.sambapos.com/t/lower-user-roles-not-allowed-to-create-higher-role/12666/127
... look waaaaaaaaaaaaaaaaaaaay over heeeeeeeeeeeeere ----->>>--------->>>------------^^^

I makes the following link, and you can click on the UP Arrow in the top-right corner to jump: