Method getReference example

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).

I was trying this in Create of Schema 2:

getReference(ctx.data.reference.iv[0], callbacktest);

function callbacktest(valuecontent){
ctx.data.codification.iv = “H-” + valuecontent[0].data.code.iv;
}

But I get the message “Failed to create content. Please reload”.

Do you have any examples of how to use this methods?

Thanks!

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.

Hi,

So I got kind of the same problem…

This is what I have :

Animator :

  • firstName : String
  • LastName : String
  • Salary : int

Actions :

  • name : String
    -animators (List of Animator)
    • anims : id (reference to Animator id)
    • hoursWork : int
    • travelTime : int
    • totale : int

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…

1 Like

First of all you have to fix your loops,

it is either

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();
});
2 Likes

Thank you it worked!

1 Like

Lol, now I am surprised. Have not expected to get it right :wink:

1 Like