1

I'm getting an odd error when trying to pull a value in a list. I know the error is due to no rows however it fires the exception on row 0 which is odd to me. Any help would be great.

Here is the error I'm getting

System.DmlException: Update failed. First exception on row 0 with id a0f2F000000ElPBQA0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, HomeDepotStore: execution of BeforeUpdate

caused by: System.QueryException: List has no rows for assignment to SObject Class.HomeDepotStoreHandler.beforeUpdate: line 6, column 1

Class.TriggerHandler.run: line 41, column 1 Trigger.HomeDepotStore: line 2, column 1: []

Here is my Trigger

trigger HomeDepotStore on i360__Sale__c(before insert, before update) {
new HomeDepotStoreHandler().run();
}

Here is my Trigger Handler Class

public class HomeDepotStoreHandler extends TriggerHandler {   
public HomeDepotStoreHandler() {}

public override void beforeUpdate() {
i360__Sale__c a = new i360__Sale__c();
List <i360__Staff__c> staffid = [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 = staffid.get(0).Id;
}
System.debug(LoggingLevel.INFO, 'HD Base Store Our System:' + 
a.HD_Base_Store_Our_System__c);
}
}

Here is my Test Class

@isTest
private class HomeDepotStoreTest {
static testMethod void TestHomeDepotStores() {
   i360__Staff__c staff = new i360__Staff__c();
   staff.Name = '2036';
   staff.i360__Start__c = Date.today();
   insert staff;
    
   i360__Staff__c salesrep = new i360__Staff__c();
   salesrep.Name = 'Mike Levitzke';
   salesrep.i360__Start__c = Date.today();
   salesrep.i360__Position_Title__c = 'Sales Res';
   insert salesrep; 
    
   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__Marketing_Source__c source = new i360__Marketing_Source__c();
   source.i360__Type__c = 'Amazon Services';
   source.i360__Start__c = Date.today();
   source.i360__End__c = Date.today().addYears(1); 
   insert source;
       
   i360__Lead_Source__c leadsource = new i360__Lead_Source__c();
   leadsource.i360__Source__c = source.Id;
   leadsource.i360__Prospect__c = Pros.Id;
   leadsource.i360__Taken_On__c = Date.today();
   insert leadsource;   
    
   i360__Appointment__c App = new i360__Appointment__c();
   App.Name = '2036';
   App.i360__Prospect__c = Pros.Id; 
   App.i360__Lead_Source__c = leadsource.Id;
   App.i360__Start__c = Date.today();
   App.i360__Sales_Rep_1__c = salesrep.Id;
   insert App;
    
   i360__Sale__c a = new i360__Sale__c();
   a.Name ='2036';
   a.i360__Appointment__c = App.Id;
   insert a;
    
   a.i360__Email_Address__c = '[email protected]';
   update a;
}
}
3
  • where i360__Staff__c.Name =: a.Referral_Store__c this looks suspect, a is a new record that hasn't been inserted yet, what are you expecting a.Referral_Store__c to be at this point in the code? Commented Aug 28, 2017 at 21:46
  • @BrianMansfield I wasn't sure what to do to refer to the referral store on the sale object. As it sent back an error when I did it as "i360__Sale__c.Referral_Store__c". I'll give it another shot and see if I can reproduce the error. Commented Aug 28, 2017 at 21:51
  • I'll see if I can put you on the right track for the bulk thing too... Commented Aug 28, 2017 at 21:53

1 Answer 1

3

This trigger is not bulkified so is not going to work in many cases.

However, to fix this immediate issue, do not create a new i360_Sale__c object - you want the trigger object, which is of this type (you'll need to cast it though)

Try this:

i360__Sale__c[] sales = (i360__Sale__c[])Trigger.new;
i360__Sale__c a = sales[0];

NOTE THIS IS NOT FIXING YOUR BULK PROBLEM.

The bulk problem:

First, get all your referral store ids and put them in a set:

Set<String> referralStoreNames = new Set<String>();
for (i360__Sale__c sale : sales){
  referralStoreNames.add(a.Referral_Store__c);
}

Then, query the referral items in the set:

i360__Staff__c[] staffMembers = [SELECT Id, Name 
                                 FROM i360__Staff__c 
                                 WHERE Name IN :referralStoreNames];

Then map by name:

Map<String,i360__Staff__c> staffMap = new Map<String,i360__Staff__c>();
for (i360__Staff__c staffMember : staffMembers){
  staffMap.put(staffMember.Name,staffMember);
}

Finally you can fill in the id at the end of your trigger (I'm going to assume you have made a loop for this:

if (a.HD_Base_Store_Our_System__c == null) {
  //you will need to null check this;
  a.HD_Base_Store_Our_System__c = staffMap.get(a.Referral_Store__c).Id;
}
7
  • No worries - check the updated answer which shows you how to bulkify the trigger. Commented Aug 28, 2017 at 22:06
  • Also, would you know the proper way of getting the TriggerHandler code coverage to be above the 75% threshold? It's the opensource handler at github.com/kevinohara80/sfdc-trigger-framework. I'm currently at 29%. Commented Aug 28, 2017 at 22:53
  • You should probably put that in a new question. Have you used the code coverage tool in the developer console? You run your test, then open the handler class up and it highlights what you are missing. At 29% it's probably not really getting in the trigger much at all. Also, when running your unit tests, look at your debug log in the developer console - it'll log any errors automatically. Commented Aug 28, 2017 at 22:57
  • I am using the code coverage tool. On my actual HomeDepotStoreHandler class I have 100% code coverage. However on the 3rd party open source "TriggerHandler" class that my class refers to I have 29% and that stops me from deploying it. Commented Aug 28, 2017 at 23:03
  • You should also be using his test method that he supplies with that class - it's in the same github repo Commented Aug 28, 2017 at 23:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .