Calling Custom Code From a Page Extension

To call a custom code from a page extension you will need to create a dynamic action and then call it from an AJAX call.

This example verifies if an account already exists on a add new account page.

Configuration

Adding the Custom Code

Start by logging in as an administrator, and navigate to the Setup page. Under the Configuration category, click on the Custom Code then add new and set the name to “Verify User Account Name”. In the “Code” field, copy and paste the following:

//Reference: Castle.MonoRail.Framework //Reference: System.Data //Reference: Presentation using System; using Castle.MonoRail.Framework; using Logisense.Boss.Logic.DomainModel; namespace Logisense.Boss.CustomScripts{ public class AJAXCheckAccountNameIfExists : IDynamicAction { public void Execute(Controller controller) { string accountName = controller.Request["accountName"]; bool exists = false; if(!string.IsNullOrEmpty(accountName)) { try { if(User.GetByName(accountName)!=null) { exists = true; } } catch (Exception) {} } controller.RenderText(exists ? "true" : "false"); } } }

Adding the Page Extension

Next, we need to configure the page extension. Create a new page extension called “Check Account Name” targeting the page “User/Add”. Paste the following code into the script box:

jQuery("#User.Name").change(function() { var elemID = this.id; var elemValue = this.value; jQuery.get("/AJAXCheckAccountNameIfExists.rails", { accountName: this.value }, function(data) { if (data === 'true') { //make it invalid SetInvalidField(elemID); alert('Account Name ' + elemValue + ' already exists'); } else { SetValidField(elemID); } }); });

The snippet of jQuery above binds a change handler to the account box. As the change event fires, a lookup is performed by calling the custom code (above) to see if the user already exists. If there is an account that matches, an alert will fire to let the user know.