on 2025 Mar 18 5:34 AM
I have created the below js Controller with the name PDMDocumentListActions.controller.js in ext/controller folder in app folder.
Fiori element generated through the Fiori application generator in vs code
sap.ui.define(
['sap/m/MessageToast', 'sap/ui/model/json/JSONModel', 'sap/ui/core/mvc/Controller'],
function (MessageToast, JSONModel, Controller) {
'use strict';
return Controller.extend('router.ext.controller.PDMDocumentListActions', {
onDeactivateDocument: function (oEvent) {
var oModel = this.getView().getModel();
var sPath = oEvent.getSource().getBindingContext().getPath();
// Call CAP backend action
oModel.callFunction('/deactivateDocument', {
method: 'POST',
urlParameters: {
docId: oModel.getProperty(sPath + '/docId')
},
success: function () {
MessageToast.show('Document deactivated successfully.');
oModel.refresh();
},
error: function (oError) {
MessageToast.show('Error deactivating document.');
}
});
},
onGetDocumentByDocId: function () {
MessageToast.show('Custom handler invoked.');
},
onInit: function () {
console.log('PDMDcoumentListActions controller initialized.');
var oModel = this.getOwnerComponent().getModel();
var that = this;
//Fetch user roles from backend service
oModel.callFunction('/getUserRoles', {
method: 'GET',
success: function (oData) {
console.log('User roles fetched successfully', oData + ' ' + oData.value);
that._setButtonVisibility(oData.value);
},
error: function (oError) {
console.error('Error fetching user roles', oError);
}
});
},
_setButtonVisibility: function (aRoles) {
var isEntityManager = aRoles.includes('EntityManager');
var oUIModel = new JSONModel({
showActions: isEntityManager
});
this.getView().setModel(oUIModel, 'ui');
}
});
}
);
In manifest.json , I have added the below content in"sap.ui5"
"extends": {
"extensions": {
"sap.ui.controllerExtensions": {
"sap.fe.templates.ListReport.ListReportController": {
"controllerName": "router.ext.controller.PDMDocumentListActions"
}
}
}
}
These are the below action added in manifest.json
"content": {
"header": {
"actions": {
"getDocumentByDocId": {
"id": "getDocumentByDocId",
"press": "router.ext.controller.PDMDocumentListActions.onGetDocumentByDocId",
"visible": "{ui>/showActions}",
"enabled": true,
"text": "Get Document"
},
"deactivateDocument": {
"id": "deactivateDocument",
"press": "router.ext.controller.PDMDocumentListActions.onDeactivateDocument",
"visible": "{ui>/showActions}",
"enabled": true,
"text": "Deactivate Document"
}
}
}
}
The below is the controller handler in cap Java,
package customer.poc_ams_policy.handlers;
import java.util.List;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.sap.cds.services.handler.EventHandler;
import com.sap.cds.services.handler.annotations.On;
import com.sap.cds.services.handler.annotations.ServiceName;
import com.sap.cds.services.request.UserInfo;
import cds.gen.documentservice.GetUserRolesContext;
@Component
@ServiceName("DocumentService")
public class UserRoleHandler implements EventHandler {
private static final Logger logger = LoggerFactory.getLogger(UserRoleHandler.class);
@On(event = "getUserRoles")
public void getUserRoles(GetUserRolesContext context) {
UserInfo userInfo = context.getUserInfo();
List<String> roles = userInfo.getRoles().stream().collect(Collectors.toList());
logger.info("User roles: {}", roles);
context.setResult(roles);
}
}
Please let me know where I went wrong in above code even first line
Request clarification before answering.
You should see the following error log somewhere:
Attempt to load Extension Controller router.ext.controller.PDMDocumentListActions was not successful. Controller extension should be a plain object.
I.e. instead of:
return Controller.extend(...);
simply
return { ... }:
See also:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
i am able to solve the above problem with below code.
return ControllerExtension.extend("router.ext.controller.PDMDocumentListActions", { overrides: { onInit: function () { let oModel = this.base.getAppComponent().getModel(); console.log("Default model", oModel); var that = this; if (!oModel) { console.error("Model is not available."); return; } var oContext = oModel.bindContext("/getUserRoles(...)"); oContext .execute() .then(function () { return oContext.requestObject(); }) .then(function (oData) { console.log("User roles fetched successfully", oData); that._setButtonVisibility(oData.value); }) .catch(function (oError) { console.error("Error fetching user roles", oError); }); } },
User | Count |
---|---|
86 | |
11 | |
8 | |
8 | |
6 | |
6 | |
5 | |
5 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.