Sunday, October 12, 2025

Automating Account & Contact Creation using Agentforce Agent And Using Apex As An Action

Use Case Overview:

Let’s say a customer initiates a chat or call, and the agent (or bot) collects basic details like company name, contact name, and email. Instead of manually creating records or building complex Flow logic, you can invoke a single Apex method from the Agentforce Topic to:

  • Check if the Account already exists

  • Create the Account if needed

  • Create a related Contact

  • Return success or error messages to the agent or bot

Pre-requisite:

Turn On Einstein Setup
Enable Einstein for Sales
Assign below Permission Sets to your user

Prompt Template Manager – for users who create and manage templates

Prompt Template User – for users who run the templates




Let's create a Agentforce Agent now.

Automating Account & Contact Creation using Agentforce Agent And Using Apex As An Action

In next step, un select all selected topics.

Automating Account & Contact Creation using Agentforce Agent And Using Apex As An Action


Automating Account & Contact Creation using Agentforce Agent And Using Apex As An Action

We are not specifying any data source. Click create.

Create a Topic in next step as shown below along with Instructions properly specified and attached an apex action to the Topic.

Name: AccountContactCreationTopic

Classic Description: The AccountContactCreationTopic facilitates real-time creation of Account and Contact records in Salesforce by invoking the AccountContactCreator Apex class.

Scope: Your job is to create Account Contact only

Instructions:

Instruction 1:

Select this topic when the user says or implies any of the following:

“I want to create an account and contact”

“Add a new company and person”

“Register a new customer”

“Create contact for this account”

“Set up a new business record”

This topic should be triggered by bot intent, agent selection, or guided flow when the goal is to create both an Account and a related Contact in Salesforce.

Instruction 2:

Below variables must be collected before invoking the Apex class:

Variable Name Required Description Validation Tip
accountName Yes Name of the Account (company/org) Must not be blank
contactFirstName Yes First name of the Contact Must not be blank
contactLastName Yes Last name of the Contact Must not be blank

Instruction 3:

Optional variables (recommended if available):

Variable Name Required Description
accountPhone No Phone number of the Account
accountWebsite No Website of the Account
contactEmail No Email address of the Contact
contactPhone No Phone number of the Contact

Instruction 4:

Call the action AccountContactCreator for creating account and contact

Instruction 5:

Once the action is successfully executed display the message The Account and Contact have been created successfully. Account ID: {accountId}  andContact ID: {contactId}

Automating Account & Contact Creation using Agentforce Agent And Using Apex As An Action

Action associated to a Topic:


Agentforce Service Agent
Agentforce Service Agent

Agentforce Service Agent
Agentforce Service Agent

Agentforce Service Agent


Apex Class:

public class AccountContactCreator {

    public class AccountContactInput {
        @InvocableVariable(required=true)
        public String accountName;

        @InvocableVariable
        public String accountPhone;

        @InvocableVariable
        public String accountWebsite;

        @InvocableVariable(required=true)
        public String contactFirstName;

        @InvocableVariable(required=true)
        public String contactLastName;

        @InvocableVariable
        public String contactEmail;

        @InvocableVariable
        public String contactPhone;
    }

    public class AccountContactOutput {
        @InvocableVariable
        public String resultMessage;

        @InvocableVariable
        public Id accountId;

        @InvocableVariable
        public Id contactId;
    }

    @InvocableMethod(label='Create Account and Contact' description='Creates Account and associated Contact records')
    public static List<AccountContactOutput> createAccountContact(List<AccountContactInput> inputList) {
        List<AccountContactOutput> results = new List<AccountContactOutput>();

        for (AccountContactInput input : inputList) {
            AccountContactOutput output = new AccountContactOutput();

            try {
                // Check if Account already exists
                List<Account> existingAccount = [SELECT Id FROM Account WHERE Name = :input.accountName LIMIT 1];

                Account account;
                if (existingAccount.size() > 0) {
                    account = existingAccount[0];
                } else {
                    account = new Account(
                        Name = input.accountName,
                        Phone = input.accountPhone,
                        Website = input.accountWebsite
                    );
                    insert account;
                }

                Contact contact = new Contact(
                    FirstName = input.contactFirstName,
                    LastName = input.contactLastName,
                    Email = input.contactEmail,
                    Phone = input.contactPhone,
                    AccountId = account.Id
                );
                insert contact;

                output.resultMessage = 'Account and Contact created successfully.';
                output.accountId = account.Id;
                output.contactId = contact.Id;

            } catch (Exception ex) {
                output.resultMessage = 'Error creating Account/Contact: ' + ex.getMessage();
            }

            results.add(output);
        }

        return results;
    }
}


Now, it is time to Test. Before testing make sure you create a permission set and enable access to Apex Class created above and asssign it to agent associated to your service agent.

Agentforce Service Agent

No comments:

Post a Comment