[SalesForce] Bell icon in communities: syntax of the setTargetPageRef string

The Winter '21 CustomNotification Class offers two ways to set the target navigated to when the message displayed under the "bell" icon is clicked:

  • setTargetId(Id targetId) – Sets the target record of the custom notification
  • setTargetPageRef(String pageRef) – Sets the target page of the custom notification

What can the pageRef string contain? I'd like to use these notifications in a community (i.e. the bell notifications) where the page to go to would need to be set via that value. So far no value I have set results in the notification appearing; the notification does appear when the targetId is set.

PS

Based on Shamina's comment/link I tried passing this JSON (without the line feeds; this is one of the LWC PageReference Types) using API 50 and got:

Invalid parameter value for: targetPageRef

for:

{
    "state":{
        "eventId":"a053F000004MTJOQA4"
    },
    "attributes":{
        "name":"event-details"
    },
    "type":"comm__namedPage"
}

I also can't get this to work with e.g. the standard__webPage type either. But see Jeferson's answer where some of the standard__* types work.

What am I missing here? Are only some PageReference types supported?

PPS

Can this be set by any means e.g. via a flow or the REST API? I tried the REST API approach, and got:

Unexpected data type for targetPageRef: expected String

with JSON data for the targetPageRef and:

Invalid parameter value for: targetPageRef

with JSON data serialized to a string for the targetPageRef.

Note that the "Send Custom Notifications" permission must be granted to avoid this error:

Unsupported action type: CUSTOM_NOTIFICATION_ACTION

Best Answer

I could see it working (outside of Communities, I have not tried that part yet) with the following scenarios:

  • Named Page Type (type = standard__namedPage): quite limited but useful.
  • Item Page Type (type = standard__navItemPage): navigates to tabs.
  • Record Page Type (type = standard__recordPage): navigates to a records, in modes such as view, edit (my example), and clone.

Conclusion so far: it seems that the comment from Bartheleway, setTargetPageRef using JSON format syntax is correct, based on my tests, at least.

Note: thanks for the references shared, that LWC PageReference Types & CustomNotification Class references served as the basis of the tests I made.

Code to send a notification to the current user:

// Named Page Type. Drives to Home Page.
/*String addressTest = 
'' +
'    {' +
'        type: \'standard__namedPage\', ' +
'        attributes: {' +
'            pageName: \'home\'' +
'        }'+
'   }'+
'';*/

// Item Page Type. Drives to a custom tab named NotificationTab.
/*String addressTest = 
'' +
'    {' +
'        type: \'standard__navItemPage\', ' +
'        attributes: {' +
'            apiName: \'NotificationTab\'' +
'        }'+
'   }'+
'';*/

// Record Page Type. Drives to the edit page for the given Account record id.
String addressTest = 
'' +
'{' +
'       type: \'standard__recordPage\',' +
'       attributes: {' +
'           recordId: \'0012X000022RUSLQA4\',' +
'           objectApiName: \'Account\',' +
'           actionName: \'edit\'' +
'       }' +
'}';

// Get the Id for our custom notification type
CustomNotificationType notificationType = 
    [SELECT Id, DeveloperName 
        FROM CustomNotificationType 
        WHERE DeveloperName = 'Custom_Notification'];

// Create a new custom notification
Messaging.CustomNotification notification = new Messaging.CustomNotification();
notification.setTitle('Apex Custom Notification');
notification.setBody('The notifications are coming from INSIDE the Apex!');
notification.setNotificationTypeId(notificationType.Id);
notification.setTargetPageRef(addressTest);

// Actually send the notification
try {
    notification.send(new Set<String>{UserInfo.getUserId()});
}
catch (Exception e) {
    System.debug('Problem sending notification: ' + e.getMessage());
}
Related Topic