How to resolved reference item field value in Rule?

Hi @Sebastian ,

I want to push the referenced item field value to elastic search using the rules through the scripts. I found **getReference(id, callback) ** method mentioned in docs.

Let me know Is it possible using above method then how ? or any other way to achieve this.

Regards,
Rakesh Kanani

What do you use to format your rule? Scripts or Liquid?

Hi @Sebastian,

I am using ‘Scripts’ format for rule.

You have to do somehing like this

getReference(event.data.author.iv[0], function (references) {
	var author = references[0];
	
	var jsonObject = {
		title: event.data.title.iv,
		text: event.data.text.iv,
		authorName: author.data.name.iv
	};
	
	var json = JSON.stringify(jsonObject);
	
	complete(json);
})

Btw: You can now make the changes in the rule and run the simulator without saving it. So you can play around with that.

See: Asset Id to URL in webhook

1 Like

Thanks @Sebastian.

This is working fine.

I have a 1: M relationship in one of the table, then how can we loop in the “getReferences” method.

Just loop over them

getReferences(event.data.author.iv, function (references) {
	var jsonObject = {
		title: event.data.title.iv,
		text: event.data.text.iv,
		authors: []
	};
	
	for (var author of references) {
		jsonObject.authors.push({
			name: author.data.name.iv
		});
	}
	
	var json = JSON.stringify(jsonObject);
	
	complete(json);
})
1 Like

Thank you so much @Sebastian, for your prompt response! I appreciate you getting this information to me so quickly. :+1:

1 Like