This is one of every marketer’s worst nightmares 👻:

Your plate is full and you barely have time to finish your tasks for the day, when your Marketing Manager asks you to upload a list to Marketo ASAP.

You get it done quickly, but your SDR says the leads have the wrong information!

The list is full of bad formatting and incorrect entries.

It’s a disaster. Some leads have already existed in your database for years and now have the wrong job titles, others have come from different sources, etc.

Fortunately, Marketo’s API has a solution – a “time machine” of sorts that allows us to revert data changes.

In this guide, we’ll show you how to roll back your data using the API.

Let’s get into it!

(This guide is for Marketo users and assumes a basic understanding of the Marketo API. If you want to learn more, check out our API webinar and an extensive API course by Tyron Pretorius.)

pink line

Note: Whenever we use the Marketo APIs, remember to obtain a Marketo API access token before anything else. This will be required to communicate with the API.

Step 1. Extract data value change activities.

First, we need to identify the recent data changes that occurred when we uploaded the faulty list. 

Using the Marketo API, we can pull a log of data value change activities. This will give us both the old values and the new (incorrect) values, allowing us to update lead fields with the original old values via the API in later steps. 

Start by acquiring the “paging token” using this code snippet:

 url="https://"+MUNCHKIN+".mktorest.com/rest/v1/activities/pagingtoken.json"
token=get_access_token()
params={'access_token': token,
        'sinceDatetime':sinceDate}
response=requests.get(url=url,params=params)
data=response.json()
nextPageToken=data['nextPageToken']

 
Next, use this code snippet to extract data value change activities:

url="https://"+MUNCHKIN+".mktorest.com/rest/v1/activities.json"
params={'access_token': token,
        'nextPageToken': nextPageToken,
        'activityTypeIds':[13],
        'listId': listID}
response=requests.get(url=url,params=params)
data=response.json()
print(data)
act=data['result']
while data['moreResult']==True:
    nextPageToken=data['nextPageToken']
    token=get_access_token()
    params={'access_token': token,
                'nextPageToken': nextPageToken,
                'activityTypeIds':[13],
                'listId': listID}
    response=requests.get(url=url,params=params)
    data=response.json()
    print(data)
    act=act+(data['result'])

 

Step 2. Isolate the old values from the list import.

Now that we have the activity log, we can pinpoint the old values that were replaced by the faulty list and extract them.

Use this code snippet to do this:

df=pd.json_normalize(act)
df=df[df['primaryAttributeValue']==field]
df=df.sort_values('activityDate')
df=df.reset_index()
df=df.drop(columns=['index'])
df1=pd.json_normalize(df['attributes'])
i=4
while i<len(df1.columns):
    df1=df1.drop(columns=[i])
    i=i+1
df1.columns=['New_Value','Old_Value','Reason','Source','Other']
df1.New_Value=pd.json_normalize(df1.New_Value)['value']
df1.Old_Value=pd.json_normalize(df1.Old_Value)['value']
df1.Reason=pd.json_normalize(df1.Reason)['value']
df1.Source=pd.json_normalize(df1.Source)['value']
df=pd.merge(df,df1,left_index=True, right_index=True)
df=df.drop(columns=['attributes'])
df=df.drop_duplicates(subset='leadId', keep="first")

 

Step 3. Update Marketo fields with the old (original) values.

We can now restore the old values to our leads using the Marketo API. This process requires a specific API call to update each lead’s information back to its original state.

Use this code snippet to get you started:

ids=df[df.columns.to_list()[2]].to_list()
camposval=df['Old_Value'].to_list()
for i in range(len(camposval)):
    if camposval[i] == None:
        camposval[i] = 'NULL'
STEP=300
a=math.ceil(len(ids)/STEP)
i=0
while i < a: 
    tempids=ids[i*STEP:(i+1)*STEP]
    tempcamposval=camposval[i*STEP:(i+1)*STEP]
    params={'action': 'updateOnly',
            'lookupField': 'id',
            'input':[]}
    j=0
    while j<len(tempids):
        lead={'id':tempids[j],
              fieldRest:tempcamposval[j]}
        params['input'].append(lead)
        j=j+1
    token=get_access_token()
    url="https://"+MUNCHKIN+".mktorest.com/rest/v1/leads.json?access_token="+token
    headers={'content-type': 'application/json'}
    i=i+1
    response=requests.post(url=url,data=json.dumps(params), headers=headers)
    print(response.json()['result'])

 
Now, when we look at our Marketo instance, all the lead values should be reverted back to what they were before the faulty list was uploaded – crisis averted!

pink line

With a deep understanding of the Marketo API, we can find efficient solutions like this to problems that would normally require countless hours of manual cleanup.

So, next time you mistakenly upload a bad list, don’t panic!

Use this guide as a framework to quickly and efficiently reverse those changes, ensuring your SDRs have the most accurate lead information possible.

If you need help with this guide or have any other questions about the Marketo API, you can book a chat with us here.

Recommended: