Skip to main content
Responda nuestra encuesta de la comunidad en 10 minutos. Disponible ahora y hasta el 19 de julio. Haga clic aquí para participar.

Build Using Vibes Coding

Learning Objectives

After completing this unit, you’ll be able to:

  • Vibe code an anonymous Apex script to update a contact.
  • Use the Quick Fix menu options on the generated Apex code.

Vibe Code an Anonymous Apex Script

An anonymous Apex script is a block of code that you can compile and run without storing its metadata. In this unit, you use vibe coding to get the AI agent to create an anonymous Apex script for you. Make sure that you are in the DX project that you created in the last unit.

Case Scenario

Your data team just imported a legacy list of business contacts, but many of them are missing individual email addresses or contain old, broken data. To ensure your automated system doesn’t trigger bounced emails, you need to find all contacts associated with any corporate account and temporarily route their digital correspondence to a generic inbox matching their specific company’s name.

You want to build and execute an anonymous Apex script via Agentforce Vibes to scan your database, read each contact’s associated company name, and update their email address to a standardized format of info@[companyname].com (with spaces removed). Here’s how to do it.

Build a Prompt

Your prompt must include action, context, and any best practices.

  • Action: Create an anonymous Apex block to loop through Contacts and their company names.
  • Context: Change the individual email addresses to 'info@' plus their company name plus '.com'.
  • Best practice: Remove any white spaces.

The complete prompt is going to be: Create an anonymous Apex block to loop through Contacts and their company names. Change the individual email addresses to 'info@' plus their company name plus '.com'. Remove any white spaces.

Enter this prompt in the Agentforce Vibes chat window and press Enter.

Review the generated Apex code, which should look similar to this code snippet.

// Anonymous Apex: Update Contact Email based on associated Account (Company) name.
// Email format: info@<CompanyNameNoSpaces>.com
// Only processes Contacts that have an associated Account.
List<Contact> contactsToUpdate = new List<Contact>();
for (Contact c : [
   SELECT Id, Email, Account.Name
   FROM Contact
   WHERE AccountId != NULL
]) {
   String companyName = c.Account.Name.replaceAll(' ', '');
   c.Email = 'info@'+ companyName + '.com';
   contactsToUpdate.add(c);
}
if (!contactsToUpdate.isEmpty()) {
   update contactsToUpdate;
   System.debug('Updated '+ contactsToUpdate.size() + 'Contact email(s).');
} else {
   System.debug('No Contacts with an associated Account were found.');
}

Edit the Apex Script Using Inline Autocomplete

Inline autocomplete is an AI-powered editor feature of Agentforce Vibes IDE that predicts and suggests the next part of your code in real-time as you type. Instead of just suggesting a single variable or method name like traditional autocomplete, inline autocomplete looks at the context of your active code file to predict whole lines, arguments, or even entire blocks of code. The suggestion usually appears right ahead of your cursor as grayed-out text. If you like what the AI suggests, you can accept it instantly by pressing the Tab key.

An example of inline autocomplete happens right inside that for loop in your anonymous Apex script where you are constructing the email address string and adding it to the list. As you begin to type, the AI reviews the context of your SOQL query and the variables you’ve initialized above. Here’s how that prediction appears in context.

Inline autocomplete predicts a line of code as it’s being typed.

Run the Apex Code

You can run your Apex code by clicking Execute at the top of the code block as shown in this image or by asking the agent to run it for you by typing a prompt in the chat window.

A segment of an anonymous Apex script with Execute link highlighted.

Let’s ask the agent to run the code. Type Run the open anonymous Apex code. Then press Enter.

Review the changes to the contact emails on your org.

Quick Fix Menu

Think of the Quick Fix menu as your editor’s built-in code medic. Whenever you accidentally make a typo, forget to declare a variable, or write something that breaks a coding rule, the IDE flags it with a red or yellow squiggly underline to let you know something is wrong. Instead of leaving you to guess how to fix it, when you click on the flagged text, a lightbulb icon pops up. You can click that bulb—or just type Command+. on a Mac or Ctrl+. on Windows—to open up the Quick Fix menu.

Use the Quick Fix menu to:

  • Add to Agentforce: Shortcut that instantly sends a piece of code over to your main AI assistant panel so you can talk about it
  • Explain with Agentforce: Context-aware feature that instantly breaks down what a specific block of code does in plain English
  • Improve with Agentforce: Feature that automatically optimizes, cleans up, or refactors your existing code

Wrap It Up

You’ve learned the basics of vibe coding by using AI prompts to create an anonymous Apex script and editing your code with inline autocomplete. You also explored how to use the Quick Fix menu to troubleshoot and refine your work. In the next unit, you learn how to use the SOQL Builder to construct and optimize your database queries.

Resources

Comparta sus comentarios de Trailhead en la Ayuda de Salesforce.

Nos encantaría saber más sobre su experiencia con Trailhead. Ahora puede acceder al nuevo formulario de comentarios en cualquier momento en el sitio de Ayuda de Salesforce.

Más información Continuar a Compartir comentarios