Skip to main content
Join the Agentforce Hackathon on Nov. 18-19 to compete for a $20,000 Grand Prize. Sign up now. Terms apply.
Estimación de tiempo

Create SOQL to Return Data to Apex

Follow Along with Trail Together

Want to follow along with an expert as you work through this step? Take a look at this video, part of the Trail Together series.

(This clip starts at the 24:15 minute mark, in case you want to rewind and watch the beginning of the step again.)

Create a Query

Create a SOQL query to retrieve the Recruiting Account record that you created.

  1. In the Developer Console, click File | Open Resource.
  2. Select Account.obj and then click Open.
  3. Select Id and Name. Use CTRL (in Windows) or CMD (on a Mac) to select multiple fields.
  4. Click Query.
  5. If the Query Editor displays instructions for building a query, click Query again.
  6. At the end of the query, type WHERE Name = 'Recruiting'.
  7. Verify that your SOQL query reads:
    SELECT Id, Name FROM Account WHERE Name = 'Recruiting'
  8. Click Execute to execute the query and display the results.

To associate the newly created Contact with the Recruiting Account, add the SOQL query to the createContact method.

  1. In the CreateContactFromCan class, replace the existing code with this code:
    public with sharing class CreateContactFromCan {
        //Declare a method that returns void and accepts a Candidate list named candsFromTrigger
        public static void createContact (List<Candidate__c> candsFromTrigger) {
            //Select the Recruiting account from the database and add it to a list
            //named candAccts (from the Account class)
            List<Account> candAccts = [SELECT Id, Name FROM Account WHERE Name = 'Recruiting'];
            //Instantiate a Contact list named conList
            List<Contact> conList = new List<Contact>();
            //Declare a FOR list loop that uses an iteration variable named currentCandidate
            //to loop through the candsFromTrigger list
            for(Candidate__c currentCandidate:candsFromTrigger) {
                //Create a Contact and add it to conList
                //Set the FirstName, LastName, and Email attributes to match the
                //corresponding attributes of the currentCandidate object (First_Name__c,
                //Last_Name__c, and Email__c)
                conList.add(new Contact(
                    AccountId = candAccts[0].Id,
                    FirstName = currentCandidate.First_Name__c,
                    LastName = currentCandidate.Last_Name__c,
                    Email = currentCandidate.Email__c)
                );
            }
            //Save conList to the database
            Database.insert(conList);
        }
    }
  2. Save the CreateContactFromCan class.
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