imported>Evelyn |
imported>Aeric |
Line 1: |
Line 1: |
| This example shows a custom post-selection lookup script in the standard Benefits object in the Employee Manager application.
| | {{Deprecated AJAX API}} |
|
| |
|
| 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".
| | {{Deprecated_page|8.0|80}} |
| | |
| You enter scripting by following the instructions in [[Lookup#Customize_Lookup_Field_Options|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|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 <tt>alert</tt> statements in this example for production code.
| |
| | |
| <pre>
| |
| 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);
| |
| </pre>
| |
| | |
| The example for [[#On Change Event Script|On Change Event Script]] showed how you call <tt>setServerId</tt> and specify <tt>{$AppSessionID}</tt> to use the credentials of the current logged in user.
| |
| | |
| The next lines of code set up a search as you have seen previously:
| |
| # Create an instance of <tt>LongJump.Record</tt>
| |
| # Set the object identifier of the Employees object
| |
| # Set the field list to retrieve
| |
| # Set the [[Expressions#Filter_Expressions|filter expression]], specifying to match on the <tt>record_id</tt> field in the Employee object that has the same value as the <tt>id</tt> of the record that the user selected; Learn more [[recordID|record_id]]
| |
| # Call <tt>searchRecords</tt>
| |
| | |
| <pre>
| |
| //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'));
| |
| </pre>
| |
| | |
| As shown previously, the <tt>searchRecords</tt> call returns an instance of <tt>LongJump.Record</tt> which is called <tt>result</tt>. The <tt>status</tt> property of the first element of <tt>result</tt> contains "ERROR" when the search does not retrieve any records. If records were retieved, then the <tt>status</tt> property of the first element contains "RECORD". So, the code first handles the error condition by displaying an error dialog:
| |
| <pre>
| |
| if(result[0].getStatus() != 'RECORD')
| |
| {
| |
| alert("Enter a valid 'Employee ID'");
| |
| }
| |
| </pre>
| |
| | |
| If there was not an error, it means there was a match. The final steps are to check the <tt>employee_type</tt> field:
| |
| | |
| * If the <tt>employee_type</tt> field is "Full Time", the <tt>benefit_type[0]</tt> field is set to true
| |
| * Otherwise, the <tt>benefit_type[1]</tt> field is set to true
| |
| | |
| <pre>
| |
| 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;
| |
| }
| |
| } | |
| </pre>
| |