Query extension examples related to accounts
Query extension examples related to accounts
This article describes complex queries using extensions that provide information related to the context of the currently selected account in Salesforce.
Recent emails
This query lists, in chronological order from the most recent to the latest, all emails exchanged between all people connected to the currently selected account.
{{accounts=$type(name:'Account') @title="{!>Name}"}}
{{contacts=$join(fromResultSet: {{accounts}}, toResultSet: $type(name: 'Contact'), field: '@sfaccountid')}}
{{emailAddresses=$valuesOfField(field: '@sfemail', resultSet: {{contacts}})}}
$emailsExchangedWithThoseAddresses(addresses: {{emailAddresses}})
NOT @sysisattachment
$sort(criteria: 'datedescending')
For more information on this type of query, see Anatomy of a complex query using extensions.
Related accounts
This query lists accounts for which the email address of attached contacts share the email domain of the currently selected account.
{{accounts=$type(name:'Account') @title="{!>Name}"}}
{{contacts=$join(fromResultSet: {{accounts}}, toResultSet: $type(name: 'Contact'), field: '@sfaccountid')}}
{{emailDomains=$valuesOfField(field: '@sfemaildomainname', resultSet: {{contacts}})}}
$join(toResultSet: $type(name: 'Account'),
fromResultSet: $joinOnValues(resultSet: @uri,
field: '@sfemaildomainname',
values: {{emailDomains}}),
field: '@sfaccountid')
In more details:
-
Line 1 gets the current account context.
It creates an alias named
accountsonly containing results of typeAccountin which the name contains the name of the currently selected account. -
Line 2 gets the contacts for this account context.
It uses the standard extension
$jointo create an alias namedcontactsonly containing results of typeContactthat share the Salesforce account ID(field: '@sfaccountid')with accounts in theaccountsalias. -
Line 3 gets the email domains for these contacts.
It uses the standard extension
$valuesOfFieldto create an alias namedemailDomainscontaining a list of email domains from the contacts in the contacts alias. -
Lines 4 to 8 get the accounts that share these email domains.
They use the standard extensions
$joinand$joinOnValuesto return only accounts that share the email domains from theemailDomainsalias.
Key colleagues
This query lists co-workers who exchanged emails in connection with the currently selected account.
{{accounts=$type(name:'Account') @title="{!>Name}"}}
{{contacts=$join(fromResultSet: {{accounts}}, toResultSet: $type(name: 'Contact'), field: '@sfaccountid')}}
{{contactEmailAddresses=$valuesOfField(field: '@sfemail', resultSet: {{contacts}})}}
{{emailsExchangedWithAccounts=$emailsExchangedWithThoseAddresses(addresses: {{contactEmailAddresses}})}}
{{emailAddressesInvolvedWithAccounts=$participantsForThoseEmails(emails: {{emailsExchangedWithAccounts}})}}
{{onlyInternalEmailAddresses=$keepMatchingValues(regex: '.*@\Qmyorganization.com\E', values: {{emailAddressesInvolvedWithAccounts}})}}
$joinOnValues(resultSet: $type(name: 'Contact'), field: '@sfemail', values: {{onlyInternalEmailAddresses}})
In more details:
-
Line 1 gets the current account context.
It creates an alias named
accountsonly containing results of typeAccountin which the name contains the name of the currently selected account. -
Line 2 gets the contacts for this account context.
It uses the standard extension
$jointo create an alias namedcontactsonly containing results of typeContactthat share the Salesforce account ID(field: '@sfaccountid')with accounts in the accounts alias. -
Line 3 gets the email addresses for these contacts.
It uses the standard extension
$valuesOfFieldto create an alias namedcontactEmailAddressescontaining a list of emails for the contacts in thecontactsalias. -
Line 4 gets the emails messages exchanged with these email addresses.
It uses the
$emailsExchangedWithThoseAddressesstandard extension to create anemailsExchangedWithAccountsalias containing the email messages exchanged with addresses from thecontactEmailAddressesalias. -
Line 5 gets the participants to these email exchanges.
It uses the
$participantsForThoseEmailsstandard extension to create anemailAddressesInvolvedWithAccountsalias containing the email addresses involved in exchanges with emails from theemailsExchangedWithAccountsalias. -
Line 6 keeps only the internal participants to these email exchanges.
It uses the
$keepMatchingValuesstandard extension to create anonlyInternalEmailAddressesalias only containing internal addresses involved in exchanges with emails from theemailAddressesInvolvedWithAccountsalias.In the regular expression regex
'.*@\Qmyorganization.com\E', you must changemyorganization.comwith your own domain. Use the Java regular expression (REGEX) syntax (see Lesson: Regular Expressions). -
Line 7 gets the names of the internal participants to these email exchanges.
It uses the
$joinOnValuesstandard extension to return contact names for the emails addresses from theonlyInternalEmailAddressesalias.
Key contacts
This query lists external contacts with whom emails were exchanged in connection with the currently selected account.
{{accounts=$type(name:'Account') @title="{!>Name}"}}
{{contacts=$join(fromResultSet: {{accounts}}, toResultSet: $type(name: 'Contact'), field: '@sfaccountid')}}
{{contactEmailAddresses=$valuesOfField(field: '@sfemail', resultSet: {{contacts}})}}
{{emailsExchangedWithAccounts=$emailsExchangedWithThoseAddresses(addresses: {{contactEmailAddresses}})}}
{{emailAddressesInvolvedWithAccounts=$participantsForThoseEmails(emails: {{emailsExchangedWithAccounts}})}}
{{onlyInternalEmailAddresses=$keepMatchingValues(regex: '.*@\Qmyorganization.com\E', values: {{emailAddressesInvolvedWithAccounts}})}}
$joinOnValues(resultSet: $type(name: 'Contact'), field: '@sfemail', values: {{onlyInternalEmailAddresses}})
In more details:
-
Lines 1 to 5 are identical to that of the Key colleagues query.
-
Line 6 only keeps the internal participants to these email exchanges.
It uses the
$removeMatchingValuesstandard extension to create anonlyExternalEmailAddressesalias only containing external addresses involved in exchanges with emails from theemailAddressesInvolvedWithAccountsalias.In the regular expression regex:
'.*@\Qmyorganization.com\E', you must changemyorganization.comby your own domain. Use the Java regular expression (REGEX) syntax (see Lesson: Regular Expressions). -
Line 7 gets the names of the internal participants to these email exchanges.
It uses the
$joinOnValuesstandard extension to return contact names for the emails addresses from theonlyExternalEmailAddressesalias.