Search Results for 'script'
-
Search Results
-
Topic: Question on Note Field
I think I know the answer here, but thought I would ask. Is it possible to format data in a note field via code? Or when I create text and save it to a note field. Is it possible to send it tags that would format the data when creating via a script.
Thanks, rocky
A simple Calculation (number) field is warming my brain!
IF(Type = "Dividend";24;32)“Type” is the name of a Script field which contains “Dividend” or one of two other values.
24 and 32 are two arbitrary numbers to help me sort things out.With this Calculation, all the records have 24 in the Calculation field, no matter whether Type contains “Dividend” or one of the other values.
With
IF(Type = "";24;32), all the records still show 24.
WithIF(TaxYear = "1718";24;32), all the records in Tax Year 1718 show 24. The others show 32, as expected.I can’t see what I’m doing wrong when referencing the field “Type”.
In multi-column view with “Show sections” enabled, when my records are sorted on a Date field, the sections are set automatically to include the records for each month. Is there a way of changing that default section period to, say, a year or a day?
I have a Script field that holds a record’s applicable tax year in 4-digit format (e.g. 1819). When I sort on that field to get some totals for a tax year, the multi-column view shows no sections at all. This is all regardless of showing/hiding Group summaries, and showing/hiding the Calculations row. Nor do sections show up if I sort (e.g.) on account number
Topic: Rate Limiter Script
Based on a reply I added on the time delay post I wrote a while back, I decided to clean up and better document the script.
There are three functions:
- delay – simple spin lock delay mechanism for blocking execution for
durationmilliseconds. - rateLimitedDelay – ensure that a script blocks for at least
minTimemilliseconds since the last execution tracked bykey. - rateLimitedCallback – non-blocking method to execute
callbackno more frequently thanminTimemilliseconds since the last execution tracked bykey.
The first use case is pretty simple: call it and wait until the
durationexpires. The second one is for ensuring you don’t go through a code path too quickly but if you haven’t called it lately it will immediately call it. The third one is used to optionally executecallbackif you haven’t executed it in the lastminTimemilliseconds which is useful for avoiding spamming log messages or other similar events. It enables you to put in a callback but if it’s recently executed to immediately skip it (it’s not blocking). I built this to support a REST progress interface for ensuring it didn’t post updates more frequently than once every few seconds.This script uses a pattern I’m adopting of including a simple test case at the end. If you execute the script directly, it’ll run the test case as a sample of how to run. This means when you import you need to also define a variable called
PARENT_SCRIPTwhich is used to disable the test. Technically you can setPARENT_SCRIPTto any value but I use the formatFORM NAME::SCRIPT NAME:var PARENT_SCRIPT = 'Products::Update SKUs for Product'; document.getFormNamed('Script Manager').runScriptNamed('Rate Limiter');Test case:
console.log('Message 1 at ' + new Date()); rateLimitedDelay('test'); rateLimitedDelay('test'); rateLimitedCallback('callback', function() { console.log('Callback 1: ' + new Date()) }); console.log('Message 2 at ' + new Date()); delay(3000); rateLimitedCallback('callback', function() { console.log('Callback 2: ' + new Date()) }); rateLimitedDelay('test'); console.log('Message 3 at ' + new Date()); delay(6000); rateLimitedCallback('callback', function() { console.log('Callback 3: ' + new Date()) }); console.log('Message 4 at ' + new Date()); rateLimitedDelay('test'); console.log('Message 5 at ' + new Date()); rateLimitedCallback('callback', function() { console.log('Callback 4: ' + new Date()) });Here’s the full script:
// ========== Rate Limiter Start ========== // // NAME: Rate Limiter // VERSION: 1.0.0 // CHANGELOG: // 1.0.0: Initial release. /** * Rate Limiter module provides utilities to limit and delay * over time. */ if (typeof rateLimiter === 'undefined') { var rateLimiter = {}; /** * Spin lock delay mechanism. * * This will block execution until the time limit. * * @param {integer} duration - The length of the delay in milliseconds. */ function delay(duration) { let now = new Date(); let future = now.getTime() + duration; while((new Date()).getTime() < future) { } } /** * Blocking rate limited delay mechanism. * * This will block a request until a minimum time has been elapsed. * If based on the last execution of the `key`, `minTime` milliseconds * have not elapsed, this will block until that time has elapsed. * * `key` is shared with rateLimitedCallback. * * @param {string} key - The key to validate the last execution. * @param {integer} minTime - The minimum amount of time between execution. */ function rateLimitedDelay(key, minTime = 5000) { if (typeof rateLimiter[key] === 'undefined') { rateLimiter[key] = 0; } let now = new Date().getTime(); let nextExecution = rateLimiter[key] + minTime; if (now < nextExecution) { delay(nextExecution - now); } rateLimiter[key] = new Date().getTime(); } /** * Non-blocking rate limited callback executor. * * This will execute `callback` only if `callback` hasn't been * executed as `key` for at least `minTime` milliseconds since * the last execution. If it has been executed then it will not * execute this instance. * * `key` is shared with `rateLimitedDelay`. * * @param {string} key - The key to validate the last execution. * @param {function} callback - Callback to execute. * @param {integer} minTime - The minimum amount of time between executions. */ function rateLimitedCallback(key, callback, minTime = 5000) { if (typeof rateLimiter[key] === 'undefined') { rateLimiter[key] = 0; } let now = new Date().getTime(); let nextExecution = rateLimiter[key] + minTime; if (now > nextExecution) { callback(); rateLimiter[key] = new Date().getTime(); } } } // Tests if (typeof PARENT_SCRIPT === 'undefined') { console.log('Message 1 at ' + new Date()); rateLimitedDelay('test'); rateLimitedDelay('test'); rateLimitedCallback('callback', function() { console.log('Callback 1: ' + new Date()) }); console.log('Message 2 at ' + new Date()); delay(3000); rateLimitedCallback('callback', function() { console.log('Callback 2: ' + new Date()) }); rateLimitedDelay('test'); console.log('Message 3 at ' + new Date()); delay(6000); rateLimitedCallback('callback', function() { console.log('Callback 3: ' + new Date()) }); console.log('Message 4 at ' + new Date()); rateLimitedDelay('test'); console.log('Message 5 at ' + new Date()); rateLimitedCallback('callback', function() { console.log('Callback 4: ' + new Date()) }); } // ========== Rate Limiter End ========== //I have a checkbox field “Paid”, and a date field “Payment Date”, with their interactions controlled by two script fields. Checking or unchecking “Paid” enters the current date in “Payment Date”, or empties it. Manually entering a date in “Payment Date” checks “Paid”. Deleting the date unchecks “Paid”. Editing the date leaves it alone.
Everything works fine on their own form. Issues arise when I access them through a Link-to-Form field in another form. Some of the behaviour is consistent, some not. One particular oddity is this:
Accessing these records via the Link-to-Form field, editing the date field results in the checkbox being unchecked, which shouldn’t happen. But when I go to the native form I find that the checkbox is still checked. Back to the linked form – the box is certainly unchecked there.
So my investigation of how to get the scripting to work across forms is hampered by this anomaly. Is the box checked or not? I’d appreciate some advice on how to approach this problem.
Here are the scripts from the control fields, in case they shed any light on things:
//Responds to the "Paid" checkbox field being toggled, //by setting or emptying the Payment Date field var paid_id = 'fld-4f9f9ac525964be99ca478bdaee47144'; var paid = record.getFieldValue(paid_id); var thisDate = new Date(); // put the current date into <thisdate> if (paid) { // Use a special version of "setFieldValue" to prevent triggering the Toggle script record.setFieldValue('fld-cf95ec11043d4d2b8dc51d2ab93f177b', thisDate, false); } else { record.setFieldValue('fld-cf95ec11043d4d2b8dc51d2ab93f177b', "", false); }//Responds to the "Payment Date" field getting an entry or being emptied, //by toggling the "Paid" checkbox field var payment_date_id = 'fld-cf95ec11043d4d2b8dc51d2ab93f177b'; var payment_date = record.getFieldValue(payment_date_id); if (payment_date) { record.setFieldValue('fld-4f9f9ac525964be99ca478bdaee47144', "true", false); } else { record.setFieldValue('fld-4f9f9ac525964be99ca478bdaee47144', "false", false); }Any observations very welcome. And, just to note, clicking the refresh button seems to make no difference at any point.
- delay – simple spin lock delay mechanism for blocking execution for