Tap Forms app icon half
Tap Forms Forum text image
Blue gradient background

Exchange tips and ideas with the Tap Forms community

Search Results for 'script'

Viewing 15 results - 2,056 through 2,070 (of 2,952 total)
  • Author
    Search Results
  • Sam Moffatt
    Participant

    I’m working on a merge record script to be able to take two records and merge them together. This isn’t too bad for almost all of the field types except for the photo and file attachment types. How do I copy the attachments from one record to another for the file and photo fields?

    #39087
    Sonya Walters
    Participant

    Hi, its Sonya again,

    It worked! Because I only ever have one record in the PortfolioPaintings link to form field, I tried the non-Child Loop method:

            //-----------Pulls field from one-record linked form-----------
    function FramePriceFromPortfolioDB() {
    	
    	//Get id of record from current form
    	var portfoliopaintings = record.getFieldValue('fld-0b51acdae23a47789d7210379a6ddf2f');
    
    	//Using the id, pull the one record in the linked form and find a field in that form
            // (since there is only one record in the linked form, the index value of zero works).
    
    var frameprice = portfoliopaintings[0].getFieldValue('fld-c9071aa3421e4715a6bda880b63a32a3');
    
    	return frameprice;
    }
    FramePriceFromPortfolioDB();
    //.  --- [ end script to pull linked form field]

    I went on to the Child Loop method but I’m getting an error with the “length” variable…

    ScriptWithChildLoop: TypeError: undefined is not an object (evaluating 'portfoliopaintings.length'), column:48, line:12

    Here’s the script, sorry for all the code:

         //---- Child Loop method -----
         //Pulls the framecost field from linked portfoliopaintings form
    
    function recordsLoop() {
    
    	var portfoliopaintings_id = record.getFieldValue('fld-fdfa6263cd8c49ceb0bb048a841ab469');
    
    	var framecost_id = 'fld-c9071aa3421e4715a6bda880b63a32a3';
    	
    	var portfoliopaintings = record.getFieldValue(portfoliopaintings_id);
    
    	for (var index = 0, count = portfoliopaintings.length; index < count; index++){
    	
         	var framecost = portfoliopaintings[index].getFieldValue(framecost_id);
    		if (framecost) {
    
    			return framecost;
    		}
    	}
    	return;
    }
    
    recordsLoop();
    
    ---- [end code with Child Loop method] -----

    I think I’m all set since the first method works.
    This is a fantastic database and software, thank you for your support.

    Sonya

    #39080
    Sonya Walters
    Participant

    Hi, this is Sonya again, thank you for your suggestions on how to better document my questions.

    Here is my entire script (partially written by clicking on fields):

    function FramePriceFromPortfolioDB() {
    	
    	//Get id of same work in portfoliopaintings using the link
    
    	var portfoliopaintings = record.getFieldValue('fld-0b51acdae23a47789d7210379a6ddf2f');
    
    	//Find the Frame price of that record in portfolio paintings
    	
    	var framecost = portfoliopaintings[index].getFieldValue('fld-c9071aa3421e4715a6bda880b63a32a3');
    
    	return "framecost";
            //    return "3.00";  this was a test
    }

    The string fld-0b51acdae23a47789d7210379a6ddf2f (mmmm in previous post) is the Field ID of the Link to Form Field (used in the join).

    For index, I have tried inserting 16 (because the field I am looking to transfer is the sixteenth in the joined form Portfoliopaintings), as well as 2, and for kicks, 0.

    But I am not sure how to fill the index string. In case it helps I attached a screen shot of the fields in my form, including the linked form. It doesn’t show all the fields in the linked form, in the screenshot, but the one I want is number sixteen, a numeric value.

    I hope this makes sense, and.

    Thank you for your help!

    Attachments:
    You must be logged in to view attached files.
    #39079
    Brendan
    Keymaster

    Hi Sonya,

    I hope you don’t mind, but I put in a couple of tilde characters in your post to make your code more easily readable.

    is fld-mmmm the Field ID for your Link to Form field?

    And what is the value of index when you use it?

    Can you please post the entirety of your script so we can have a better look at it?

    Thanks,

    Brendan

    #39077
    Brendan
    Keymaster

    You can setup different sort orders using Saved Searches for the same form, even if your Saved Search has no Search Rules defined. Then you can ask a form for a specific search if you like:

    var search1 = form.getSearchNamed("Alpha Sort");

    var search2 = form.getSearchNamed("Number Sort");

    and so on.

    But the sorts have to be defined outside of your script. When you create a Saved Search and specify the sort order, the database engine I use (Couchbase Lite) creates an index table that contains references to the records sorted by the order you’ve specified. So this is why the Saved Searches need to be created beforehand.

    #39074
    Larry Stoter
    Participant

    Hi Brendan,

    Thanks – very helpful.

    With regard to sorting, I would like to change the records sort order within the script. Or, more specifically, I would like to have a conditional branch within the script which returns different sort orders depending on the result of the conditional test.

    I was thinking I could do this with form.getRecords() and then sort the array but there doesn’t seem to be an API function to write the sorted array back to the database. Instead, it looks as though this would need to be done with record.setFieldValues() for every record in the database …?

    I guess part of the problem I have is that I see records and fields as more fundamental than forms and that forms are just a way to enter and display fields values for each record? So, I’m used to setting up forms/layouts as the final step in putting a database together, after I’ve got everything else working.

    Perhaps the TapForms way to handle this is to set up several different forms, each with a different sort order and then use the scripts to move between forms ?

    Happy New Year,

    Larry

    #39072
    Sonya Walters
    Participant

    Hi,

    I’m a very inexperienced programmer trying to use the scripts function to draw data from one form to another from via a linked field.

    I would like to transfer the price of framing a work from one form (portfolio paintings) to another (Serendipity) so as to not have to enter the same data twice.

    I searched the forums andfound the information in this thread to be relevant and useful.
    However I have almost no experience in using programming languages so the line of code
    referenced in the thread above

    otherform[0].getFieldValue(‘fld-otherid’)

    does not have enough information for me to implement it. The field I would like to pull in, is field number sixteen (starting from zero) in the linked form.

    I find using “0” as the index does not return an error but “2”, “indexOf” and “16” all produce something like:

    Script: TypeError: undefined is not an object (evaluating ‘portfoliopaintings[2].getFieldValue’), column:39, line:9

    from the lines below:

    var framecost = portfoliopaintings[index].getFieldValue(‘fld-nnn’);
    // ^^
    // error appears at this spot

    function FramePriceFromPortfolioDB() {

    //Get id of same work in matching linked portfoliopaintings form:
    var portfoliopaintings = record.getFieldValue(‘fld-mmmm’);

    //Find the Frame price of that record in portfoliopaintings

    var framecost = portfoliopaintings[index].getFieldValue(‘fld-nnn’);
    // ^^
    // error appears at this spot

    return “framecost”;

    }

    FramePriceFromPortfolioDB();

    Does anyone understand this question enough to tell me how to refer to the field in the linked record, and return its value?

    Thank you for any help, and, happy new year!
    Sonya

    #39071
    Sonya Walters
    Participant

    Hi,

    I’m a very inexperienced programmer trying to use the scripts function to draw data from one form to another from via a linked field.

    I would like to transfer the price of framing a work from one form (portfolio paintings) to another (Serendipity) so as to not have to enter the same data twice.

    I searched the forums andfound the information in this thread to be relevant and useful.
    However I have almost no experience in using programming languages so the line of code
    referenced in the thread above

    otherform[0].getFieldValue('fld-otherid')

    does not have enough information for me to implement it. The field I would like to pull in, is field number sixteen (starting from zero) in the linked form.

    I find using “0” as the index does not return an error but “2”, “indexOf” and “16” all produce something like:

    Script: TypeError: undefined is not an object (evaluating ‘portfoliopaintings[2].getFieldValue’), column:39, line:9

    from the lines below:

    var framecost = portfoliopaintings[index].getFieldValue(‘fld-nnn’);
    // ^^
    // error appears at this spot

    function FramePriceFromPortfolioDB() {
    	
    	//Get id of same work in matching linked portfoliopaintings form:
    	var portfoliopaintings = record.getFieldValue('fld-mmmm');
    
    	//Find the Frame price of that record in portfoliopaintings
    	
    	var framecost = portfoliopaintings[index].getFieldValue('fld-nnn');
             //                                ^^
             //                      error appears at this spot
         
    	return "framecost";
    
    }
    
    FramePriceFromPortfolioDB();

    Does anyone understand this question enough to tell me how to refer to the field in the linked record, and return its value?

    Thank you for any help, and, happy new year!
    Sonya

    #39064
    Wayne Powell
    Participant

    Brilliant, thank you Sam! I totally forgot the IT Crowd Mantra = “Have you tried turning it off and on again?”

    Closing Tap Forms and restarting it fixed the issue, the script works again! Now I just have to fix my calculation values.

    #39056
    Brendan
    Keymaster

    Hi Larry,

    You can’t directly access the Go To functions in Tap Forms from the Scripting engine.

    But you can tell Tap Forms to select a record. However, it’s generally only used once in a script.

    But instead of having the UI select different records, you can just get the records directly from your script.

    Sorting is controlled by the Forms and Saved Searches. So whatever sort order you’ve specified on your Form or Saved Search is what will be used within the Script engine.

    If you want to find out what your previous record is, you need to first get the list of records. Then you’ll need to pick out which record you want.

    For example:

    function Previous_Record() {
    	
    	var records;
    	var previousRecord = record;
    
    	try {
    		if (search != undefined) {
    			records = search.getRecords();
    		} else {
    			records = form.getRecords();
    		}
    		
    		var indexOfCurrent = records.indexOf(record);
    		
    		if (indexOfCurrent > 0) {
    			previousRecord = records[indexOfCurrent - 1];
    		}
    	
    	} catch (e) {
    		// no search selected, so just don't do anything
    	}
    	
    	return previousRecord;
    }
    
    Previous_Record();

    I had to stick in the try/catch there because search generates a ReferenceError when you don’t have a Saved Search selected if you try to reference it.

    Anyway, so without having to manipulate the user interface, you can get the previous record using the above technique.

    #39053
    Sam Moffatt
    Participant

    I created a new form with the three fields, created a new script field, pasted your script in, changed the field ID’s around and it worked out of the box for me. Tested on TF 5.3.9 (Build 955) from MAS.

    The Can't find variable: record error I know happens with form scripts from time to time and if you’re working in the script editor I have seen it get disconnected from state though I’ve never been able to reproduce it consistently. The one I run into from time to time is that form isn’t a variable for my form script however closing the script editor and re-opening it seems to reconnect everything for me.

    If you’re still getting the hang of Javascript, Humble Bundle have a book bundle including “Javascript: The Good Parts” for their $1 tier. I came to JS a little earlier than the book, so I’ve never read it but I’ve had plenty of folk I trust who have read it that found it useful. It’s available as a part of the O’Reilly Classics bundle over at Humble Bundle. Humble do books in generally PDF, Mobi and ePub.

    #39052
    Wayne Powell
    Participant

    That error “Can’t find variable: record.” Occurs now whenever I make any Field Script field and then try to add a variable from the snippets.

    I’ve tested very simple scripts (like 1+1+1) and that works fine.
    Not sure if it matters, but I am experimenting with basic scripts using my iPad (Generation 6).

    Frustrating as I can’t move even an “inch” forward with this and not certain why. Lol

    #39051
    Wayne Powell
    Participant

    (I know what is wrong above, but can’t delete or edit my Post using Forum tools, so please ignore.). I’ve encountered a different scripting error which I’ve posted in the correct forum.

    #39050
    Wayne Powell
    Participant

    (I am moving this from another Forum as the question is more Script based.)

    My knowledge of JavaScript is VERY rudimentary although I am generally familiar with object oriented programming and can pick things up quickly with working examples.

    I first tried this without being in a function, and didn’t get an error, but then also do not get a result in the Script Field.

    SO I changed the script to below and now get an odd error like it can no longer find the very same field record.

    (If I “Paste” in from the snippet editor, I get the very same field id so that is not the error!?)

    function calculation_result () {
    var length = record.getFieldValue('fld-0f39aaf844314fde8b5af7adb5d3bd5f');
    var value_unit = record.getFieldValue('fld-616fa42329f347148fa7813db6a3289b');
    var result_unit = record.getFieldValue('fld-3148ad66cbb14d5b875ee8b26429cdec');
    
    var result = "";
    
    if (result_unit == "inches") {
       
    if (value_unit == "feet") {
    	result = length * 12;
    } else if (value_unit == "yard") {
       result = length * 36;
    } else if (value_unit == "cm") {
    	result = length * 2.54;
    } else if (value_unit == "metre") {
    	result = length * 0.0254;
    } else {
       result = length;
    } }
    
    if (result_unit == "feet") {
       
    if (value_unit == "inches") {
    	result = length / 12;
    } else if (value_unit == "yard") {
       result = length / 3;
    } else if (value_unit == "cm") {
    	result = length * 30.48;
    } else if (value_unit == "metre") {
    	result = length * 0.3048;
    } else {
       result = length;
    }
    }
    
    if (result_unit == "yard") {
       
    if (value_unit == "inches") {
    	result = length / 36;
    } else if (value_unit == "feet") {
       result = length / 3;
    } else if (value_unit == "cm") {
    	result = length * 0.0109361;
    } else if (value_unit == "metre") {
    	result = length * 1.09361;
    } else {
       result = length;
    }
    }
    
    if (result_unit == "cm") {
       
    if (value_unit == "inches") {
    	result = length * 2.54;
    } else if (value_unit == "feet") {
       result = length * 30.48;
    } else if (value_unit == "yard") {
    	result = length * 91.44;
    } else if (value_unit == "metre") {
    	result = length * 100;
    } else {
       result = length;
    }
    }
    
    if (result_unit == "metre") {
       
    if (value_unit == "inches") {
    	result = length / 39.37;
    } else if (value_unit == "feet") {
       result = length * 0.3048;
    } else if (value_unit == "cm") {
    	result = length / 100;
    } else if (value_unit == "yard") {
    	result = length * 0.9144;
    } else {
       result = length;
    }
    }
    
    return result;
    
    }
    
    calculation_result();

    This gives me the error:

    ReferenceError: Can’t find variable: record. Column:20, line:2

    I must be missing something very simple about field declarations that I can’t see.

    #39049
    Wayne Powell
    Participant

    OK, getting back to this one now that I have some time. I’ll search for an answer (as I am still again at very basic level of learning JavaScript), but my initial attempt returns an error (Return statements are only valid in Functions):

    var length = record.getFieldValue('fld-0f39aaf844314fde8b5af7adb5d3bd5f');
    var value_unit = record.getFieldValue('fld-616fa42329f347148fa7813db6a3289b');
    var result_unit = record.getFieldValue('fld-3148ad66cbb14d5b875ee8b26429cdec');

    var result = "";

    if (result_unit == "inches") {

    if (value_unit == "feet") {
    result = length * 12;
    } else if (value_unit == "yard") {
    result = length * 36;
    } else if (value_unit == "cm") {
    result = length * 2.54;
    } else if (value_unit == "metre") {
    result = length * 0.0254;
    } else {
    result = length;
    } }

    if (result_unit == "feet") {

    if (value_unit == "inches") {
    result = length / 12;
    } else if (value_unit == "yard") {
    result = length / 3;
    } else if (value_unit == "cm") {
    result = length * 30.48;
    } else if (value_unit == "metre") {
    result = length * 0.3048;
    } else {
    result = length;
    }
    }

    if (result_unit == "yard") {

    if (value_unit == "inches") {
    result = length / 36;
    } else if (value_unit == "feet") {
    result = length / 3;
    } else if (value_unit == "cm") {
    result = length * 0.0109361;
    } else if (value_unit == "metre") {
    result = length * 1.09361;
    } else {
    result = length;
    }
    }

    if (result_unit == "cm") {

    if (value_unit == "inches") {
    result = length * 2.54;
    } else if (value_unit == "feet") {
    result = length * 30.48;
    } else if (value_unit == "yard") {
    result = length * 91.44;
    } else if (value_unit == "metre") {
    result = length * 100;
    } else {
    result = length;
    }
    }

    if (result_unit == "metre") {

    if (value_unit == "inches") {
    result = length / 39.37;
    } else if (value_unit == "feet") {
    result = length * 0.3048;
    } else if (value_unit == "cm") {
    result = length / 100;
    } else if (value_unit == "yard") {
    result = length * 0.9144;
    } else {
    result = length;
    }
    }

    return result;

    So you can see I am missing the very rudimentary knowledge of how to output the result to a field. A simple example to guide me would likely suffice (which I will go looking for).

    A repository of complete example scripts and snippets would be great to be able to review (and for users to contribute to), but maybe I have missed such a thing (or is the forum intended that way).

    Thank you!

Viewing 15 results - 2,056 through 2,070 (of 2,952 total)