Hi,
I was reading the scripting documentation and I wonder how the methods getReferences(ids, callback) and getReference(id, callback) work.
This is what I have:
Schema 1:
code (int)
name (string)
Schema 2:
name (string)
reference (Schema 1)
codification (string)
I would like to autogenerate the field codification with a string like “H-XXX”, where XXX should be the value of the field code of the reference (Schema 1).
Please use code formatting for code and use the template for new support requests. I will help you then, but I need answers to the questions that you will see when you select the “Support” category.
I would like to autocalculate the totale per animators (which is salary * (hoursWork + travelTime)) where salary should be the value of the field salary from Animator.
So I created an array containing all the animators ids in order to get all the animator from the schema Animator. Here’s an exemple :
let ids = [];
for(var i in ctx.data.animateurs.iv){
ids.push(ctx.data.animateurs.iv[i].anims);
}
getReferences(ids, function(reference) {
for(var i in ids){
ctx.data.animateurs.iv[i].totalemp = reference[i].data.salaire * (ctx.data.animateurs.iv[i].tempDeplacement + ctx.data.animateurs.iv[i].travail);
}
replace();
});
But I noticed that my reference is empty so I’m a bit lost…
for (let i = 0; i < array.length; i++) {
let item = array[i];
}
or
for (let item in array) {
}
So this should work.
let ids = [];
for (let animateur in ctx.data.animateurs.iv){
ids.push(animateur.anims[0]); // References are always array.
}
// or just
// const ids = ctx.data.animateurs.iv.map(a => a.anims[0]);
// References is an array of references
getReferences(ids, function(references) {
for (const reference of references) {
// Find the corresponding animateur.
const animateur = ctx.data.animateurs.iv.find(x => x.anims[0] === reference.id);
if (animateur) {
animateur.totalemp = reference.data.salaire.iv * (animateur.tempDeplacement + animateur.travail);
}
}
// I think the change is only detected when you set a field value.
ctx.data.animateurs.iv = ctx.data.animateurs.iv;
replace();
});