+ Start a Discussion
Raymond MortuRaymond Mortu 

trigger on a lookup field

Hi, I'm new to triggers.
can someone help me with the below trigger.

NCT Object
  1. if field (Process 8a case) Court process field is NCT 
  2. Change case/query owner to NCT queue
  3. Open an NCT object
  4. Complete client full name in NCT object from case field

this is the where the  field is query owner(owner) located
query owner(owner) which is a lookup field.
should be Queue (1) and the next field should be NCT queque (2)  Below:
User-added image

This is what I did to change the Owner to NCT queue and got an ErrorUser-added image
 
Best Answer chosen by Raymond Mortu
Dev_AryaDev_Arya
Hi Raymond,

Before I help you with the code, I advice you to go through the best practices for the triggers and how to bulkify them. It's really important, a little research will help you a lot.
As a general rule, always avoid SOQL in the for loop. 

Now, here is how you get the ID of the queue, 
trigger t1 on case (before insert, before update){ 

    Group q = [select Id from Group where Name = 'AU Lead Queue' and Type = 'Queue'];
    for(Case c : Trigger.new){
       if(c.Court_Process__c == 'NCT'){
           c.OwnerId = q.Id;
       }
    }
}

and also you can paste the code here via 'Add a code sample' :-). 
User-added image

Mark it gree if it soves your problem.

Cheers,
Dev

All Answers

Dev_AryaDev_Arya
Hi Raymond,

You need first query the record Id of your 'NCT Queue' and then assign that id to owner id field of ur case.
//select Id from Group where Name = 'NCT Queue' and Type = 'Queue'
.
.
cas.OwnerId = queue.id
.
.

Please mark this post green if it helps. Cheers, Dev
Raymond MortuRaymond Mortu
Hi Dev_Arya, Thank you for your help.
i greatly appreciate it.

How do I query the Id of 'NCT Queue'.
 
Raymond MortuRaymond Mortu
This is what i did :(  i'm so new to this .....its fraustrating
User-added image

This is where the Queue and the NCT Queue is:
User-added image
Dev_AryaDev_Arya
Hi Raymond,

Before I help you with the code, I advice you to go through the best practices for the triggers and how to bulkify them. It's really important, a little research will help you a lot.
As a general rule, always avoid SOQL in the for loop. 

Now, here is how you get the ID of the queue, 
trigger t1 on case (before insert, before update){ 

    Group q = [select Id from Group where Name = 'AU Lead Queue' and Type = 'Queue'];
    for(Case c : Trigger.new){
       if(c.Court_Process__c == 'NCT'){
           c.OwnerId = q.Id;
       }
    }
}

and also you can paste the code here via 'Add a code sample' :-). 
User-added image

Mark it gree if it soves your problem.

Cheers,
Dev
This was selected as the best answer