How do I use Scheduled Tasks to automatically create a Contact Group for every account in Autotask, and then automatically ensure that all contacts for that account are added to the account’s contact group?
Hi @AliM ! Thanks for posting this here.
I would add a new User-Defined Field in Autotask to the “Account” entities, called “Auto-Assigned Contact Group ID” (or similar). Once you have that, create two different Scheduled Tasks in MSPintegrations.
Task #1: Ensure all Accounts have a Contact Group**
This task will iterate over all accounts (or a limited set if you only this on some accounts) to ensure that each one has a contact group.
Step 1. API: Query for Multiple Objects: Retrieve all Accounts
- Set the criteria to match all the accounts you want to create contact groups for.
- Set the results variable to
custom.accounts
Step 2. Iterate through multiple items in an array
-
Iterate over all
custom.accounts
and store the current account incustom.account
.Step 2.1. API: Create An Object: Create a new Contact Group if needed
- This step should only fire if the account UDF
Auto-Assigned Contact Group ID
is empty. - Create a new
Contact Group
- I recommend the name be similar to:
{{custom.account.AccountName}} [Automatically Maintained]
- Store the result in
custom.contactgroup
Step 2.2. API: Update An Object Add Contact Group ID to Account if created
- This step should only fire if
{{custom.contactgroup.id}}
is not empty. - Update the Account
{{custom.account.id}}
and set theAuto-Assigned Contact Group ID
to{{custom.contactgroup.id}}
- This step should only fire if the account UDF
You could add extra logic to this task to rename the contact group if the name doesn’t match the account, as well.
Task #2: Ensure all Contacts are associated to Contact Group**
This task will iterate over all accounts with a contact group id stored in the Auto-Assigned Contact Group ID
UDF. For each account, it will query for all contacts and ensure each is associated to the contact group. This task will some use some creativity to limit the number of Autotask API calls to query for contact group associations.
Step 1. API: Query for Multiple Objects: Retrieve all Accounts
- Set the criteria to match all the accounts where the UDF
Auto-Assigned Contact Group ID
is not empty. - Set the results variable to
custom.accounts
Step 2. Iterate through multiple items in an array
-
Iterate over all
custom.accounts
and store the current account incustom.account
.Step 2.1. API: Query for Multiple Objects: Retrieve all Contact Group Contacts
- Set the criteria to match all the contact group contacts where
ContactGroupID
={{custom.account.[UDF:Auto-Assigned Contact Group ID]}}
. - Set the results variable to
custom.contactgroupcontacts
Step 2.2. Render Text and Store as Variable: Generate A Table of all Contact Group Contacts
- This is where the magic happens. You will create a new string that contains a list of all of the contacts that are already in the contact group.
- The text field should be:
,{{implode custom.contactgroupcontacts glue="," property_name="ContactID"}},
[NOTE there are commas before and after the curly-braces]
This will render a string like,123,456,789,111,222,333,
- Set the results variable to
custom.contactids
Step 2.3. API: Query for Multiple Objects: Retrieve all Contacts
- Set the criteria to match all the contacts where
AccountID
={{custom.account.id}}
and whereActive
=1
. - Set the results variable to
custom.contacts
Step 2.4. Iterate through multiple items in an array
-
Iterate over all
custom.contacts
and store the current contacts incustom.contact
.Step 2.4.1. Render Text and Store as Variable: Render current contact ID surrounded by commas
- The text field should be:
,{{custom.contact.id}},
[NOTE there are commas before and after the curly-braces]
This will render a string like,123,
- Set the results variable to
custom.contactidwithcomma
Step 2.4.2. API: Create an Object: Add Contact to Contact Group if needed
- This is the second half of where the magic happens. This will only fire if
custom.contactids
doesn’t containcustom.contactidwithcomma
- In the expression field for this step, hold SHIFT and press the button to add a new stanza, and then enter
!(custom.contactids contains custom.contactidwithcomma)
- This step will create a
Contact Group Contact
to associate the Contact to the Contact Group.
- The text field should be:
- Set the criteria to match all the contact group contacts where
Whew! That’s a lot of steps. Please let me know if you hit any snags or something doesn’t add up - I very well may have missed or overlooked something.
Also, if this works but you don’t understand the purpose of something I wrote, please ask. I’d be happy to explain the how and why behind the logic.
@AliM ,
I saw you had a few questions that you removed. Is it safe to assume you got this setup successfully? How’s it working for you?
Yes I figured it out, thanks!
Excellent! Thanks for confirming.