Dude, I super super appreciate this. It needs to be made into a tutorial because I’m sure I’m not going to be the only person who needed to stop people creating higher roles!
Much love for this, I cant thank ALL of you enough for your help here.
Not convinced thats 100% the case, while it might not create duplicate it wouldnt return a an error message like making your own verification @GreatShakesBar think it might be worth double checking and maybe adding validation and message to match…
Not 100%, the two are independent and in theory editable separately.
You would want to do and exist entity api with the other validations and add the responce in the if &&…
Expect it will return true or false but never tried.
I knew you could work it all out though… I went on a boat, swam in the ocean, did some snorkeling, caught 5 Tuna on the way home, then had Sashimi dinner with friends until late in the night, and the power went out on the island. Great time, lots of JD, sun, and fun!
it was almost the same, but ive deleted what I did.
I think its ordered wrong? I don’t get the cancelled message, but says pin or username already in use lol
// 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');
}
}
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;
var addEmployeeAQ = dlg.AskQuestion('Add Employee Named: '+usr+' ?','OK,Cancel');
if ( addEmployeeAQ == 'OK' ) {
if (usr!='' && pin!='' && grp!='' && nameCount==0 && pinCount==0) {
// 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');
}
}
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
Not tested…