Would like to fetch Name from related record

Tap Forms Database Pro for Mac, iPhone, iPad and Apple Watch Forums Script Talk Would like to fetch Name from related record

Viewing 4 reply threads
  • Author
    Posts
  • January 28, 2025 at 5:22 AM #51582

    Josef Tingbratt
    Participant

    I have a basic CRM-setup. For my meeting notes I connect all of them to a customer (a related table). I would like to fetch the name from the related table (customer name) and put in to a text-field as a string. Been trying with CHatGPT with the following script but it doesn’t fill the text filed with the value:

    function calc() {
    // Fält-ID för det fält i relaterade tabellen som innehåller namn
    var namn_id = ‘fld-e59a27d956a442f7b1995c71bffdde1e’;

    // Fält-ID för fältet som länkar till relaterade poster
    var relaterade_falt_id = ‘fld-1234567890abcdef1234567890abcdef’;

    // Fält-ID för textfältet “Kundnamn”
    var kundnamn_id = ‘fld-9b111db729f343629bdc9823c7af9810’;

    // Hämta relaterade poster
    var relateradePoster = record.getFieldValue(relaterade_falt_id);

    // Kontrollera om det finns relaterade poster
    if (relateradePoster && relateradePoster.length > 0) {
    // Skapa en lista med namn från relaterade poster
    var namnLista = relateradePoster.map(function(poster) {
    return poster.getFieldValue(namn_id); // Hämta namn från varje relaterad post
    });

    // Kombinera namnen till en kommaseparerad lista
    var sammanstalltNamn = namnLista.join(‘, ‘);

    // Skriv värdet till fältet “Kundnamn”
    record.setFieldValue(kundnamn_id, sammanstalltNamn);

    } else {
    // Om inga relaterade poster finns, töm fältet “Kundnamn”
    record.setFieldValue(kundnamn_id, ”);
    }

    // Returnera ingenting (fokus är på att skriva till textfältet)
    return ”;
    }

    January 28, 2025 at 4:29 PM #51584

    Daniel Leu
    Participant

    It seems that the script is a bit more complicated than it needs to be. You only have one customer per note, don’t you? The script assumes several. I would remove the save field functionality and just use the return value. Set the return type to text.

    Just out of curiosity, I used ChatGPT to simplify your script and the result seems reasonable:

    function calc() {
        // Fält-ID för det fält i relaterade tabellen som innehåller namn
        var namn_id = 'fld-e59a27d956a442f7b1995c71bffdde1e';
    
        // Fält-ID för fältet som länkar till relaterade poster
        var relaterade_falt_id = 'fld-1234567890abcdef1234567890abcdef';
    
        // Hämta relaterade poster
        var relateradePoster = record.getFieldValue(relaterade_falt_id);
    
        // Kontrollera om det finns exakt en relaterad post
        if (relateradePoster && relateradePoster.length > 0) {
            // Hämta namn från den relaterade posten
            var namn = relateradePoster[0].getFieldValue(namn_id);
    
            // Returnera namnet
            return namn;
        } else {
            // Om inga relaterade poster finns, returnera en tom sträng
            return '';
        }
    }
    

    I think you could achieve the same result by using a calculation field. Select the field of your related form and set the return value to text.

    January 28, 2025 at 4:31 PM #51585

    Daniel Leu
    Participant

    Obviously, ChatGPT can translate as well. Makes it easier for me to read the comments :-)

    function calc() {
        // Field ID for the field in the related table that contains the name
        var namn_id = 'fld-e59a27d956a442f7b1995c71bffdde1e';
    
        // Field ID for the field that links to related records
        var relaterade_falt_id = 'fld-1234567890abcdef1234567890abcdef';
    
        // Retrieve related records
        var relateradePoster = record.getFieldValue(relaterade_falt_id);
    
        // If there is at least one related record, return the name from the first one
        if (relateradePoster && relateradePoster.length > 0) {
            return relateradePoster[0].getFieldValue(namn_id);
        }
    
        // If no related records exist, return an empty string
        return '';
    }
    
    January 29, 2025 at 2:24 AM #51589

    Josef Tingbratt
    Participant

    Thanks i did solve it oth help from ChatGPT, I just needed to point out the correct table to search in :)

    But is there a way to set the scripts to run automatically for each post?

    January 29, 2025 at 9:43 AM #51592

    Daniel Leu
    Participant

    But is there a way to set the scripts to run automatically for each post?

    Yes, you must use a field script instead of a form script. Create a new field of type script and assign the code you already have. This is why I recommended to use the return value instead of setting another field’s value. This makes it easier to understand what’s going on, specially if you have several scripts.

    • This reply was modified 3 months ago by Daniel Leu.
    • This reply was modified 3 months ago by Daniel Leu.
Viewing 4 reply threads

You must be logged in to reply to this topic.