{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":879,"path":"api-reference/server-api-reference/PreferenceDestinationBothAPI.md","title":"PreferenceDestination - Scoped, Global","bundle":null,"doc_type":null,"release":"australia","body":"\n# PreferenceDestination - Scoped, Global\n\nThe PreferenceDestination API updates user notification preferences.\n\nA notification destination is somewhere that a notification can be delivered to, such as a specific email address or phone number. This API is based on notifications from the Notification \\[sys\\_notification\\] table. Notifications are sent through channels such as email or Workspace. A channel can be used to send notifications to multiple types of destinations. For example, an email channel could send notifications to both personal email and work email destinations. Destination types are listed in the Notification Destination Type \\[sys\\_notif\\_destination\\_type\\] table.\n\nA user can update their own notification preferences, and an admin can update notification preferences for any user. User notification preferences control which destinations will receive which notifications.\n\nUse this API with the [Preferences](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/PreferencesBothAPI.md) API. Before calling any methods in this class, you must call the Preferences.getDestinations\\(\\) or Preferences.getDestinationsByChannel\\(\\) methods to instantiate a PreferenceDestination object.\n\nThis class uses the `sn_notification` namespace identifier.\n\n**Parent Topic:**[Server API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/api-server.md)\n\n## PreferenceDestination - getChannel\\(\\)\n\nReturns the channel that is used to send notifications to the destination.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|GlideRecord|GlideRecord from the Notification Channel \\[sys\\_notification\\_channel\\] table for the channel that is used to send notifications to the destination.|\n\nThis example prints the channel that is used to send notifications for each of Abel Tuter's destinations.\n\n```\nvar recipient = new GlideRecord('sys_user');\nrecipient.get('last_name', 'Tuter');\nvar prefs = new sn_notification.Preferences(recipient);\nvar dests = prefs.getDestinations();\nfor (var i=0; i<dests.length; i++) {\n     gs.print('Channel for destination '+(i+1)+' is '+dests[i].getChannel().name); \n}\n```\n\nOutput:\n\n```\nChannel for destination 1 is Workspace\n```\n\n## PreferenceDestination - getDeliverTo\\(\\)\n\nReturns the identifier for the destination.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|String|Identifier for the destination. For example, an email address, push application install token, or sys\\_id.|\n\nThis example prints the identifier for each of Abel Tuter's destinations. The output shows that the identifier for Abel's Workspace destination is the user sys\\_id.\n\n```\nvar recipient = new GlideRecord('sys_user');\nrecipient.get('last_name', 'Tuter');\nvar prefs = new sn_notification.Preferences(recipient);\nvar dests = prefs.getDestinations();\nfor (var i=0; i<dests.length; i++) {\n     gs.print('The identifier for '+recipient.first_name+'\\'s '+dests[i].getDestinationType().name+' destination is '+dests[i].getDeliverTo());\n}\n```\n\nOutput:\n\n```\nThe identifier for Abel's Workspace destination is 62826bf03710200044e0bfc8bcbe5df1\n```\n\n## PreferenceDestination - getDestinationType\\(\\)\n\nReturns the type of destination, such as personal email or work email.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|GlideRecord|GlideRecord from the Notification Destination Type \\[sys\\_notif\\_destination\\_type\\] table for the destination type.|\n\nThis example prints the identifier for each of Abel Tuter's destinations. The output shows that the identifier for Abel's Workspace destination is the user sys\\_id.\n\n```\nvar recipient = new GlideRecord('sys_user');\nrecipient.get('last_name', 'Tuter');\nvar prefs = new sn_notification.Preferences(recipient);\nvar dests = prefs.getDestinations();\nfor (var i=0; i<dests.length; i++) {\n     gs.print('The identifier for '+recipient.first_name+'\\'s '+dests[i].getDestinationType().name+' destination is '+dests[i].getDeliverTo());\n}\n```\n\nOutput:\n\n```\nThe identifier for Abel's Workspace destination is 62826bf03710200044e0bfc8bcbe5df1\n```\n\n## PreferenceDestination - isActive\\(GlideRecord notification\\)\n\nChecks if the destination has permission to receive a notification.\n\n|Name|Type|Description|\n|----|----|-----------|\n|notification|GlideRecord|Optional. Notification to check if the destination has permission to receive that notification. An exception is thrown if the notification doesn't exist or if the notification isn't readable by the destination's user. If no notification is specified, this method checks if the destination has permission to receive any notifications.|\n\n<table id=\"table_k4d_bw3_wnb\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nBoolean\n\n</td><td>\n\nFlag that indicates whether the destination has permission to receive a notification.Possible values:\n\n-   true: The destination has permission receive a notification.\n-   false: The destination doesn't have permission to receive a notification.\n\n</td></tr></tbody>\n</table>This example checks if each of Abel Tuter's destinations has permission to receive the Approval Request notification.\n\n```\nvar recipient = new GlideRecord('sys_user');\nrecipient.get('last_name', 'Tuter');\nvar prefs = new sn_notification.Preferences(recipient);\nvar dests = prefs.getDestinations();\nvar notif = new GlideRecord('sys_notification'); \nnotif.get('name', 'Approval Request'); \nfor (var i=0; i<dests.length; i++) {\n   var active = dests[i].isActive(notif) ? 'active' : 'inactive'; \n   gs.print(dests[i].getDestinationType().name + ' ' + dests[i].getDeliverTo() + ' is ' + active + ' for this notification');\n}\n```\n\nOutput:\n\n```\nWorkspace 62826bf03710200044e0bfc8bcbe5df1 is active for this notification\n```\n\n## PreferenceDestination - isOverriden\\(GlideRecord notification\\)\n\nChecks if a notification overrides a user's preferences for the destination.\n\n|Name|Type|Description|\n|----|----|-----------|\n|notification|GlideRecord|Specify a notification to check if it overrides user preferences for the destination. The notification should be a GlideRecord from the Notification \\[sys\\_notification\\] table.|\n\n<table id=\"table_t1c_5v3_wnb\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nBoolean\n\n</td><td>\n\nFlag that indicates whether the notification overrides user preferences for the destination.\n\n Valid values:\n\n-   true: User preferences for the destination are ignored for this notification.\n-   false: The destination receives or doesn't receive the notification according to the user preference.\n\n</td></tr></tbody>\n</table>This example checks if the Approval Request notification overrides user preferences for one of Abel Tuter's destinations. If the notification doesn't override user preferences, the destination is given permission to receive the notification. Otherwise, no preference is set because the notification overrides the preference.\n\n```\nvar recipient = new GlideRecord('sys_user');\nrecipient.get('last_name', 'Tuter');\nvar prefs = new sn_notification.Preferences(recipient);\nvar dests = prefs.getDestinations();\nvar notif = new GlideRecord('sys_notification'); \nnotif.get('name', 'Approval Request'); \nvar firstDest = dests[0]; \nif (!firstDest.isOverridden(notif)) { \n     dests[0].setActive(notif, true); \n} else { \n     gs.print('This notification overrides user preference, so don't bother to set a preference.'); \n} \n```\n\n## PreferenceDestination - setActive\\(GlideRecord notification, Boolean active\\)\n\nSets the user preference for a destination to receive or not receive notifications.\n\nThe isOverriden\\(\\) method can be called first to check if the user preference will be ignored, but it is not required.\n\n<table id=\"table_h3k_kw3_wnb\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nnotification\n\n</td><td>\n\nGlideRecord\n\n</td><td>\n\nOptional. If a notification is specified, the user preference is set for the destination to receive or not receive that notification. The notification should be a GlideRecord from the Notification \\[sys\\_notification\\] table. If no notification is specified, the user preference is set for the destination to receive or not receive all notifications.\n\n</td></tr><tr><td>\n\nactive\n\n</td><td>\n\nBoolean\n\n</td><td>\n\nFlag that indicates whether the destination has permission to receive a notification.\n\n Valid values:\n\n-   true: The destination has permission receive a notification.\n-   false: The destination doesn't have permission to receive a notification.\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|None| |\n\nThis example checks if the Approval Request notification overrides user preferences for one of Abel Tuter's destinations. If the notification doesn't override user preferences, the destination is given permission to receive the notification. Otherwise, no preference is set because the notification overrides the preference.\n\n```\nvar recipient = new GlideRecord('sys_user');\nrecipient.get('last_name', 'Tuter');\nvar prefs = new sn_notification.Preferences(recipient);\nvar dests = prefs.getDestinations();\nvar notif = new GlideRecord('sys_notification'); \nnotif.get('name', 'Approval Request'); \nvar firstDest = dests[0]; \nif (!firstDest.isOverridden(notif)) { \n     dests[0].setActive(notif, true); \n} else { \n     gs.print('This notification overrides user preference, so don't bother to set a preference.'); \n} \n```\n\n"}}