Help with a script 3

Viewing 9 reply threads
  • Author
    Posts
  • April 30, 2022 at 9:17 AM #47201

    Stig Widell
    Participant

    Hi,
    The Swedish farmer now has an excellent tool with Tap Forms 5 in organizing cows and calves. Two script are involved created by Brendan and Sam, thanks to both of them.
    However there is a need for one more script.
    The birth date of a calf in form “Kalvningar” is equal to the delivery date for the mother cow. In the form “Rekryteringsdjur” there is a list of all deliveries for a cow.
    Now there is a need for a field in “Rekryteringsdjur” identifying the date of the cows latest delivery.
    I hope someone can help me!!
    Enclose the template.

    Attachments:
    You must be logged in to view attached files.
    May 11, 2022 at 2:42 AM #47274

    Stig Widell
    Participant

    HELP!
    Is there anyone who could help a farmer with no knowledge in scripts?

    May 11, 2022 at 11:39 PM #47283

    Brendan
    Keymaster

    Hi Stig,

    I’m sorry but I haven’t had any spare time to work on your scripts for you at this time. I’ve been too busy working on the next version of Tap Forms. And I’m sure that Sam has been pretty busy lately too.

    You’ve said that you want to get the maximum birth date value. But I don’t know which field represents the birth date in which form. It’s all in Swedish and I just don’t know which field you need to birth date on and I don’t know which form you want it displayed in.

    Maybe I can help if you can translate your form to English for me and re-upload it here. It would certainly make it easier for me to write a script for you.

    But if it starts to get complicated with lots of back-and-forth messaging, then I’ll have to bow out for now as I really need to get back to working on the next Tap Forms version.

    Thanks,

    Brendan

    May 12, 2022 at 12:39 PM #47285

    Stig Widell
    Participant

    Hi Brendan,
    Thank you so much for responding.
    I believe the easiest way to describe what I need is to send you a print screen with comments (enclosed).
    Regards
    Stig

    Attachments:
    You must be logged in to view attached files.
    May 13, 2022 at 12:42 AM #47288

    Brendan
    Keymaster

    Ok, so you need two Field Scripts. One to get the maximum birthdate value. The other to get the Djur-ID of the calf with the maximum birthdate value.

    Here’s the first one:

    function MaxBirthDate() {
    
    	var kalvningar_id = 'fld-9107a2de427749e18f024644fd3cc1ad';
    	var födelse_id = 'fld-8e0cd05ffe8d4603a8abb8ca4694b4ee';
    	var djur_field_id = 'fld-1bb0eb29c4774502bf91c318093ae4f6';
    
    	var kalvningar = record.getFieldValue(kalvningar_id);
    	
    	var maxBirthDate;
    	
    	for (var index = 0, count = kalvningar.length; index < count; index++){
         	var födelse = kalvningar[index].getFieldValue(födelse_id);
    
    		if (födelse) {
    			if (maxBirthDate == undefined || födelse.getTime() > maxBirthDate.getTime()) {
    				maxBirthDate = födelse;
    			}
    		}
    	}
    	return maxBirthDate;
    }
    
    MaxBirthDate();

    Set the Result type of the above Field Script to Date.

    function MaxBirthDateDjurID() {
    
    	var kalvningar_id = 'fld-9107a2de427749e18f024644fd3cc1ad';
    	var födelse_id = 'fld-8e0cd05ffe8d4603a8abb8ca4694b4ee';
    	var djur_field_id = 'fld-1bb0eb29c4774502bf91c318093ae4f6';
    
    	var kalvningar = record.getFieldValue(kalvningar_id);
    	var calves_id;
    	
    	var maxBirthDate;
    	
    	for (var index = 0, count = kalvningar.length; index < count; index++){
         	var födelse = kalvningar[index].getFieldValue(födelse_id);
         	var djur_id = kalvningar[index].getFieldValue(djur_field_id);
    
    		if (födelse) {
    			if (maxBirthDate == undefined || födelse.getTime() > maxBirthDate.getTime()) {
    				maxBirthDate = födelse;
    				calves_id = djur_id;
    			}
    		}
    	}
    	return calves_id;
    }
    
    MaxBirthDateDjurID();

    Set the Result Type of the above Field Script to Number.

    That should do it.

    Thanks,

    Brendan

    • This reply was modified 1 year, 11 months ago by Brendan.
    May 13, 2022 at 12:44 AM #47290

    Brendan
    Keymaster

    They’re basically the same scripts, just one returns the Djur-ID and the other returns the Birth Date. You can only return a single value at a time, so two scripts are needed. One for each field.

    May 14, 2022 at 12:40 PM #47315

    Stig Widell
    Participant

    Thank you Brendan for your efforts.
    Unfortunately I have to bother you once more because nothing shows up in the added script fields.
    Regards
    Stig

    Attachments:
    You must be logged in to view attached files.
    May 14, 2022 at 1:26 PM #47317

    Daniel Leu
    Participant

    Hi Stig, instead of sharing a TapForms template, could you share a TapForms archive of a document that contains entries for just a few animals? I tried to see what the script does, but it returns an error. I don’t know if I missed to enter some data or if there is an issue with the script. Having a TapForms archive with known data would make debugging much easier. Thanks!

    Cheers

    May 14, 2022 at 10:38 PM #47326

    Brendan
    Keymaster

    Oh, it’s just a matter of the wrong field IDs in the script. I guess I used an archive of the form you sent me a while ago to write the script. You also renamed the Kalvningar field to Antal kalvningar so that took me a minute to find it again.

    var kalvningar_id = 'fld-cdb4431a905f4b1dbaa750b07a63b6f1';

    When you see an error in the console, first check to make sure all the field IDs in the code match the field IDs on the left. You can select a field and see its ID below.

    May 15, 2022 at 2:37 AM #47327

    Stig Widell
    Participant

    Thanks Brendan now it works perfectly!

Viewing 9 reply threads

You must be logged in to reply to this topic.