function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Denise Neal 2Denise Neal 2 

For this project:Quick Start: Apex Coding for Admins

I have gotten all the way to Create a Trigger, and I get:

Couldn’t find the record named 'Nina Simone' with Email 'nsimone@test.com'. Please double check the instructions.

When I try to create the candidate, I get:

CreateContact: execution of AfterInsert caused by: System.QueryException: List has more than 1 row for assignment to SObject Class.CreateContactFromCan.createContact: line 5, column 1 Trigger.CreateContact: line 5, column 1   

 
MenteeMentee
can you check do you have permissions to view the record. 
Denise Neal 2Denise Neal 2
This is a brand new playground and I am the administrator, wouldn't I have access? it says the record is not created. 
 
MenteeMentee
past the code here. 
Assumption: inserion happens but due to some issue in after insert event,  transaction rolls back. 
Denise Neal 2Denise Neal 2
There are 3 parts of code involved, the trigger, and the Appex Classes, this should be pretty straight forward, not sure how I messed it up, you put it in workbench then it has the code for you to copy as you go. This is to create a contact from a candidate form. When I attempt to create the candidate is when I have the error.  

****BankAcct
public class BankAcct {
    private integer balance=0;
    public string acctName;
    //Declare a public string attribute called accttype
    public string accttype;
    public void makeDeposit (integer deposit) {
        balance = balance + deposit;
    }
    //Declare a method named getBalance that returns an integer and no input parameter
    public integer getBalance() {
        //Return the balance attribute
        return balance;
    }
}

***For CreateContactFromCan
public with sharing class CreateContactFromCan {
   //Declare a method that does not return anything and takes one input parameter of a Candidate list object called candsFromTrigger
   public void createContact (List<Candidate__c> candsFromTrigger){
       //Select the Recruiting record from the database and assign to an object called candAcct from the sObject Account class
       Account candAcct = [SELECT a.Id FROM Account a WHERE a.Name = 'Recruiting'];
       //Instantiate a Contact list object from the List class called conList
       List<Contact> conList = new List<Contact>();
       //Declare a For list loop to loop through the input parameter list candsFromTrigger with an iterationvariable called currentCandidate
       for(Candidate__c currentCandidate:candsFromTrigger){
           //Instantiate an object called con from the sObject class contact
           Contact con = new Contact();
           //Set the attribute AccountID of the con object to the value of the Id attribute of the candAcct object
           con.AccountId = candAcct.Id;
           //Set the attribute Firstname of the con object to the value of the First_Name__c attribute of the currentCandidate object
           con.FirstName = currentCandidate.First_Name__c;
           //Set the attribute Lastname of the con object to the value of the Last_Name__c attribute of the currentCandidate object
           con.LastName = currentCandidate.Last_Name__c;
           //Set the attribute Email of the con object to the value of the Email__c attribute of the currentCandidate object
           con.Email = currentCandidate.Email__c;
           //Add the con object to the conList
           conList.add(con);
       }
       //Persist the conList to the database
       Database.insert(conList);
   }
}

****And the Trigger on Candidates 
trigger CreateContact on Candidate__c (after insert){
    //Instantiate an object called cc from the class CreateContactFromCan
    CreateContactFromCan cc = new CreateContactFromCan();
    //Invoke the method createContact and send a List of Candidates as an input parameter
    cc.createContact(Trigger.new);
}
 
MenteeMentee
List<Account> candAcct = [SELECT Id FROM Account WHERE Name = 'Recruiting']; is returning more than one record. you need to iterate the value and then assign the account. Can you tell me what is the your aim? with assumptions I wrote some hope it helps.
 
public with sharing class CreateContactFromCan {

    /*  write what is this method doing here
    * single comments doesn't look good
    */
   public void createContact (List<Candidate__c> newCandidateList){
       List<Contact> conList = new List<Contact>();
      // Account candAcct = [SELECT a.Id FROM Account a WHERE a.Name = 'Recruiting'];
       Account candAcct = [SELECT Id FROM Account WHERE Name = 'Recruiting'][0];
            for(Candidate__c currCandidate:newCandidateList){
                Contact con = new Contact();
                con.AccountId = candAcct.Id;
                con.FirstName = currentCandidate.First_Name__c;
                con.LastName = currentCandidate.Last_Name__c;
                con.Email = currentCandidate.Email__c;
                conList.add(con);
            }
       insert(conList);
   }
}

 
Denise Neal 2Denise Neal 2
The code as I posted it, is exactly as in the trailhead project and my collegue made it work, so there is possibly something else I did or did not do. The trigger is supposed to create a contact from my creating a candidate. 
MenteeMentee
ok! so meant you got help ? if not send me the requirement
Denise Neal 2Denise Neal 2
Thank you for trying to help, I knew the code was correct as the project was proving the code every step of the way. I did, however, determine (with the help of my collegue) that I must have been doing this step: Use DML to Persist  sObjects to the Database, twice as it was returning 2 accounts that were recruiting so when I ran my trigger it was bringing back the error, so I deleted one account and checked the project again and it went through.
MenteeMentee
Good that it helped and what I was telling you is correct. It is always safe to get(0) from a list. unless it is Id.
Abhishek Shukla 97Abhishek Shukla 97
Hello @Denise Neal2, 

Nice posting here. Saved life. 

Thanks 
Abhishek Shukla
New ComerNew Comer
You can even limit the record to 1 by modifyly the code as,
Account candAcct = [SELECT Id FROM Account WHERE Name = 'Recruiting' LIMIT 1];
Add LIMIT 1 at the end in class CreateContactFromCan.