back to top

Add Code to When the View Model is Initialized (or the Equivalent of the onEntityInitialized Function) in a Creatio Freedom UI Page

In Creatio classic pages, you could add a function called onEntityInitialized to perform things when an edit page loads and the data is available to access. In Freedom UI pages, to do the same, you need to wire up a handler for the crt.HandleViewModelInitRequest.

To add code to the page that will fire when the page’s model/data is available and ready, simply add the following to the page’s handlers:

{
	request: "crt.HandleViewModelInitRequest",
	handler: async (request, next) => {
		// allow base page initialization to complete
		await next?.handle(request);
		
		// add any custom code here
	}
}

This is the equivalent to the onEntityInitialized in a Freedom UI page. In this handler, you can also to things you’re used to doing in classic Creatio pages as well. For example, check if the page is for and “add” or “copy”:

{
	request: "crt.HandleViewModelInitRequest",
	handler: async (request, next) => {
		await next?.handle(request);
		
		const cardState = await request.$context.CardState;
		if (cardState == "add" || cardState == "copy") {
			// do something here
		}
	}
}

Additionally, any field/attribute values from the page can be accessed via the request.$context.

Contact Customer FX for your CRM project
We let our expertise speak for itself - let us know how our all-star team can help on your CRM project
About the Author
Ryan Farley
Ryan Farleyhttps://customerfx.com/article/author/ryanfarley/
Ryan Farley is the Director of Development for Customer FX and creator of slxdeveloper.com. He's been blogging regularly about SalesLogix, now Infor CRM, since 2001 and believes in sharing with the community. His new passion for CRM is Creatio, formerly bpm'online. He loves C#, Javascript, web development, open source, and Linux. He also loves his hobby as an amateur filmmaker.

2 COMMENTS

  1. Hi Ryan, thanks for this, is there any way to set entity field attributes from this onEntityInitialized equivalent? When I try to do so, it seems that any values I set are overwritten by the entity being loaded/default values being populated (in the case of new records) and I can’t see a nice way of preventing that.

    • Hi Harvey,

      Setting values in the crt.HandleViewModelInitRequest actually broke in 8.0.10 due to how default or initial values are getting set (including a “blank” default value which is the case for every field without a default, essentially clearing any values your code has set). I have a case with Creatio and have been told that a fix is coming in 8.1.1 or 8.1.2 (I’ve not yet confirmed in the fix is present in 8.1.1).

      For now, you can do one of two work arounds to set values in this init.

      Workaround Option 1:
      Use a setTimeout to set the values, this will break it out of the current message loop and slightly delay the setting of the default values until after the defaults have been set. For example:

      setTimeout(async () => {
          request.$context.DateTimeAttribute_gubw95y = new Date();
      }, 100);

      Using this approach will delay it just enough to allow the form to set default/initial values before your value gets set.

      Workaround Option 2:
      Wire a change for the Id, and then set any values from there:

      Add a change event for the setting of the form’s Id:

      viewModelConfigDiff: /**SCHEMA_VIEW_MODEL_CONFIG_DIFF*/[
      {
      	"operation": "merge",
      	"path": [
      		"attributes"
      	],
      	"values": {
      		"Id": {
      			change: {
      				request: "usr.SetFormValues" // <- wire up request for change of Id
      			}
      		}
      	},
      	{
      		"operation": "merge",
      		"path": [
      			"attributes",
      			"Id",
      			"modelConfig"
      		],
      		"values": {
      			"path": "PDS.Id"
      		}
      	}
      ]/**SCHEMA_VIEW_MODEL_CONFIG_DIFF*/,
      handlers: /**SCHEMA_HANDLERS*/[
      	{
      		request: "usr.SetFormValues",
      		handler: async (request, next) => {
      			const result = await next?.handle(request);
      					
      			const cardState = await request.$context.CardState;
      					
      			if (cardState == "add" || cardState == "copy") {
      				request.$context.DateTimeAttribute_s98fa18 = new Date();
      			}
      
      			return result;
      		}
      	}
      ]/**SCHEMA_HANDLERS*/

      Ryan

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Embedding a Creatio Chat Widget on your Website in Less than 5 Minutes

Creatio version 10X has an out of the box...

Query the Creatio Database to Locate Source Code or Client Schemas Containing a Value

Often when working with Creatio, I need to locate...

Building AI-Powered Workflows in Creatio Without Writing Code

See how Creatio can combine AI with no-code business processes to qualify, summarize, and route incoming leads while keeping your existing business rules and human oversight in place.

AI Readiness Checklist for CRM Teams: Is Your CRM System Ready for AI?

Before using AI in Creatio, make sure your CRM data, users, processes, and automation foundation are ready. Use this practical AI readiness checklist for CRM teams.

Build Your Creatio AI Skills with the 2026 AI Summer Program

Join Creatio’s free 2026 AI Summer Program to learn how to identify, build, deploy, and manage AI agents.

Related Articles

Embedding a Creatio Chat Widget on your Website in Less than 5 Minutes

Creatio version 10X has an out of the box feature that allows you to easily embed a Chat...

Query the Creatio Database to Locate Source Code or Client Schemas Containing a Value

Often when working with Creatio, I need to locate code or client schemas based on their content, not...

Building AI-Powered Workflows in Creatio Without Writing Code

See how Creatio can combine AI with no-code business processes to qualify, summarize, and route incoming leads while keeping your existing business rules and human oversight in place.

AI Readiness Checklist for CRM Teams: Is Your CRM System Ready for AI?

Before using AI in Creatio, make sure your CRM data, users, processes, and automation foundation are ready. Use this practical AI readiness checklist for CRM teams.

Related Videos