Skip to main content Stream TDX Bengaluru on Salesforce+. Start learning the critical skills you need to build and deploy trusted autonomous agents with Agentforce. Register for free.

Hi Team,

I'm getting an error message on saving the trigger in step 6. I have attached the screenshot. Please help.

Create the CreateContact trigger to invoke the createContact method.

  1. In the Developer Console, click File | New | Apex Trigger. The New Apex Trigger window opens.
  2. For Name, type CreateContact.
  3. For sObject, select Candidate__c.
  4. Click Submit.
  5. Replace the existing code with this code:

trigger CreateContact on Candidate__c (after insert){

/* Invoke the createContact method with a list of Candidates as the argument

to create a corresponding Contact from each new Candidate Record */

CreateContactFromCan.createContact(Trigger.new);

}

6. Save the trigger.

14 answers
  1. Jul 19, 2021, 10:23 AM

    @aayush you have made huge mistake on this line your account is stored in candAccts  but you are using conList to get the Id please change the line below I have updated the conList with CandAccts

     

    conList.add(new Contact (AccountId = candAccts[0].id, 

     

    And I am sorry I also didn't see this error when you last commented on your code as I thought

    the issue is because of before trigger. basically, your account with the name 'Recuriting' is in the list CandAccts but you are using Conlist which is an issue 

     

    The method should be like this 

     

    public static void createContact (List<Candidate__c> candsFromTrigger){

    List <Account> candAccts = [SELECT Id, Name FROM Account WHERE Name = 'Recruiting'];

    List<Contact> conList = new List<Contact>();

    if(candAccts.size()>0){

    for(Candidate__c currentCandidate:candsFromTrigger){

    List.add(new Contact (AccountId = candAccts[0].id,

    FirstName = currentCandidate.First_Name__c, LastName = currentCandidate.Last_Name__c, Email = currentCandidate.Email__c

    ));

    }

    Database.insert(conList);

    }

    }

     

    LET me know if the error still happens

Loading
0/9000