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
DeveloperDeveloper 

Please create a Trigger on Case that if Owner is external Community User (Profile=Customer Community User) then the Checkbox "assign using active assignment Rules" should be set to active

please help me on this...........................



Thaks in Advance ......
Best Answer chosen by Developer
BALAJI CHBALAJI CH
Hi Gopal,

You can try below Trigger as per your requirement.
trigger CaseAssignmentTrigger on Case (after insert) {
    
    set<id> caseids = new Set<id>();
    list<case> CaseList = new list<case>();
    for(Case c : trigger.New)
    {
        caseids.add(c.id);
    }
    
    for(Case c: [Select id, Owner.Profile.Name from Case where Id IN:caseids])
    {
        if(c.owner.Profile.Name == 'Customer Community User')
        {
            CaseList.add(c);
        }
    }
    if(CaseList.size() > 0)
    {
    Database.DMLOptions dmo = new Database.DMLOptions();
    dmo.assignmentRuleHeader.useDefaultRule = true;
    Database.update(caseList, dmo);
    }
}
You need to have a Active Case Assignment Rule.

Let us know if that helps you.

Best Regards,
BALAJI

All Answers

Lokesh KumarLokesh Kumar
You can achieve this through Process Builder presuming you have knowledge of Process Builder.

Thanks
Loeksh
DeveloperDeveloper
Thanks for giving reply Lokesh. i need to write trigger only.
That should be active through trigger only, could you plase help me on this.


Thanks In advance.
Gopal AgrawalGopal Agrawal
Hi Gopal,

I think, this will help you
trigger CaseAssignment on Case (after insert,after update) {
    //Variable decleration
    List<Case> caseList = new List<Case>();
    User integrationUserObj = new User();
    
   //Fetching the assignment rules on case
    AssignmentRule AR = new AssignmentRule();
    AR = [select id from AssignmentRule where SobjectType = 'Case' and Active = true limit 1];

        //Creating the DMLOptions for "Assign using active assignment rules" checkbox
    Database.DMLOptions dmlOpts = new Database.DMLOptions();
    dmlOpts.assignmentRuleHeader.assignmentRuleId= AR.id;

   
    
    if(Trigger.isAfter && Trigger.isInsert){
        //Fetching the Community User details
        integrationUserObj = [SELECT Id, Name FROM User where Profileid= '00e28000001hb4Y' LIMIT 1];
           
        for (Case caseObj : Trigger.new) {
            if (caseObj.OwnerId  == integrationUserObj.Id) {
             Case newCase = new Case(Status = 'New') ;
                //Setting the DMLOption on Case instance
              newCase.setOptions(dmlOpts);
                caseList.add(newCase);
            }
        }
   
        
        Database.update(caseList, dmlOpts);
    }
}

Please let me know if you have any query. 
BALAJI CHBALAJI CH
Hi Gopal,

You can try below Trigger as per your requirement.
trigger CaseAssignmentTrigger on Case (after insert) {
    
    set<id> caseids = new Set<id>();
    list<case> CaseList = new list<case>();
    for(Case c : trigger.New)
    {
        caseids.add(c.id);
    }
    
    for(Case c: [Select id, Owner.Profile.Name from Case where Id IN:caseids])
    {
        if(c.owner.Profile.Name == 'Customer Community User')
        {
            CaseList.add(c);
        }
    }
    if(CaseList.size() > 0)
    {
    Database.DMLOptions dmo = new Database.DMLOptions();
    dmo.assignmentRuleHeader.useDefaultRule = true;
    Database.update(caseList, dmo);
    }
}
You need to have a Active Case Assignment Rule.

Let us know if that helps you.

Best Regards,
BALAJI
This was selected as the best answer