I'm having issues creating an Apex Trigger, I'm fairly new to the salesforce platform. I'm attempting to auto-populate a lookup field that refers back to the staff page.

trigger HomeDepotStore on i360__Sale__c(before insert, before update) {
for (i360__Sale__c a: Trigger.new) {
   list<i360__Staff__c> stafflist = [select id from i360__Staff__c where i360__Staff__c.Name =:a.Referral_Store__c];
    if (a.HD_Base_Store_Our_System__c == null) {
        a.HD_Base_Store_Our_System__c = staffList[0].id;

Each store has its own Staff account with its own ID. There is already a field a.Referral_Store__c that has the store number filled out. The idea is to select the id of the staff that has a Name that is equal to that Referral Store variable. My current issue is that when I run it against my Apex Test Class I receive no "System.QueryException: List has no rows for assignment to SObject." I'll also include my Apex test class below. Any help would be greatly appreciated as I feel like I'm missing something incredibly simple.

        private class HomeDepotStoreTest {
            static testMethod void TestHomeDepotStores() {
                i360__Prospect__c Pros = new i360__Prospect__c();
                Pros.Name = 'Bob Joe';
                Pros.Referral_Store__c = '2036';
                Pros.i360__Phone_1__c = '(123) 123-1234';
                Pros.i360__Primary_Last_Name__c = 'TESTMCTEST';
                insert Pros;
                i360__Appointment__c App = new i360__Appointment__c();
                App.Name = '2036';
                App.i360__Prospect__c = [SELECT i360__Prospect__c.i360__Prospect_Id_String__c FROM i360__Prospect__c WHERE i360__Prospect__c.Name = : ('2036')].Id;
                i360__Sale__c a = new i360__Sale__c();
                a.Name = '2036';
                insert a;
                i360__Staff__c staff = new i360__Staff__c();
                staff.Name = '2036';
                staff.Id = 'a0j0a00000ADi2yAAD';
                insert staff;

I made a few tweaks and took your suggestions on how to write a proper Apex Trigger. Thanks so much guys!

public class HomeDepotStoreHandler extends TriggerHandler {    
/* context overrides */
public Id getStaffid() {
    i360__Sale__c sa = new i360__Sale__c();
    List <i360__Staff__c> staffid = [select id from i360__Staff__c where i360__Staff__c.Name 
                 =: sa.Referral_Store__c];
    return staffid[0].Id;
   protected override void beforeUpdate() {
i360__Sale__c a = new i360__Sale__c();
if (a.HD_Base_Store_Our_System__c == null) {
    a.HD_Base_Store_Our_System__c = getStaffid();

There are a lot of problems with the trigger and the test class, but to answer the scope of this question:

Your issue is that you're inserting the i360__Sale__c record (which causes the trigger to run) before you insert the i360__Staff__c record, so when the query is executed in your trigger, there is no match for the 2036 value. Basically this is what's happening, in order:

  1. sale record is inserted
  2. trigger runs, queries for staff record in database, finds no match, throws exception

I would advise first modifying your trigger code to follow best practices (Adrian linked) before fixing the QueryException. I am also fairly certain that once you fix the QueryException, this line is going to throw a different exception:

staff.Id = 'a0j0a00000ADi2yAAD';

This should be ommitted altogether.

One other thing, because it will save you time - this line:

App.i360__Prospect__c = [SELECT i360__Prospect__c.i360__Prospect_Id_String__c FROM i360__Prospect__c WHERE i360__Prospect__c.Name = : ('2036')].Id;

can be changed to App.i360__Prospect__c = Pros.Id;

Once a record has been inserted, its Id has been generated, and you can set the relationship directly from the variable.

| improve this answer | |
  • Definitely should've cleaned it up slightly before putting it on here. I was getting odd errors so I started putting nonsense code to see if something stuck. Thanks for pointing out the error with the staff though. – Nathan White Aug 25 '17 at 14:50
  • Oh, and thanks for the heads up on the ID, I wasn't sure whether in a test environment that it was created or not. – Nathan White Aug 25 '17 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.