Counter field?

Viewing 2 reply threads
  • Author
    Posts
  • February 2, 2020 at 8:41 PM #39436

    al m
    Participant

    Is there a way to create a counter field eg 300 the next record 301 then 302 etc and not to allow modification of this field?

    February 2, 2020 at 9:34 PM #39438

    Sam Moffatt
    Participant

    You can use a script field to get something like this. Here’s something that gives you a counter within the form. I created a new “Counter” script field and put the value in it:

    function Counter() {
    	//var title = record.getFieldValue('fld-a0c07157d30f400d96b5ff2080574896');
    
    	let counterFieldId = form.getFieldNamed('Counter').getId();
    	let currentValue = Number(record.getFieldValue(counterFieldId));
    	
    	if (currentValue)
    	{
    		console.log(`Using current value: ${currentValue}`);
    		return currentValue;
    	}
    	
    	let maxValue = form.getMaxOfField(counterFieldId);
    	if (maxValue)
    	{
    		let retval = maxValue + 1;
    		console.log(`Using value from search: ${retval}`);
    		return retval;
    	}
    	
    	console.log('Defaulting to a single value.');
    	return 1;
    }
    
    Counter();
    

    You’ll notice a line near the top that is var title = record.getFieldValue but commented out. I did this to make sure that the script triggers on a field. You can double click on a field from your form in the script editor to get a similar effect.

    February 2, 2020 at 10:22 PM #39440

    Brendan
    Keymaster

    Hi Al,

    Perhaps you’re thinking of the Auto-Increment setting on the Number field?

    If you turn that on and create a new record, Tap Forms will increment the value for that field. The field will not be editable. You can also set what the next default value will be when new records are created. And you can also specify the increment amount, that is, for every new record, how much will be added to the next default value for the new record.

    Thanks,

    Brendan

Viewing 2 reply threads

You must be logged in to reply to this topic.