Adding Postdefault Values to Fields
This topic describes how to add a value to a field if the user does not enter any data in this field when the user creates a new record. Adding a value in this way is known as adding a postdefault value. The example in this topic modifies the predefined opportunity form to make sure Siebel CRM Desktop sets the postdefault value for the Lead Quality field in the opportunity to 5-Poor if the user does not set a value for this field.
To add a postdefault value to a field
Use a JavaScript editor to open the form that includes the field you must modify.
Make sure the following function exists:
ctx.form.on_saving.connect(function_name);
where:
function_name is the name of the custom function that handles the event.
This function handles the on_saving event for the form. For example, the predefined opportunity_form function includes the following code:
ctx.form_on_saving.connect(form_saving);
To set the form_saving function, the predefined code uses the following code. It makes sure the opportunity is capitalized. It occurs earlier in the script:
var form_saving = function() { var fields = ctx.form.item.snapshot; ctx.form.item["Name"] = fields["Name"].replace(/\b[a-z]/g,function(w){return w.toUpperCase()}); // Capitalization }
For more information, see Customizing Form Functions.
Add the following code to the end of the form_saving function:
if (validator.empty_field_validator("Quality")) ctx.form.item["Quality"] = "5-Poor";
To determine if the user set the value for the Quality field, this if statement uses the empty_field_validator function in the following way:
If the field is empty, then the empty_field_validator function returns a value of true and the ctx.form.item statement sets the value of the Quality field to 5-Poor.
If the field is not empty, then the empty_field_validator function returns a value of false and the code exits the if statement.
(Optional) Support an environment that does not use English. You do the following:
Replace the ctx.form.item["Quality"] = "5-Poor"; code that you added in step 3 with the following code:
ctx.form.item["Quality"] = ctx.session.res_string("lang_lead_quality_poor");Add the following code to the package_res.xml file:
<str key="lang_lead_quality_poor">5-Poor</str>
The code you added in step 3 works in an environment that uses English but fails in a multilingual environment because you hard-coded the field value. The call to ctx.session.res_string allows you to retrieve a string from the resource file and to write JavaScript code that is language independent.
Test your changes and then republish the customization package.
For more information, see Republishing Customization Packages.