Aaaaaah What am I doing wrong???

Hello all.

I have a method which used to work perfectly. I cannot fathom now why it doesn’t. I think that a servoy update has possibly deprecated something, but I cannot find what it is.

I cannot debug it because debug does not work if there are rtf fields in a table view - which I have.

My method is supposed to add 10 to estimate_item_id (a number) depending on what the highest number is. Simple.

I have tried estimate_item_id = vthehighestvaluenumber + 10 as
estimate_item_id = utils.stringToNumber(vthehighestvalue + 10) but the result (if the highest value is 60) is 10.16 - I don’t understand what it is doing.

Very annoying as this used to work perfectly. Now it just gives every line the number 10.

My method is:

function new_line_using_group()
{
var sql_query = "SELECT MAX(estimate_item_id) FROM line_items WHERE line_items.sales_id = " + forms.sale_master.salesid + " AND line_items.main_group = " + "'" + globals.g_main_group + "'" + " AND line_items.hidden_from_estimate != 1"
	var vthehighestvalue = databaseManager.getDataSetByQuery(controller.getServerName(), sql_query, null, 1);
	var vthehighestvaluenumber = utils.stringToNumber(vthehighestvalue)
	
	if (globals.g_main_group == 'new group')
			{
			var vmaingroup = plugins.dialogs.showInputDialog('Create a group', 'Please create a group for this item (eg. Master bedroom, Delivery lines etc.)','type here')
				controller.newRecord(false,true)
				main_group = vmaingroup
				sales_id = forms.sale_master.salesid
				contacts_id = forms.sale_master.contacts_id
				estimate_item_id = 10

			databaseManager.saveData()
			elements.description.requestFocus()
			}
	else if (globals.g_main_group == 'all groups')
			{
			var thePressedButton = plugins.dialogs.showErrorDialog('No group', 'Please create or choose a group before adding a line','OK');
			return
			}
	else 

			{
			controller.newRecord(false,true)
			main_group = globals.g_main_group
			sales_id = forms.sale_master.salesid		
			contacts_id = forms.sale_master.contacts_id
			if (vthehighestvalue)
				{				
				estimate_item_id = vthehighestvaluenumber + 10
				}
			
			else
				{
				estimate_item_id = 10
				}
			

				databaseManager.saveData()
			elements.description.requestFocus()
			}
	//open the item editor immediately
	edit_item();

}

Any help would be appreciated

Bevil

Hi Bevil,

The one issue I see is that you are trying to cast a DataSet into a Number. Not sure if that works :)
Also use prepared statements. It’s more secure and if you fire this query a lot it will be faster too (database server only have to plan it once).
So if you change these 2 things your code looks like this:

function new_line_using_group() {
    var sql_query = "SELECT MAX(estimate_item_id) FROM line_items WHERE line_items.sales_id = ? AND line_items.main_group = ? AND line_items.hidden_from_estimate != 1";
    var vDataSet = databaseManager.getDataSetByQuery(controller.getServerName(), sql_query, [forms.sale_master.salesid,globals.g_main_group], 1);
    var vthehighestvaluenumber = vDataSet.getValue(1,1);
   
    if ( globals.g_main_group == 'new group' ) {
        var vmaingroup = plugins.dialogs.showInputDialog('Create a group', 'Please create a group for this item (eg. Master bedroom, Delivery lines etc.)','type here');
        controller.newRecord(false,true);
        main_group = vmaingroup;
        sales_id = forms.sale_master.salesid;
        contacts_id = forms.sale_master.contacts_id;
        estimate_item_id = 10;
        databaseManager.saveData();
        elements.description.requestFocus();
    } else if ( globals.g_main_group == 'all groups' ) {
        var thePressedButton = plugins.dialogs.showErrorDialog('No group', 'Please create or choose a group before adding a line','OK');
        return;
    } else {
        controller.newRecord(false,true);
        main_group = globals.g_main_group;
        sales_id = forms.sale_master.salesid;
        contacts_id = forms.sale_master.contacts_id;
        if ( vthehighestvaluenumber ) {            
            estimate_item_id = vthehighestvaluenumber + 10;
        } else {
            estimate_item_id = 10;
        }
        databaseManager.saveData();
        elements.description.requestFocus();
        
    }
    //open the item editor immediately
    edit_item();
}

And I guess you should also check if the dataset has an exception.

Hope this helps.

Hi Robert

It is very odd that my method used to work fine. I changed nothing and it stopped working (I think during the update to 4.1.4)

Your fix is brilliant. It works perfectly. Can you tell me what it does? What is a prepared statement???

:)

Bevil

Thank you Robert. I have looked it up. Now learning about prepared statements. :)