Adding if statement to existing JavaScript code

Tap Forms – Organizer Database App for Mac, iPhone, and iPad Forums Using Tap Forms Adding if statement to existing JavaScript code

Viewing 3 reply threads
  • Author
    Posts
  • April 2, 2019 at 4:06 AM #34234

    Victor Warner
    Participant

    Brendan,

    I would be grateful for some help in adding the right JavaScript to existing code you provided.

    I have a several Forms in the same database. The two which are relevant:

    A. a Form to record details of documents (Document Form)
    B. a Form which lists the names of persons who will sign an individual document (Director Form)

    In the Document Form I have created a Link to Form from Document Form to Director Form (One to Many.)

    Some documents in the Document Form will be signed and some will not. If the document is signed then I will use Linked to Form field to choose a director from the Director Form.

    In the Document Form I have then created:

    (a). a further field which with a script (which you provided and I adapted) to extract certain detail from the Director Form (Script Field). See below for the code.

    (b). a further field (text) which states the number of directors signing (and is populated by a pick list (eg 1, 2, 3 etc)) (Directors Signing Field)

    For example:

    1. for one record in Document Form I may have a power of attorney which needs signing and I will add in the Linked to Form field a director (using the tick option, Select existing linked records)
    2. for another record in Dcoument Form I may have an official document which does not need signing, and I will not add a director.

    The issue is that in the Script Field is as follows:

    i. that if the document needs signing, and I will select the director and the Script Field will be populated as expected.

    but

    ii. that if I create a record in the Document Form where a document does not need signing, I will not add a director in the Linked to Form field, but the Script Field is still filled in (with details not necessarily related).

    Is there a way (or what is the code to add to the script) which tests that if the Directors Signing Field is not populated with anything then the Script Field is blank but otherwise the result variable from the script is used?

    I know how to create if fields just not with Javascript (despite the sample code you provide).

    I guess what I wish to achieve:

    If Directors Signing Field is empty then result
    otherwise make Script Field blank

    Existing code in Script Field

    // Get the ID of the Form - click on the Form heading to the left
    // Then click on "ID" button on left
    // And assign to variable "directors_id"
    
    var directors_id = 'fld-b97b1fadf82f42e08b2f8be84b5a0bb9';
    
    // set list of records to variable "directors_record
    
    var new_identified_work_record = record.getFieldValue(directors_id);
    // var directors_record = record.getFieldValue(directors_id);
    
    // Test if there is a record
    if (new_identified_work_record.length >0) { 
    
    //Select the first reocrd 
    var first_new_identified_work_record = new_identified_work_record[0];
    
    // Assign record to variable
    var identification_details_newly_identified_work_address_id = 'fld-cf17026d0c4a46c492195b21ad872a7e';
    
    // var full_address_id = 'fld-9b8f0ecdd8b3422bb1bcbd4b4a9a2077';
    
    // Assign value of reocords to a variable
    
    var address = first_new_identified_work_record.getFieldValue(identification_details_newly_identified_work_address_id);
    
    // Put value of record into a result
    
    result = address	
    
    }
    
    // print out value of result into field
    result;
    April 2, 2019 at 11:22 AM #34238

    Brendan
    Keymaster

    I think the code ou have is correct.

    To check to see if there are any entries in your new_identified_work_record array, checking its length > 0 is appropriate.

    Do you get any errors when you click the Run button in the Script editor?

    April 3, 2019 at 4:14 AM #34262

    Victor Warner
    Participant

    Brendan,

    Thank you for the response, but my query was directed to adding further code to the code I posted: a further if statement.

    Using the field names in my first post:
    1. The Script Field contains the code I posted and to which I add an if statement

    2. Directors Signing Field either nothing or a number (but being a text field)

    That is (using the field titles in my first post):

    If Directors Signing Field is empty then result (ie the result in the code posted)

    otherwise make Script Field blank

    April 4, 2019 at 6:48 AM #34281

    Victor Warner
    Participant

    I think I have solved the problem:

    I added near the top of the script:

    result = '':

    and the error message disappeared and the script runs properly (and the now the request for help with the if statement become superfluous (at least in my limited testing).

Viewing 3 reply threads

You must be logged in to reply to this topic.