replaceBlock

Replaces the selected component on the canvas of a marketing content item. Updates persist when the user saves their changes in the content builder. Use this function when building a CMS extension for Marketing Cloud Next that interacts with components on the canvas.

Syntax 

1import { LightningElement, wire } from "lwc";
2import { replaceBlock } from "experience/blockBuilderApi";
3
4class Example extends LightningElement {
5  blockText;
6  block;
7
8  @wire(getCurrentSelectedBlock, {})
9  onCurrentSelectedBlock({ data }) {
10    if (data) {
11      // extract the text attribute
12      this.blockText = data.attributes.text;
13
14      // convert the block JSON to a JS object
15      this.block = JSON.parse(JSON.stringify(data));
16    }
17  }
18
19  // manipulate the text, or other block attributes
20
21  updateBlock() {
22    const id = this.block.id;
23    this.block.attributes.text = this.blockText;
24
25    // replaceBlock requires the block and the nodeId in an object
26    replaceBlock(this.block, {
27      nodeId: id,
28    }).then((response) => {
29      // while the call returns a promise with a payload,
30      // it is not an indication of the success of the
31      // replace operation, since it is asynchronous
32    });
33  }
34}

Parameters 

Parameter NameTypeDescriptionRequired?
selectedBlockObjectThe new component that replaces the selected component on the canvas.Yes
nodeIdStringThe ID of the selected component (or node) to be replaced. This field must be included in an object as the second parameter.Yes

Returns 

  • A Promise object that resolves when the replaceBlock action has been initiated. The replacement operation is asynchronous in the content builder.
  • error—A validation error indicating that the nodeId was invalid or not provided, or that the block is invalid.

Release Preview

This release is in preview. Features described here don't become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can't guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.