0

enter image description hereHi All, I recently refreshed my sandbox for the first time and now im suddenly getting this error when i try to create a new case. (dont know if issue is related to the refresh) we call cases "requests" at ouor company so Request=Case on these screenshots. The error says line 66 but i dont understand what the code says on line 66. any help is much appreciated. Thanks!

1
  • 2
    You need to view the trigger in question to see what the code is on line 66. If you paste it here perhaps someone can help you
    – mkorman
    Commented Apr 29, 2016 at 22:56

2 Answers 2

3

If you open your CaseBefore trigger at line 66, you will see something like:

object obj = [select id, name from object where condition 1];

You query doesn't return any list, that's why you have an exception. try to modify to

list<object> objlist = [select id, name from object where condition 1];
if (!objlist.isEmpty() ){
//...
1
  • 2
    And the worst thing, that you have this code in your production... Commented Apr 29, 2016 at 22:59
1

This error is caused by one of two reasons, either of which should be addressed.

Not Bulkified

Code that isn't bulkified often has a query in the middle of a loop. For example, perhaps you want the contact that belongs to the case, and your code was written like this:

for(Case record: Trigger.new) {
  Contact aPerson = [SELECT Name FROM Contact WHERE Id = :record.ContactId];
  // Blah blah blah
}

If the field is null, you'd get an error. Instead, the appropriate alternative is to build a map, query the records, then use the query results:

Map<Id, Contact> contacts = new Map<Id, Contact>();
for(Case record: Trigger.new) {
  contacts.put(record.ContactId, null);
}
contacts.putAll([SELECT Name FROM Contact WHERE Id IN :contacts.keySet()]);
for(Case record: Trigger.new) {
  Contact aPerson = contacts.get(record.ContactId);
  if(aPerson != null) {
    // Blah blah blah
  }
}

Missing Expected Data

Sometimes, records get deleted without thinking about the consequences, or renamed, duplicated, whatever. Sometimes this is a critical problem, and sometimes it doesn't matter. Whatever your goal is, you should stick with a normal try-catch block:

Contact designatedHelper;
try {
  designatedHelper = [SELECT Name FROM Contact WHERE DesignatedHelper__c = TRUE];
} catch(QueryException e) {
  // report error or ignore, depending on your needs
}
if(designatedHelper != null) {
  // Blah blah blah
}

Only use this pattern if you literally only ever need one of a particular record no matter how big/small your transaction size is. For example, we have a master account that all of our sales locations are children of, so we query that master account and cache it in our code for everywhere we use it. We use a framework that automatically notifies us by Chatter if something goes wrong.

One common pattern I see (even the other answer had this), was to use a list and take the first element if it's not empty:

List<Contact> contacts = [SELECT Name FROM Contact WHERE DesignatedHelper__c = TRUE];
if(!contacts.isEmpty()) {
  // Blah blah blah
}

This is generally a Bad Idea. The reason why you don't want to do this is that you cannot guarantee that the record you'll find, if any, is actually the one you want (or even one of several).

If you suspect that your data may, at some point, be incorrect, it's best to fix the problem, error for the user, or alert an administrator. The error message you've received is obviously undesirable, as the exception should be handled, but the precise fix will depend on the business rules.

You must log in to answer this question.

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