Running a script on a search selection

Viewing 5 reply threads
  • Author
    Posts
  • October 20, 2019 at 9:36 AM #37267

    john cesta
    Participant

    Is there a way to run a script on just a search selection of records?

    Thanks

    October 20, 2019 at 2:38 PM #37271

    Daniel Leu
    Participant

    Yes. If the search is already active you can use var records = search.getRecords();. If you want to run a specific search from your script, use var records = form.getSearchNamed('search name').getRecords();.

    October 21, 2019 at 9:21 AM #37277

    john cesta
    Participant

    In other words I want to run this script which totals the volume of only the selected records as in the image. But totals all the records in the form.

    function Volumecontest() {

    // Replace with your own code

    var volume_id = ‘fld-9b16cf5c9ee4420c83274f23ba59e554’;
    var total = form.getTotalOfField(volume_id);

    return total;

    }

    Volumecontest();

    Attachments:
    You must be logged in to view attached files.
    October 21, 2019 at 10:16 AM #37279

    Sam Moffatt
    Participant

    Try something like this, it will only work if you’ve got a saved search active:

    function Volumecontest() {
      // Replace with your own code
      var volume_id = ‘fld-9b16cf5c9ee4420c83274f23ba59e554’;
      var total = search.getTotalOfField(volume_id);
      return total;
    }
    
    October 21, 2019 at 12:04 PM #37280

    john cesta
    Participant

    OK I’m doing my best. I use your code Sam and it resolves but I don’t get a result anywhere.

    Here’s what I’m trying to do….
    I can win an apple watch if I write more than 3,000,000 of insurance volume before the end of the year,

    So I search for the policies I wrote between October and Dec 31 And have a total of volume.

    Now I want to, in each record, count down from the 3,000,000 to see where I’m at.

    So I reckon I need a form search to gather all the records that count and a record script to store the total of the search records in order to use the total to subtract from the 3,000,000

    October 21, 2019 at 7:15 PM #37289

    Sam Moffatt
    Participant

    Ok so essentially you’re wanting to treat this like a bank account where each record is a line item that has a transaction value and then updates the balance from a starting point.

    Since you want to do per record storing that’s a little more nuanced, I’d actually go with a form script. I’m assuming you’re sorting records by something like date and what ever you’re using needs to be consistent. I’m going to assume this is ordered in a way that makes sense.

    Something like this should do the trick:

    function Calc_Balance() {
    	let balance = 3000000;
    	var amount_id = 'fld-454d9fcbfabd4e3fb89823668e042079';
    	var balance_id = 'fld-69d8d05d722f4dda9691dd687ed3b96c';
    	var transaction_date_id = 'fld-0ce56448807849699dfb2a22bb984774';
    	
    	for(record of form.getSearchNamed('Transaction Date: 1/10/19').getRecords())
    	{
    		balance -= record.getFieldValue(amount_id);
    		record.setFieldValue(balance_id, balance);
    	}
    	document.saveAllChanges();
    }
    
    Calc_Balance();
    

    The script is a little brute force which means it’ll rewrite everything but seems to work.

    Attaching a form template that has this script and an autogenerate script in it to generate sample data quickly.

    Attachments:
    You must be logged in to view attached files.
Viewing 5 reply threads

You must be logged in to reply to this topic.