# How can we help?

## Overview

Using Smarty utilities, you can update contact attributes and list values when a message is sent. It's a highly useful tactic to keep contact data up to date.

These updates take place at render time during the sending process as opposed to open time or message preview.

Contact data uploaded to or otherwise stored at Cordial is replicated to a secondary data layer to facilitate speed and availability. This replication process typically ensures data consistency within 30 seconds, dependent upon update frequency and volume. Updates made to contact records (new or existing) may not be immediately available, and we recommend waiting 30-60 seconds before issuing a query for recently updated information.

Use the following code to run the Smarty function. Replace `attribute_key` with the desired attribute to be updated and `new_attribute_value` with the new value.

```
{$utils->updateContact(['attribute_key'=>'new_attribute_value'])}
```

## Update contact attributes

### Update contact's subscribe status

```
{$utils->updateContact(['channels'=>['email'=>['subscribeStatus'=>'subscribed']]])}
```

The above will update the contact's email `subscribeStatus` attribute to `subscribed`.

### Update a string attribute

```
{$utils->updateContact(['preferred_category'=>'jeans'])}
```

The above will update the contact's `preferred_category` attribute to `jeans`.

### Use external variables to update contact attribute

You can pass an [external variable](https://support.cordial.com/hc/en-us/articles/115005855588) in your message using `{$extVars.variableName}` and then add the variable data to a contact attribute of your choice:

```
{$utils->updateContact(['preferred_category'=>{$extVars.jeans}])}
```

The above will update the contact's `flavor` attribute with data passed as an external variable.

## Update number attributes

### Update a number attribute

```
{$utils->updateContact(['viewed'=>'3'])}
```

The above will update the contact's `viewed` attribute to `3`.

### Increment a number

```
{$utils->updateContact(['total'=>['inc' => '+1']])}
```

The above will update a contact's `total` attribute by `+1`.

### Decrement a number

```
{$utils->updateContact(['viewed'=>['inc' => '-1']])}
```

The above will update a contact's `viewed` attribute by `-1`.

## Update array attributes

### Update an array attribute

```
{$utils->updateContact(['color_preference'=>['blue']])}
```

The above will update the contact's `color_preference` array attribute with the value of `blue`.

All existing array values will be overwritten.

```
{$utils->updateContact(['color_preference'=>['blue','orange']])}
```

The above will update the contact's `color_preference` array attribute with the values of `blue` and `orange`. Note that all existing array values will be overwritten.

### Add values to an array attribute

```
{$utils->updateContact(['color_preference'=>['add' => ['pink','green']]])}
```

The above will add the values `pink` and `green` to the contact's `color_preference` array attribute. Existing values will be retained unless the max items limit is exceeded.

If the number of array values exceeds the max items limit (default 25), the oldest values will be replaced with the newest values. The max items limit can be set using a POST or PUT [Account Contact Attributes API call](https://support.cordial.com/hc/en-us/articles/204570347-Account-Contact-Attributes).

### Remove values from an array attribute

```
{$utils->updateContact(['color_preference'=>['remove' => ['pink','green']]])}
```

The above will remove the values `pink` and `green` from the contact's `color_preference` array attribute.

## Update list membership

You can add or remove a contact to a specified list with the following code by replacing `mylist` with the name of the list to be updated and either `true` (on the list) or `false` (off the list).

The value `true` or `false` is a boolean value and is written in Smarty without quotes.

```
{$utils->updateContact(['mylist'=>true])}
```

### Add to a list

```
{$utils->updateContact(['weekly-specials'=>true])}
```

This will add the contact to the `weekly-specials` list.

You can add the contact to more than one list by separating the key/value pairs by a comma: `['weekly-specials'=>true,'monthly-specials'=>true]`.

### Remove from a list

```
{$utils->updateContact(['weekly-specials'=>false])}
```

This will remove the contact from the `weekly-specials` list.

You can remove the contact from more than one list by separating the key/value pairs by a comma: `['weekly-specials'=>false,'monthly-specials'=>false]`.

## Combine multiple update functions

You can combine multiple update functions into one tag.

```
{$utils->updateContact(['preferred_category'=>'shirts','age'=>['inc' => '+1']])}
```

This will update the contact's `preferred_category` attribute to `shirts` and increment the `age` attribute `+1`.

The `$util->updateContact()` method is ignored by test messages, so it's necessary to send a live message in order to use the method.
