JavaScript fieldsOnSave or onSave Commands Example
In this example, if a user creates a lead record and saves its status as Qualified, then a message is displayed prompting (forcing) a follow up call to be created by immediately opening a new Call record. The fieldsOnSave command reads the value currently set in the Status field. The onSave command executes the action by checking to see if the status is equal to Qualified. The following assumptions are made in this example:
- The MEFIELDNAME of the status field in the current Lead module is LEADKW01.
- The MODULEID for the Activity module is 6.
The typical format for a fieldsOnSave or onSave query is as follows:
function fieldsOnSave(){ return "MODULEID.MEFIELDNAME"; }Â Â function onSave(name){ if( name == 'Qualified' ){Â window.location = "melt://showAlert/Message to display";Â window.location = "melt://createNew/MODULEID";Â } }
An example of a complete fieldsOnSave query with header and footer information is as follows:
<html><head><title>fieldsOnSave</title><script type="text/javascript"> function fieldsOnSave(){ return "this.LEADKW01";} function onSave(Status){ if(Status == 'Qualified') { window.location = "melt://showAlert/You must create an Activity for Qualified Leads" ; window.location = "melt://createNew/6" ; } } </script></head><body></body></html>
where:
fieldsOnSave defines which field to read. The field being read is Status, which is the LEADKW01 field in the current Lead module. Because the field is within the current module, you can use the actual field instead of the module ID. In the fieldsOnSave example, the code is as follows:
function fieldsOnSave(){ return "this.LEADKW01"; }
function onSave(name) gives the status keyword an alias name to use in the conditional query.
In the fieldsOnSave example, the code is as follows:
function onSave(Status)
A left brace { begins the IF query, which specifies the condition to check for, for example, as follows:
if(Status == 'Qualified')
The actions to perform are specified next, enclosed within braces {}. In this example, two actions are performed, and they are separated by a semicolon. The first action is to display a message, and the second action is to create a new call record (Module ID 8). In the fieldsOnSave example, the code is as follows:
{window.location = "melt://showAlert/You must create an Activity for Qualified Leads"; window.location = "melt://createNew/6";}
A right brace } closes the IF query.