Write, Bureaucrats, Administrators
4,332
edits
mNo edit summary |
mNo edit summary |
||
Line 642: | Line 642: | ||
== Built-in Objects Examples == | == Built-in Objects Examples == | ||
=== Example: Extracting components from a Contact field into separate fields === | |||
Suppose you have a Contact field called myContact. If you would like the name, primary phone, and primary email as separate fields, create the fields, maybe ContactFullname, ContactPrimaryPhone, and ContactPrimaryEmail. The create two triggers, as follows: | |||
:; CreateBefore | |||
:: Event: Creating a new entry | |||
:: Phase: Before saving the entry | |||
:: Script: As follows | |||
<source lang="JavaScript"> | |||
var e = entry(); | |||
var contact = e.field("myContact"); | |||
if (contact.hasNext) | |||
contact = contact.next; | |||
e.set("ContactFullname", contact.fullName); | |||
e.set("ContactPrimaryPhone", contact.phone); | |||
e.set("ContactPrimaryEmail"), contact.email); | |||
</source> | |||
<br/> | |||
:; UpdateBefore | |||
:: Event: Updating an existing entry | |||
:: The rest the same as above. | |||
Now, every time you create a new entry or update an existing one, the contact information will be extracted to separate fields. | |||
=== Example: Extracting coordinates from a Location field into Real fields === | === Example: Extracting coordinates from a Location field into Real fields === | ||
Line 654: | Line 677: | ||
var loc = e.field("myLocation"); | var loc = e.field("myLocation"); | ||
if (loc.hasNext) | if (loc.hasNext) | ||
loc = loc.next | loc = loc.next; | ||
e.set("LocationLatitude", loc.lat); | e.set("LocationLatitude", loc.lat); | ||
e.set("LocationLongitude", loc.lng); | e.set("LocationLongitude", loc.lng); |