Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
Showing results for 
Search instead for 
Did you mean: 
Active Participant
This short document explain one possible alternative to avoid the utilization of code in the "onInitialization", mainly when we need to improve performance in our SAC AD (when it's possible).

When we introduce code in the "onInitialization" we are stacked with the performance of execution sequencial instructions one by one. In some cases just to introduce code "onInitialization" we increase the duration in more 5 seconds.

In the below points I want to explain how we can use an alternative option to initialize our application without of the use of "onInitialization" in the canvas.

Alternative to "onInitialization"

To achieve this we need to use two techniques:

  1. Create a Custom Widget (CW).

  2. Use of onPostMessageReceive.

"onPostMessageReceive" is an event called when the application receives a message from another component like a hosting page or a custom widget. It's important to check the origin of this post messages.

In this alternative the idea is to create a simple CW with a simple property (Builder Panel) that allows to specify the "message" / "method" to send as a postMessage. The code can be inserted in "onCustomWidgetAfterUpdate(changedProperties)".

As son as the CW is loaded during the runtime of the application the "postMessage" is sent to the application and the event "onPostMessageReceived" is triggered. Here is where we can call the list of functions or instructions to execute.

Use of Simple Custom Widget (CW)

This simple CW must have at least:

  1. The JSON file definition.

  2. The Javascript for the main/core CW definition.

  3. The Javascript for the builder panel properties definition.

You can find more details how to create a CW in some blogs or also in the SAP Help:

The JSON file could be for example as the below one:
"name": "SAC OnInitialization",
"description": "SAC OnInitialization Avoid CW",
"eula": "EULA",
"vendor": "jmlds",
"license": "1.0",
"id": "",
"newInstancePrefix": "SACOnInitializationAvoidCW",
"version": "1.0.0",
"icon": "",
"webcomponents": [
"kind": "main",
"tag": "com-sap-jmlds-oninitialization-main",
"url": "",
"integrity": "" ,
"ignoreIntegrity": true

"kind": "builder",
"tag": "com-sap-jmlds-oninitialization-bp",
"url": "",
"integrity": "",
"ignoreIntegrity": true
"properties": {
"width": {
"type": "integer",
"default": 50
"height": {
"type": "integer",
"default": 20
"method": {
"type": "string",
"description": "Method",
"default": "onInitialization"

"methods": {
"clear": {
"description": "Clear the resultset"
"events": {
"onClick": {
"description": "Only for Visible Process Click"

The builder panel JavaScript file must contains the property for the "method" / "message" configuration:
(function()  {
let base = document.createElement("template");
base.innerHTML = `
<form id="frmSacPerf">
<legend>Method Call</legend>
<td>Name of the Method</td>
<td><input id="id_method" type="text" size="20" maxlength="25"></td>
<input type="submit" style="display:none;">
:box {
display: block;
padding: 1px 1px 1px 1px;

class PanelSacPerf extends HTMLElement {
constructor() {
this._shadowRoot = this.attachShadow({mode: "open"});
this._shadowRoot.getElementById("frmSacPerf").addEventListener("submit", this._submit.bind(this));

_submit(e) {
this.dispatchEvent(new CustomEvent("propertiesChanged", {
detail: {
properties: {
method: this.method

set method(setMethod) {
this._shadowRoot.getElementById("id_method").value = setMethod;

get method() {
return this._shadowRoot.getElementById("id_method").value;

customElements.define("com-sap-jmlds-oninitialization-bp", PanelSacPerf);

The main JavaScript that must send the message to the canvas receiver must have at least, the definition of the template, the constructor, the declaration of the custom element and the function / event "onCustomWidgetAfterUpdate(changedProperties)":
(function () {
let template = document.createElement('template');
template.innerHTML =
`<button type="button" id="myBtn">SACOnInitializationAvoidCW</button>` ;

class SacPerf extends HTMLElement {
constructor() {

init(e) {
let shadowRoot = this.attachShadow({mode: "open"});
this.addEventListener("click", event => {
var event = new Event("onClick");

if ("method" in changedProperties) {
this.$method = changedProperties["method"];

var msg="connectedCallback";
fireOnClick() {
console.log("OnClick Triggered");

customElements.define('com-sap-jmlds-oninitialization-main', SacPerf);

Executing the instructions moved from onInitialization

When the CW is loaded and the event "onCustomWidgetAfterUpdate" is triggered with the postMessage we are triggering also in the Canvas the function "onPostMessageReceived" and here we can execute the instructions moved from the "onInitialization". The next sample is a piece of the application used as base for those tests and to document this sample:
case "onInitialization_1":
StartTime =;
console.log("#### Load App... ####");
case "onInitialization2":

Having the code onInitialization

In this application sample, having all the instructions onInitialization the application takes more 5 seconds to show the result. The below are the instructions moved:
StartTime =;
console.log("#### Load App... ####");
console.log("#### Initialization 2 ####");
console.log("#### Initialization 3 ####");
EndTime =;
console.log("#### App Finished. Time Taken=" + (Math.floor((EndTime-StartTime)/1000)).toString());

Here is the simple application created as an example for this experience:

Potential uses of postMessage + CW

We can use the postMessage with a CW to improve our application, for example:

  • As a CW menu passing the values to variables, parameters, ... in our application without the need to refresh the window or the reload of the full application.

  • As a CW that calls custom functions created in our application without the need to pass in the URL variables.

  • As a CW that activate / trigger any standard widget. For example to link from CW to another widgets.

  • ...

Labels in this area