Use Comment-Related APIs

As an application designer or story developer, you can use APIs to let viewers show or hide all comments, handle table cell comments and manage filters on comment widgets.

All the existing comment APIs support comment widgets. Data cell comments edited by APIs are synchronized with comment widgets as well.

The comment-related APIs support SAP BW live data models. To know about comments built on the models, see Adding Comments to a Data Cell or Adding Comment Widgets to Your Story.

API to Show or Hide Comments

You can use the following API to enable or disable comment mode, which means showing or hiding all the comments:
Code Syntax
setCommentModeEnabled(isEnabled: boolean): void

APIs to Manage Table Cell Comments

You can use the APIs to let viewers get, remove, add, update and like table cell comments.

Code Syntax
CommentInfo { //For BW commenting, CommentInfo always refers to latest comment
    'commentId': 'string', // For BW commenting, commentId refers to the unique technical DocumentId(Id of BW document) of the specific cell
    'text': 'string',
    'createdAt': 'string',
    'numberOfLikes': 'integer', //For BW commenting, this field should be undefined
    'createdBy': '+UserInfo'
}

getComment(commentId: string): CommentInfo
getAllComments(selection: Selection): CommentInfo[]//For BW commenting, returns the only comment of the latest version.

removeComment(commentId: string): boolean
removeAllComments(selection: Selection): boolean//For BW commenting, removes the only comment along with all history versions

addComment(selection: Selection, value: string): CommentInfo
addComments(selection: Selection, value: string[]): CommentInfo[]//Not supported for BW commenting, return undefined

updateComment(commentId: string, value: string): CommentInfo//Updates comment with the latest version. Only supported for BW commenting.

setCommentLiked(commentId: string, isLiked: boolean): boolean//Not supported for BW commenting, return false
Example
For example, you’ve created an input field InputField_1 and would like to let viewers:
  1. Select Button_1 to see the comment on a table cell in the input field.

  2. Select Button_2 and post the comment in the input field to any selected table cell.

First, write the following script for Button_1:

Sample Code
var comments = Table_1.getDataSource().getComments().getAllComments(Table_1.getSelections()[0]);
console.log(comments);
var str = "";
for (var i = 0; i < comments.length; i++) {
    var txt = comments[i].text;
    str = str + txt;
}
InputField_1.setValue(str);

Then write the following script for Button_2:

Sample Code
var str = InputField_1.getValue();
Table_1.getDataSource().getComments().addComment(Table_1.getSelections()[0], str);

APIs to Manage Filters on Comment Widgets

You can also use the following APIs to set, get or remove filters on comment widgets:
Code Syntax
setDimensionFilter(dimension: string | DimensionInfo, member: string | string[] | MemberInfo | MemberInfo[] | MeasureInfo | MeasureInfo[] | TimeRange | TimeRange[])
getDimensionFilters(dimension: string | DimensionInfo) : FilterValue[]
removeDimensionFilter(dimension: string | DimensionInfo)
Example

Here’s a script example that shows how to make a comment shown in a specific comment widget and apply the same filter as table to it.

Write the following script, so that when 2021 is selected in the filter Date, the comments in CommentWidget_1 are displayed:

Sample Code
CommentWidget_1.getCommentingDataSource().setDimensionFilter("Date_703i1904sd", "[Date_703i1904sd].[YHM].[Date_703i1904sd.YEAR].[2021]");

Furthermore, write the following script to copy the filter Location from Table_1 to CommentWidget_1:

Sample Code
var filter = Table_1.getDataSource().getDimensionFilters("Location_4nm2e04531")[0];
if (filter.type === FilterValueType.Single) {//Table_1 has dimension filter "Location=SA1"
    var singleFilter = cast(Type.SingleFilterValue, filter);
    CommentWidget_1.getCommentingDataSource().setDimensionFilter("Location_4nm2e04531", singleFilter.value);
} else if (filter.type === FilterValueType.Multiple) {//Table_1 has dimension filters "Location=SA1" or "Location=SA2"
    var multipleFilter = cast(Type.MultipleFilterValue, filter);
    CommentWidget_1.getCommentingDataSource().setDimensionFilter("Location_4nm2e04531", multipleFilter.values);
}