Update Profile Attributes with Ampscript & SSJS and Define Subscriber Filters

Author: Florin Valean

Profile update use case

As an exercise we'll aim to update a profile attribute in a Salesforce Marketing Cloud account with multiple business units. Let's assume we want the profile attribute to store the ID of the business unit that performs the email send. To achieve this, we'll have to use functions for managing API objects. We are going to use some ampscript functions such as CreateObject, SetObjectProperty, AddObjectArrayItem, InvokeUpdate and some server-side java script functions like Init and Update.
This scenario can be applied to a multi-org account configuration and once the profile is populated with the business unit id (member id) it can be used for instance to define a subscriber filter at business unit level.


Prerequisites

In a multi-org marketing cloud account (Enterprise 2.0) we are going to create a custom profile attribute called BusinessUnitID. We don't want this attribute to be visible to customers in their profile center therefore we'll set it as hidden. As a best practice it is recommended to define a default value for the custom attribute - in this case the ID of the parent business unit seems fine. We can also define a list of possible values to restrict the values the new attribute can take but this is optional. The profile attribute can be created in Email Studio > Profile Management as shown below:

Profile attribute


Ampscript sample code


%%[ var @mid, @skey set @mid = YOUR BUSINESS UNIT ID set @skey = "YOUR SUBSCRIBER KEY" set @sub = CreateObject("Subscriber") SetObjectProperty(@sub, "SubscriberKey", @skey) set @attr = CreateObject("Attribute") SetObjectProperty(@attr, "Name", "BusinessUnitID") SetObjectProperty(@attr, "Value", @mid) AddObjectArrayItem(@sub, "Attributes", @attr) set @status = InvokeUpdate(@sub) ]%%

It is important to remember that this code cannot be executed as part of an email at the moment of send as Ampscript API functions such as CreateObject() are available only in a non-sendable context.


Server-side javascript sample code


<script runat="server"> Platform.Load("Core", "1"); try { var mid = YOUR BUSINESS UNIT ID; var skey = "YOUR SUBSCRIBER KEY"; var sub = { "Attributes": { "BusinessUnitID": mid } }; var subObj = Subscriber.Init(skey); var status = subObj.Update(sub); Write(Stringify(status)); } catch(e) { Write(Stringify(e)); } </script>

You can execute this code snippet in a cloud page or a script activity in Automation Studio.

Notice the use of try-catch block - see this page for more details on how/when to use try-catch.


Define a subscriber filter

BusinessUnitID profile attribute can now be used to define a subscriber filter at business unit level. In order to define a subscriber filter you have to be logged in at the parent business unit level as administrator, mouseover your name in the top-right corner of the Marketing Cloud app and navigate to Setup > Business Units. Here you can select one of the business units available (tick the box) and then click Define Subscriber Filter. See below:

Business units

In the options that show up select the BusinessUnitID profile attribute and allow the business unit to access only those subscribers that meet the criteria you define here. See the subscriber filter example below:

Define Subscriber Filter
Share this page
Stay in touch

Subscribe to the newsletter

p1 p2 p3