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.

Want content like this delivered to your inbox? Sign up for our newsletter!
ABOUT THE AUTHOR

Ryan Farley

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.

    Reply
    • 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

Submit a Comment

Your email address will not be published. Required fields are marked *