Change Entity Name via Automation

Is this possible? or is it dangerous? I want to be able to change an entity name

Maybe I should clone current ticket and void everything through automation?

Use the api to update it

1 Like

ahh good shout, I rekon I could do that!

For my info:

http://www.w3schools.com/sql/sql_update.asp

Could someone just check this to see if there would be a problem? I based it upon the add user script that was made.

function editParty(d,t,n) {
  // can get party details fed via function or 
  //use Data.Get to retrieve Program Settings
  var dat = (typeof d === 'undefined' || d=='' ? Data.Get("Partydate") : d);
  var tim = (typeof t === 'undefined' || t=='' ? Data.Get("Partytime") : t);
  var nam = (typeof n === 'undefined' || n=='' ? Data.Get("Partyname") : n);

  var grp = sql.Query(q).First;
  
{
		var editPartyAQ = dlg.AskQuestion('Would you like to change '+nam+' s Party time to '+tim+' and Date to '+dat+'?','OK,Cancel'); 
		
		if ( editPartyAQ == 'OK' ) {

			// run SQL to edit the party
			q = "UPDATE [Entities] ([Name]) VALUES ('" + dat + +tim + +nam+ "')";
			var r = sql.ExecSql(q);
		} else {
			dlg.AskQuestion('Changing '+nam+' Party Aborted','OK');
		}
	}
}

Stop. Do not execute that! That will not work properly. You will end up updating the name of every Entity in the entire DB. You need to constrain that SQL Statement to ensure you only update a single entity of the proper entity type.

The rest of the script is fairly “broken” as well.

1 Like

good job I asked first lol :smiley:

Ok, I can fix that using {ENTITY NAME:Birthday Party} and adding it to a {SETTING:beforehand}

thanks :smiley:

what about

q = "UPDATE [Entities] ([Name]) VALUES ('" + dat + +tim + +nam+ "') WHERE Name= '+old+' ";

This is why I was never cut out to do scripting lol, is it because the { brackets } are in the wrong places?

First, you need to get the Entity Type Id. Change the last part of this query to coincide with your setup.

var q = "SELECT [Id] FROM [EntityTypes] WHERE [Name] = 'BirthdayPartyEntityType'";
var entityTypeId = sql.Query(q).First;

You also need the Original Entity Name:

q = "SELECT [Id] FROM [Entities] WHERE [Name] = '" + oldName + "' AND [EntityTypeId] = " + entityTypeId;
var entityId = sql.Query(q).First;

That makes a big assumption that there will only be a single Entity with “oldName”, but the First parameter will select only a single Entity anyway.

Then the Update Statement becomes something like this:

q = "UPDATE [Entities] SET [Name] = '" + newName + "' WHERE [Id] = " + entityId + " AND [EntityTypeId] = " + entityTypeId;
2 Likes

Thanks @QMcKay ill do it again with the above and post when its changed.

Just had a rush of about 150 kids from the local private school lol

It really depends if its Primary Key or not. If its Primary then you could completely render it unusable and possibly crash SambaPOS if you change it and it conflicts.

in my head, I’m wondering wether to clone current ticket. set it to cancel the current party?

In beta right now there is GQL api available to check if Entity exists.

isEntityExists(type: String!name: String!): Boolean

There is legacy js api available in production version.

api.Entity(name).Exists();

Beta also has this available.

getEntity(type: String!name: String!): Entity

I am willing to bet eventually we will have update entity available.

1 Like

Not going to put a client in beta yet. But it does interest me.

Maybe ill just wait for now and he will have to cancel them individually if they change?