Triggers: Difference between revisions

57 bytes removed ,  7 October 2016
m
no edit summary
mNo edit summary
mNo edit summary
Line 336: Line 336:
The user will enter the value in PriceUSD and the value in Euros will appear in PriceEUR according to the current exchange rate.
The user will enter the value in PriceUSD and the value in Euros will appear in PriceEUR according to the current exchange rate.


We will create a Trigger on Event '''Create entry to Add''' and phase '''After saving the entry'''.
We will create a Trigger on Event '''Create an entry to Add''' and Phase '''After saving the entry'''.


<source lang="javascript">
<source lang="javascript">
result = http().get("http://api.fixer.io/latest?base=USD")       // Use service http://fixer.io/
result = http().get("http://api.fixer.io/latest?base=USD"); // Use http://fixer.io/ for
                                                                // to get conversion rate in JSON format
                                                            // conversion rate in JSON
usdToEur = JSON.parse(result.body)["rates"]["Eur"]               // Use standard JavaScript object JSON
usdToEur = JSON.parse(result.body)["rates"]["Eur"];        // Use JavaScript object JSON
                                                                // to parse the result
                                                            // to parse the result
entry().set("PriceEUR" , entry().field( "PriceUSD") * usdToEur ) // Multiply PriceUSD by the conversion rate
entry().set("PriceEUR",
                                                                // to determine the value for PriceEUR
    entry().field( "PriceUSD") * usdToEur );                // PriceUSD * conversion rate
                                                            // for value for PriceEUR
</source>
</source>