Set or Clear Attributes

Use the Salesforce Engagement SDK to set or clear attribute key-value pairs.

This code example shows how to set or clear attributes in the SDK for Android.

1SFMCSdk.requestSdk { sfmcSdk ->
2  // GIVEN
3  val attributesMap = mutableMapOf<String, String>()
4  attributesMap["firstName"] = "Salesforce"
5  attributesMap["lastName"] = "Developer"
6  val attributesToClear: List<String> = getAttributesToClear()
7
8  sfmcSdk.identity.edit {
9    // Set Attributes
10    attributes.put("key1", "value1")
11    attributes.putAll(attributesMap)
12    attributes.set(attributesMap)
13
14    // Clear Attributes
15    attributes.clear("key1")
16    attributes.clear("key1", "key2")
17    attributes.clear(attributesToClear)
18    attributes.clearAll()
19  }
20
21  // Get Attributes
22  val attributes = sfmcSdk.identity.attributes
23}

This code example shows how to set or clear attributes in the SDK for iOS version 10 and later.

1// Set a single attribute
2SFMCSdk.identity.edit { [self] model in
3  model.addAttribute(key: "FavoriteTeamName", value: "favoriteTeamName")
4  return model
5}
6
7// Set multiple attributes
8SFMCSdk.identity.edit { [self] model in
9  model.addAttributes(attributes: ["FavoriteTeamName": "favoriteTeamName"])
10  return model
11}
12
13// Clear a single attribute
14SFMCSdk.identity.edit { [self] model in
15  model.clearAttribute(key: "AttributeKey")
16  return model
17}
18
19// Clear multiple attributes
20SFMCSdk.identity.edit { [self] model in
21  model.clearAttributes(attributes: ["AttributeKey1", "AttributeKey2"])
22  return model
23}
24
25// Get all attributes
26let attributes = SFMCSdk.identity.get()?.attributes

This code example shows how to set or clear attributes in the SDK for Android version 8.

1SFMCSdk.requestSdk { sdk ->
2
3  // GIVEN
4  val attributesMap = mutableMapOf<String, String>()
5  attributesMap["firstName"] = "Salesforce"
6  attributesMap["lastName"] = "Developer"
7  val attributesToClear: List<String> = getAttributesToClear()
8
9  sdk.identity.run {
10
11    // Setting Attributes
12    setProfileAttribute("favoriteCRM", "Salesforce") // Set individual attributes
13    setProfileAttributes(attributesMap) // Set multiple attributes
14
15    // Clearing Attributes
16    clearProfileAttribute("favoriteCRM") // Clear individual attributes
17    clearProfileAttributes(attributesToClear) // Clear multiple attributes
18  }
19
20  // Getting Attributes
21  sdk.mp { push ->
22    val attributes = push.registrationManager.attributes
23  }
24
25}

For earlier versions of the SDK for iOS, use this code.

1// Set an attribute for all modules
2let success = SFMCSdk.identity.setProfileAttributes(["FavoriteTeamName": "favoriteTeamName"])
3// Set attributes for Mobile Push Module only
4let success = SFMCSdk.identity.setProfileAttributes([ModuleName.push: ["FavoriteTeamName": "favoriteTeamName"]])
5// Set attributes individually for all modules
6let success = SFMCSdk.identity.setProfileAttribute("FavoriteTeamName", "favoriteTeamName")
7// Set attributes individually for Mobile Push Module only
8let success = SFMCSdk.identity.setProfileAttribute("FavoriteTeamName", "favoriteTeamName", [ModuleName.push])
9
10// Clear a single attribute
11SFMCSdk.identity.clearProfileAttribute(key: "AttributeKey")
12
13// Clear multiple attributes
14SFMCSdk.identity.clearProfileAttributes(keys: ["AttributeKey1", "AttributeKey2"])
15
16// Get all attributes
17SFMCSdk.requestPushSdk { mp in
18  let attributes = mp.attributes()
19}

Considerations for Managing Attributes 

Keep these requirements in mind when you set or clear attributes.

  • The attribute key can’t be a reserved word, empty, or null.
  • The attribute value can’t be null.
  • The SDK trims whitespace from the beginning and end of the key and the value.
  • Specify dates in ISO 8601 format, such as 2024-05-07T13:28:31+00:00.
  • If you set an attribute to a datetime stamp after the SDK successfully initializes, the registration record is updated every time the SDK initializes.
  • Don’t create an attribute with a datetime value that changes regularly. For example, don’t set an attribute that tracks LastUserInteraction. Frequently changing attribute values has a negative impact on performance.

Reserved Words 

Some words are reserved and can’t be used as attribute names. The SDK ignores attempts to set or clear attributes that contain any of these words.

  • addressId
  • alias
  • apId
  • backgroundRefreshEnabled
  • badge
  • channel
  • contactId
  • contactKey
  • createdBy
  • createdDate
  • customObjectKey
  • device
  • deviceId
  • deviceType
  • gcmSenderId
  • hardwareId
  • hwid
  • isHonorDst
  • lastAppOpen
  • lastMessageOpen
  • lastSend
  • locationEnabled
  • messageOpenCount
  • modifiedBy
  • modifiedDate
  • optInDate
  • optInMethodId
  • optInStatusId
  • optOutDate
  • optOutMethodId
  • optOutStatusId
  • platform
  • platformVersion
  • providerToken
  • proximityEnabled
  • pushAddressExtensionId
  • pushApplicationId
  • quietPushEnabled
  • sdkVersion
  • sendCount
  • signedString
  • source
  • sourceObjectId
  • status
  • systemToken
  • timezone
  • utcOffset