Lookup Post-Selection Event Script

From LongJump Support Wiki
Revision as of 19:05, 26 January 2010 by imported>Evelyn (changed "Lookup Scripting" to "Post Selection JavaScript")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This example shows a custom post-selection lookup script in the standard Benefits object in the Employee Manager application.

At runtime, when a user selects an employee name via the Lookup field in Benefits, the post-selection lookup script searches the Employee object based on the selected employee. If the selected employee is full time, the script sets the Benefit Type to "HMO"; if the selected employee is part time, the script sets the Benefit Type to "PPO".

You enter scripting by following the instructions in Customize Lookup Field Options and Post Selection JavaScript.

The following explains the JavaScript code line by line for the lookup post-selection event script for this example. You can find the complete code in Lookup Post-Selection Event Script Example.

The code starts by displaying a series of dialogs to let you know what is being selected while you are testing. Obviously, you would comment these and later such alert statements in this example for production code.

alert("You have selected the following Employee from the lookup field :" + name);
alert("The record ID of the employee is : " + id);
alert("The social security of the employee is:" +
    window.opener.document.mainForm.social_security_number.value);

The example for On Change Event Script showed how you call setServerId and specify {$AppSessionID} to use the credentials of the current logged in user.

The next lines of code set up a search as you have seen previously:

  1. Create an instance of LongJump.Record
  2. Set the object identifier of the Employees object
  3. Set the field list to retrieve
  4. Set the filter expression, specifying to match on the record_id field in the Employee object that has the same value as the id of the record that the user selected; Learn more record_id
  5. Call searchRecords
//login using the credentials of the current session
AjaxInfo.setServerId('{$AppSessionID}');

var record = new LongJump.Record();
record.set('object','1555611998oit1617201617');
record.set('field_list',"record_id,first_name,last_name,security_id_,employee_type");
record.set('filter', "record_id equals'" + id + "'");
var result = AjaxAPI.searchRecords(record);
alert("Search Result: " + result[0].get('message'));

As shown previously, the searchRecords call returns an instance of LongJump.Record which is called result. The status property of the first element of result contains "ERROR" when the search does not retrieve any records. If records were retieved, then the status property of the first element contains "RECORD". So, the code first handles the error condition by displaying an error dialog:

if(result[0].getStatus() != 'RECORD')
{
    alert("Enter a valid 'Employee ID'");
}

If there was not an error, it means there was a match. The final steps are to check the employee_type field:

  • If the employee_type field is "Full Time", the benefit_type[0] field is set to true
  • Otherwise, the benefit_type[1] field is set to true
else
{
    alert('Employee you have selected is valid.');
    if((result[0].get('employee_type')) == "Full Time")
    {
        alert("Full-time Employee selected! \n HMO Benefits will be selected: " +
            result[0].get('employee_type'));
        var form = window.opener.document.mainForm;
        form.benefit_type[0].checked = true;
    }
    else
    {
        alert("Part-time Employee! \n PPO Benefits will be selected: " +
            result[0].get("employee_type"));
        var form = window.opener.document.mainForm;
        form.benefit_type[1].checked = true;
    }
}