Triggers: Difference between revisions

Jump to navigation Jump to search
mNo edit summary
mNo edit summary
Line 500: Line 500:


=== Built-in Objects Examples ===
=== Built-in Objects Examples ===
 
See '''[[Trigger 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 ====
Suppose you have a Location field called myLocation. If you would like the coordinates as Real fields, create the Real fields, maybe LocationLatitude and LocationLongitude. 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 loc = e.field("myLocation");
if (loc.hasNext)
    loc = loc.next;
e.set("LocationLatitude", loc.lat);
e.set("LocationLongitude", loc.lng);
</source>
:; 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 coordinates for myLocation will be extracted to the Real fields.
 
<br/>
<br/>
-----
-----
<br/>
<br/>
== See also ==
== See also ==
{| class="wikitable" style="font-size:120%"
{| class="wikitable" style="font-size:120%"