function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Hussein Azeez 2Hussein Azeez 2 

A Trigger to assign the cases to queues.

Hi, everyone. I wrote a Trigger to assign the cases from Origin "Facebook" and "Web Live Chat" to different queues, but what is happening that the cases from the "Facebook" it assigns correctly but the case from "Web Live Chat" it goes to the user who created it. If someone creates it manually it works well but it doesn't when the case created automatically. Here is the code that used: 
trigger ChangetheCaseOwner on Case (before insert) {
    
    ID HQId = [SELECT Id 
               FROM Group 
               WHERE name=:'HQ'].Id;
    ID HKId = [SELECT Id
               FROM Group
               WHERE name =:'B2C - HK'].Id;
    Id INId = [SELECT Id
               FROM Group
               WHERE name =:'B2C - IN'].Id;
    Id MYId = [SELECT Id
               FROM Group
               WHERE name =:'B2C - MY'].Id;
    Id PHId = [SELECT Id
               FROM Group
               WHERE name =:'B2C - PH'].Id;
    Id SGId = [SELECT Id
               FROM Group
               WHERE name =:'B2C - SG'].Id;
    Id THId = [SELECT Id
               FROM Group
               WHERE name =:'B2C - TH'].Id;

    for(Case c : trigger.new)
    {
        if(c.Origin == 'Facebook') {
            c.OwnerId = HQId;
        } 
        else if (c.Origin =='Web Live Chat' && String.isEmpty(c.Description)) {
            c.OwnerId = HQId;
        }
        else if (c.Origin =='Web Live Chat' && c.Description.contains('Country: India')) {
            c.OwnerId = INId;   
        }
        else if (c.Origin =='Web Live Chat' && c.Description.contains('Country: Thailand')) {
            c.OwnerId = THId;   
        }
        else if (c.Origin =='Web Live Chat' && c.Description.contains('Country: Philippines')) {
            c.OwnerId = PHId;   
        }
        else if (c.Origin =='Web Live Chat' && c.Description.contains('Country: Singapore')) {
            c.OwnerId = SGId;   
        }
        else if (c.Origin =='Web Live Chat' && c.Description.contains('Country: Malaysia')) {
            c.OwnerId = MYId;   
        }
        else if (c.Origin =='Web Live Chat' && c.Description.contains('Country: Hong Kong')) {
            c.OwnerId = HKId;   
        }
        
    }  
}
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Hussein Azeez,

May I suggest you please refer the below link for reference. Hope it will be helpful.

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Thanks
Rahul Kumar
Hussein Azeez 2Hussein Azeez 2
Hi Rahul Kumar,

Thank you for the reply but I already read theses posts before I post the question. 

The Trigger is working properly if the cases created manually, but the cases created automatically from the "Web Live Chat" it goes to the user who created it, while the "Facebook" cases work well. I'm confused about it.
PawanKumarPawanKumar
Please use below and follow bold highighted instructions.

trigger ChangetheCaseOwner on Case (before insert) {
    
    ID HQId = [SELECT Id 
               FROM Group 
               WHERE name=:'HQ'].Id;
    ID HKId = [SELECT Id
               FROM Group
               WHERE name =:'B2C - HK'].Id;
    Id INId = [SELECT Id
               FROM Group
               WHERE name =:'B2C - IN'].Id;
    Id MYId = [SELECT Id
               FROM Group
               WHERE name =:'B2C - MY'].Id;
    Id PHId = [SELECT Id
               FROM Group
               WHERE name =:'B2C - PH'].Id;
    Id SGId = [SELECT Id
               FROM Group
               WHERE name =:'B2C - SG'].Id;
    Id THId = [SELECT Id
               FROM Group
               WHERE name =:'B2C - TH'].Id;

    for(Case c : trigger.new)
    {
        if(c.Origin == 'Facebook') {
            c.OwnerId = HQId;
        } 
        else if (c.Origin =='Web Live Chat' && String.isEmpty(c.Description)) {
            c.OwnerId = HQId;
        }
        else if (c.Origin =='Web Live Chat' && c.Description.contains('Country: India')) {
            c.OwnerId = INId;   
        }
        else if (c.Origin =='Web Live Chat' && c.Description.contains('Country: Thailand')) {
            c.OwnerId = THId;   
        }
        else if (c.Origin =='Web Live Chat' && c.Description.contains('Country: Philippines')) {
            c.OwnerId = PHId;   
        }
        else if (c.Origin =='Web Live Chat' && c.Description.contains('Country: Singapore')) {
            c.OwnerId = SGId;   
        }
        else if (c.Origin =='Web Live Chat' && c.Description.contains('Country: Malaysia')) {
            c.OwnerId = MYId;   
        }
        else if (c.Origin =='Web Live Chat' && c.Description.contains('Country: Hong Kong')) {
            c.OwnerId = HKId;   
        }else{
        // my assumption is when there is no match the record is getting assigned to Created User.
        // To make sure Just create on record to replicate and see below log is generated in Debug log
        // if yes then none of the above condition met and owner assigned to Created User, 
        // i mean the below printed user will be owner
        System.debug('Running User :'+UserInfo.getUserId);

        }
        
    }  
}

Regards,
Pawan Kumar
PawanKumarPawanKumar
Please correct below line before copying above code.
 System.debug('Running User :'+UserInfo.getUserId());
Hussein Azeez 2Hussein Azeez 2
Hi, Pawan Kumar, 

Thank you for the reply. I tested it and when it doesn't meet the any of the conditions it assigns the case to the user who setup the Social Account Service "Service Cloud". But the strange thing when the case form Facebook it works well but anything else it goes to that user.
PawanKumarPawanKumar
Hi Azeez,

Its good to know. Now i am sure either Origin or description is not populated properly when you are not creating it manually. To troubleshoot, please put use below code and see the debug log.

trigger ChangetheCaseOwner on Case (before insert) {
    
    ID HQId = [SELECT Id 
               FROM Group 
               WHERE name=:'HQ'].Id;
    ID HKId = [SELECT Id
               FROM Group
               WHERE name =:'B2C - HK'].Id;
    Id INId = [SELECT Id
               FROM Group
               WHERE name =:'B2C - IN'].Id;
    Id MYId = [SELECT Id
               FROM Group
               WHERE name =:'B2C - MY'].Id;
    Id PHId = [SELECT Id
               FROM Group
               WHERE name =:'B2C - PH'].Id;
    Id SGId = [SELECT Id
               FROM Group
               WHERE name =:'B2C - SG'].Id;
    Id THId = [SELECT Id
               FROM Group
               WHERE name =:'B2C - TH'].Id;

    for(Case c : trigger.new)
    {
    
        // just check all the value printed along with there case sensitivity.
        // this way you can easily come to know why it is going to default block.    
        System.debug('c.Origin:'+c.Origin);
        System.debug('c.Description:'+c.Description);


        if(c.Origin == 'Facebook') {
            c.OwnerId = HQId;
        } 
        else if (c.Origin =='Web Live Chat' && String.isEmpty(c.Description)) {
            c.OwnerId = HQId;
        }
        else if (c.Origin =='Web Live Chat' && c.Description.contains('Country: India')) {
            c.OwnerId = INId;   
        }
        else if (c.Origin =='Web Live Chat' && c.Description.contains('Country: Thailand')) {
            c.OwnerId = THId;   
        }
        else if (c.Origin =='Web Live Chat' && c.Description.contains('Country: Philippines')) {
            c.OwnerId = PHId;   
        }
        else if (c.Origin =='Web Live Chat' && c.Description.contains('Country: Singapore')) {
            c.OwnerId = SGId;   
        }
        else if (c.Origin =='Web Live Chat' && c.Description.contains('Country: Malaysia')) {
            c.OwnerId = MYId;   
        }
        else if (c.Origin =='Web Live Chat' && c.Description.contains('Country: Hong Kong')) {
            c.OwnerId = HKId;   
        }else{
        // my assumption is when there is no match the record is getting assigned to Created User.
        // To make sure Just create on record to replicate and see below log is generated in Debug log
        // if yes then none of the above condition met and owner assigned to Created User, 
        // i mean the below printed user will be owner
        System.debug('Running User :'+UserInfo.getUserId());
        }
        
    }  
}

Regards,
Pawan Kumar