Scripting – get value from other form

Tagged: 

Viewing 5 reply threads
  • Author
    Posts
  • May 23, 2019 at 12:35 AM #34933

    Kana Ne
    Participant

    Is it possible to get the value from a field on an other form in a script?
    I can’t see the fields from another form in the script window of this form.

    Thanks,
    Kana

    May 23, 2019 at 12:52 PM #34938

    Brendan
    Keymaster

    Hi Kana,

    Yes you can do this. But how you get access to the field depends on how you’ve got your relationships setup or if any relationship is being used at all.

    If you have a Link to Form field or a Link From Form field, the fields from those forms will show up automatically in the Fields list. So then it’s just a matter of double-clicking on the field to get a reference to it.

    But if you just want to get a field from another form when you use the document.getFormNamed() function, then you can use the var field = form.getFieldNamed('field name'); function to get access to that field. Your field names should be unique of course since Tap Forms will give you back the first field it finds with the specified name. Once you get the field you can ask it for it’s ID using field.getID();. Now you can use that ID in other functions that require a field ID.

    Hope that helps!

    Brendan

    May 25, 2019 at 5:06 AM #34943

    Anonymous
    Inactive

    Can you elaborate on this? I’m trying to fetch values from one form into another, I’ve linked them and when I try and use:

    var customers = record.getFieldValue('fld-a538b0e58afa4d5db40dd06a19af2436');
    var name = customers[index].getFieldValue('fld-c4e6710f99544905b9cbbf1fcbd8f613');
    name;

    It gives me a ReferenceError: Can’t find variable; index. Whatever I try it doesn’t work except when it’s referencing a field within the same form.

    If its possible to reference to a field in another form without linking the form it would be even better as I don’t need that function

    May 25, 2019 at 11:49 AM #34947

    Sam Moffatt
    Participant

    You need to do a loop for that:

    
    var customers = record.getFieldValue('fld-a538b0e58afa4d5db40dd06a19af2436');
    for (var index = 0, count = customers.length; index < count; index++)
    {
      var name = customers[index].getFieldValue('fld-c4e6710f99544905b9cbbf1fcbd8f613');
    name;
    }
    

    That will overwrite if you have more than one field linked.

    The Script Editor will only display the fields from linked forms however you can copy and paste the field ID’s and keep track of them manually. Linking makes it a little easier but the ID’s in the script itself is unrelated to any links. You can use documents.getFormNamed('Customers').getRecords() to get all of the records in that form instead of using record.getFieldValue.

    Do you mind sharing a little about your use case and the problem you’re trying to solve?

    May 25, 2019 at 5:59 PM #34950

    Anonymous
    Inactive

    Hi Sam, thanks for the info i’ll try it soon.

    I’ve got a Numbers sheet with customer data comprised of names, mail, website, where it hosted, prices etc.

    Got most customer fields already in 1 form and prices etc are calculated correctly through scripts. Now I want to make an overview form which show me total prices, average monthly income / expenses, how many websites are hosted with a specific holster etc.

    As an example I asked the question because I want to have an overview how many sites are hosted with hosting provider x, y and z. Can’t do that with an calculation (Numbers got an COUNTIFS, tapforms not) and calculations are not updating in my case so I’m using the script function (also gives me more advanced features and also easier for me to understand as a coder)

    May 27, 2019 at 2:31 AM #34952

    Sam Moffatt
    Participant

    A few thoughts, not sure if these will help solve your problems. I’d start normalising your data a little because that will help you aggregate on those axis (e.g. https://en.wikipedia.org/wiki/Database_normalization).

    The first step I’d take is to create a form with hosting provider in it. Each hosting provider is a record in this new form. Then you can use a Link To Form field as either a 1:M or JOIN type to go from the hosting provider to the customer (1:M assuming each website/customer is only hosted at one place, you could do M:M if that isn’t true; if you have a “key” column for the hosting provider in place you can use this for the “JOIN” field, a text field with a controlled value is enough).

    Doing this will force you to create a record per hosting provider however the Link To Form inside of it will give you quick aggregations on the numeric fields. See a screenshot of roughly what I mean. This might not be everything you need but you can use scripting to create a text entry with the data you want for that hosting provider. At the very least this makes filtering by hosting provider relatively trivial and building JavaScript aggregations a little easier. It is a little more work to get set up but you should be able to experiment on a backup to see what I mean.

    If you go for the JOIN option, you should be able to do this by creating the hosting entries you need. You could automate that but that might be more hassle than it’s worth unless you have a lot of hosting providers. If you go for the 1:M or M:M options, then you need to link all of the records together by hand or again write a script to do it for you. Either of those choices will make sense depending on the volume of data you have and your own inclinations. As always before doing any of these tasks: make a backup first, restore it to a new document and disable any sync you have enabled to prevent changes from propagating. That way if you make a mistake, you can be confident you didn’t toast your only copy.

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

You must be logged in to reply to this topic.