1

I'm declarative developer trying to make this work: I am trying to update the case owner to a queuename based on status. Here is my code and the error I'm getting below. Any help would be appreciated!

    trigger CRME_Case_ReOpen_OOO on Case (before update) {            
        for(case c :trigger.new){       

          if(c.OwnerOOO__c == TRUE &&  c.Status == 'Working' ) 

                c.OwnerID =  [select Id from User where Name = :c.QueueNameRef__c].Id;              
                }                                    
   }

Error:Apex trigger CRME_Case_ReOpen_OOO caused an unexpected exception, contact your administrator: CRME_Case_ReOpen_OOO: execution of BeforeUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.CRME_Case_ReOpen_OOO: line 14, column 1

9
  • Can we see your test class?
    – EricSSH
    Commented Apr 28, 2014 at 18:19
  • 2
    @user8129 This task can be accomplished with a workflow rule and a field update action. Is there an unsaid reason you're doing this with a trigger?
    – Mark Pond
    Commented Apr 28, 2014 at 18:21
  • your query isnt returning any records
    – Phil B
    Commented Apr 28, 2014 at 18:47
  • 1
    Your query doesn't doesn't appear to bring back any results. Is that field properly filled out? BTW, you never want to do a query inside a loop anyways. You should query for it outside the loop and assign via a map.
    – dphil
    Commented Apr 28, 2014 at 18:54
  • @user8129 I tried an Workflow, but Field Updates doesn't allow owners to be updated from a variable, so I'd need to code for owner as a variable which I have stored in c.QueueNameRef. Or is there a way I can do it?
    – user8129
    Commented Apr 28, 2014 at 22:20

1 Answer 1

4

In case you can't find a workflow solution...

This is a more defensive version of the trigger that also avoids the query inside a loop problem (that can cause the 100 SOQL queries governor limit to be exceeded). But as the comments mention, if there are inconsistent data values they also need fixing.

trigger CRME_Case_ReOpen_OOO on Case (before update) {  
    Set<String> names = new Set<String>();          
    for (Case c : Trigger.new) {       
        if (c.OwnerOOO__c && c.Status == 'Working' && c.QueueNameRef__c != null) {
            names.add(c.QueueNameRef__c);
        }
    }
    if (names.size() > 0) {
        Map<String, Id> m = new Map<String, Id>();
        for (User u : [
                select Id, Name
                from User
                where Name in :names
                order by Name
                ]) {
            m.put(u.Name, u.Id);
        }
        for (Case c : Trigger.new) {       
            if (c.OwnerOOO__c && c.Status == 'Working' && c.QueueNameRef__c != null) {
                Id id = m.get(c.QueueNameRef__c);
                if (id != null) {
                    c.OwnerID = id;
                }
            }
        }
    }
}
5
  • Thank you Keith.... I just copy/pasted your code unchanged and it's giving a compile error on this line:
    – user8129
    Commented Apr 28, 2014 at 22:35
  • if (names.length() > 0) { Error: Compile Error: Method does not exist or incorrect signature: [SET<String>].length() at line 8 column 9
    – user8129
    Commented Apr 28, 2014 at 22:36
  • @user8129 Change it to names.size().
    – Keith C
    Commented Apr 28, 2014 at 22:44
  • if (names.size()> 0) ... --but user8129 you should have been able to figure this out that Set collections support the size() method, not the length() method. I'm sure that Keith C, who is exceptionally helpful to 'thousands' just made a simple error while trying to help you expeditiously
    – cropredy
    Commented Apr 28, 2014 at 22:44
  • @keithc thanks to both. The size made it save OK. (remember I'm a declarative programmer :-( don't know much. I'll start testing and see how far I get. Thanks to all again!
    – user8129
    Commented Apr 28, 2014 at 22:46

You must log in to answer this question.

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