/
5.21.4.3.3 - Update Custom Field Information
5.21.4.3.3 - Update Custom Field Information
This example will show you how to use scripts to update custom fields.
Script
Update Custom Field Information
// The first line specifies the field you want the script to depend on [Changed.CustomField(6364).Changed] function on_save() { // The line below specifies that the script will run when value of the field is 50 or below if (Changed.CustomField("6364").NewValueAsNumber <= 50) { // The section below picks out a custom field and specifies what the value should be changed to // and adds a system message to notify the user that the change has taken place //The format for updating a custom field is "Service.UpdateCustomField("Field name or ID", "Value") Service.UpdateCustomField("6390", "Decline"); Service.AddMessage("Custom field successfully updated", "notify-success"); //"return true" means that the new data will be stored in your system while "return false" signifies that the new data will not be saved return true; //This section specifies how the field should be updated if the value is above 50 //a different custom field can be specified here if you want to update a separate field } else if (50 < Changed.CustomField("6364").NewValueAsNumber < 100) { Service.UpdateCustomField("6390", "Proceed"); Service.AddMessage("Custom field successfully updated", "notify-success"); return true; //The script below triggers an error message if neither of the criteria above are satisfied } else { Service.AddMessage("An error occurred while updating custom field", "notify-error"); return false; } } //Service.UpdateCustomField(string nameOrId, string newValue), where nameOrId - name or id of custom field
Updating the field
When you update the field and click save changes, this will trigger the script.
Success notification
If it is included in the script, the success message will inform you that the update was a success. If you have entered an invalid value then an error message will inform you of this.
New Value
Once you have reloaded the page, the new value will be displayed.