Increase a number by a percentage

Viewing 1 reply thread
  • Author
    Posts
  • September 28, 2022 at 10:25 AM #48038

    Jake Gregory
    Participant

    Hi Everyone,

    First time posting here – very new user of Tap Forms.

    I’ve got a price list going on the Mac version, I would like to know if there’s a way I can run a script to increase the number in a number field by a percentage.

    I don’t even seem to be able to get of the ground so if someone could show a code example I would really appreciate it.

    Thanks

    September 28, 2022 at 11:10 AM #48040

    Brendan
    Keymaster

    Hi Jake,

    I replied to your email about this topic, but I’ll reply here too with the code I wrote for you in case anyone else needs this type of code:

    function Update_Prices() {
    
    	var price_field_id = 'fld-.....';
    	var records = form.getRecords();
    	var price_increase_percent = 0.05;
    	
    	for (var index = 0, count = records.length; index < count; index++){
    		var rec = records[index];
    		var current_price = rec.getFieldValue(price_field_id);
    		var new_price = current_price + current_price * price_increase_percent;
    		rec.setFieldValue(price_field_id, new_price);
    	}
    	form.saveAllChanges();
    
    }
    
    Update_Prices();
    September 28, 2022 at 12:48 PM #48041

    Jake Gregory
    Participant

    Thank you for your response Brendan, I decided to have a bash on my own – not much luck though lol

    However I have managed to rather clumsily get it working from the code you provided.


    function Update_Prices() {

    var group_1_id = 'fld-0305473a4c7e41f69ee491266c99ec03';
    var group_2_id = 'fld-93197576ce50421599626d9ccacff2f5';
    var group_3_id = 'fld-ec93413e3880425da973d35d93b99595';
    var group_4_id = 'fld-cd36a8501faf45408cd88e607e0bc4e1';

    var records = form.getRecords();
    var price_increase_percent = 0.12;

    for (var index = 0, count = records.length; index < count; index++){
    var rec = records[index];

    var g1_current_price = rec.getFieldValue(group_1_id);
    var g2_current_price = rec.getFieldValue(group_2_id);
    var g3_current_price = rec.getFieldValue(group_3_id);
    var g4_current_price = rec.getFieldValue(group_4_id);

    var g1_new_price = g1_current_price + g1_current_price * price_increase_percent;
    var g2_new_price = g2_current_price + g2_current_price * price_increase_percent;
    var g3_new_price = g3_current_price + g3_current_price * price_increase_percent;
    var g4_new_price = g4_current_price + g3_current_price * price_increase_percent;

    rec.setFieldValue(group_1_id, g1_new_price);
    rec.setFieldValue(group_2_id, g2_new_price);
    rec.setFieldValue(group_3_id, g3_new_price);
    rec.setFieldValue(group_4_id, g4_new_price);

    }
    form.saveAllChanges();

    }

    Update_Prices();

    I’m sure there’s a much better way to do this, but I’ll get there.

    I want to add a prompt box that asks for a user input for a percentage and then have a confirmation box ask them to confirm.

    I’m currently exploring the prompt options and I’m sure I’ll have more questions later lol
    Thanks again for your replies.

Viewing 1 reply thread

You must be logged in to reply to this topic.