Doing this backwards, don’t mind me…
the Father-ID in the calf record should remain the same even when the mother, the year after, will be linked to another pedigree bull
Right, the calf record should remain unchanged though presumably at some point it either is sold or grows up into one of the other forms. Which actually makes me wonder if ideally everything would be one form with relevant sections but I don’t think TF does self joins well (data model quirk).
How do you mean the calf record should be created? I dont understand that? I dont have all information already before the time of birth of the calf, eg date of birth, sex, stillborn etc.
Well you know what you know about the calf to be, you’re just entering it in immediately once you know some of the details just not all of the details. For me personally I find pre-entering partial data slightly easier because then it’s just a matter of inputting the final data once that comes in. Presumably you know which of your cows are calving so that can be a filter there to limit the possible candidates and you could also sort/group by the pasture they’re in.
“Avelstjur” shouldnt be a text field but a number, my mistake.
Yes, if I change that field to a join field I get the pedigree bulls info over to the cows record as a list with one row with all the info of that bull. But I cant get the ID of the bull over to the father-ID field in the record of the newborn calf when I link that calf record to the mother record. How do I manage that? What do you mean with “creative” join field?
Type is mostly inconsequential for the simpler data fields (text/number) and more necessary for formatting than anything. That said it might mess up some comparisons so always useful to keep it consistent.
Workflow wise I think I’d create the calf record first and then just link in the mother and father records from the calf directly. Or create the calf from the mother record and then just link it to the father at that point in time. That’d obviate the need to script because you could just use the UI to select.
I’m going to use one of my scripts to get the ID, easiest way to do this is to import the Script Manager and in the “Repository” form run the “Update Scripts” form script.
Inside “Rekryteringsdjur” I created a new form script called “Kalva” and then this is it’s implementation:
let PARENT_SCRIPT = "Kalva";
document.getFormNamed("Script Manager").runScriptNamed("getRecordFromFormWithKey");
function Kalva() {
let rekryteringsdjur_kalvningar_id = 'fld-cdb4431a905f4b1dbaa750b07a63b6f1'; // ID of link to form field to Kalvningar form in the Rekryteringsdjur form.
let avelstjur_kalvningar_id = 'fld-35f714dc0468447c9b5b9875c8a86545'; // ID of link to form field to Kalvningar form in the Avelstjur form.
let foldse_id = 'fld-8e0cd05ffe8d4603a8abb8ca4694b4ee'; // foldse field ID from Kalvningar form.
let keyFieldId = "fld-0e776009803a4c1f8415e7cbb7919858"; // ID of the Djur-ID field in the Avelstjurar form.
let keyValue = record.getFieldValue('fld-db52d7055ddd40cf95f50bcf8e2fff3d'); // ID of the Avelstjur field in Rekryteringsdjur form.
// check if the bull is even set or error.
if (!keyValue) {
Utils.alertWithMessage("Fel!", "Avelstjur värde ej inställt!");
return;
}
// look for matching bull ID record; note this assumes the bull ID is unique.
let avelstjurRecord = getRecordFromFormWithKey("Avelstjurar", keyFieldId, keyValue)
if (!avelstjurRecord) {
Utils.alertWithMessage("Varning", "Det gick inte att hitta posten!");
return;
}
// Create a new calf record from the rekryteringsdjur form.
let kalvningarRecord = record.addNewRecordToField(rekryteringsdjur_kalvningar_id);
// set foaling date to current date.
kalvningarRecord.setFieldValue(foldse_id, new Date());
document.saveAllChanges(); // flush out model changes for new record creation.
// add the calf to the bull's record ID.
avelstjurRecord.addRecordToField(kalvningarRecord, avelstjur_kalvningar_id);
document.saveAllChanges(); // save adding calf to bull
Utils.alertWithMessage("Gjord!", "");
}
Kalva();
The first two lines setup a variable some of my scripts use to identify themselves and import the getRecordFromFormWithKey function. If PARENT_SCRIPT isn’t set, some of the child scripts run self tests on import.
The Kalva function is there to create the calf, the first set of lines set up the various field ID’s we’re going to need later and then grabs the Avelstjur value for the current record. If that isn’t set we have an error message and return. It then looks for a match of that ID in the Avelstjurar form and if it doesn’t find it, also returns out. I did fix the type to match as number with the same formatting everywhere because I think initially it didn’t match properly due to a type mismatch (Javascript is a little more picky than TF).
Once we’ve got the bull, we’ve started already with the mother, so we create a new calf from the mother (record.addNewRecordToField). This will automatically set the link from form field to this record. I also set Födelse to the current date, you could also default this in your form settings but this is an example of how you can set some fields in that child record. There is a document.saveAllChanges() because when you create new records, sometimes the scripting system can get out of sync so I find it easier to force a save here.
The last step is to add the calf to the bull and then save that change.
That should do the trick for what you’re after.
Did you use the tff file or the tfarc file? what is the difference?
TFF is a form template, it just has the definition of the form and linked forms. TFARC is an archive that can also include records. I did the TFARC first then noticed I was missing a form and used the TFF.
-
This reply was modified 4 years, 6 months ago by
Sam Moffatt. Reason: fixing blockquote tags