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
m 10m 10 

A case can’t be closed if it is owned by the Integration User

Hi team,
how too write trigger for below scenario i am new to salesforce please help me
"A case can’t be closed if it is owned by the Integration User or a by a queue that is not the Closed Cases Email Received Queue."
AnudeepAnudeep (Salesforce Developers) 
Here is a sample code
 
trigger CaseTrigger on Case (after update) {

    // Find all cases that are being closed...
    List<Case> csList = new List<Case>();
    Set<Id> newlyClosedCaseIds = new Set<Id>();
    for (Id caseId : Trigger.newMap.keySet()) {
        if (Trigger.newMap.get(caseId).IsClosed && 
            !Trigger.oldMap.get(caseId).IsClosed) {
            newlyClosedCaseIds.add(caseId);
        }
    }

   User u = [select id, name from user where name = 'Integration User' limit 1];

csList = [Select id, ownerId from case where id IN:newlyClosedCaseIds];


Id QueueId = [select Id from Group where Name = 'Closed Cases Email Received Queue' and Type = 'Queue']

    // Check if the case is owned by Integration User 
    // Add the error to the Trigger.new entry to prevent the save
    for (Case cs : csList) {
        Id caseId = (Id) aggResult.get('WhatId');
        if(cs.OwnerId == u[0].id && cs.OwnerId!=QueueId);

        cs.addError('Cannot close case since it is owned by integration user ); 
    }
}

NOTE: The code provided is an example. You'll need to review and make modifications for your organization.

Let me know if this helps. If it does, please mark this answer as Best. It may help others in the community. Thank You!
m 10m 10
Hi Anudeep,
could you help me I need work support ,please share your contact number are email id are send message below imranmforce@gmail.com