4

My problem is this. When a user changes the status on a Case object, I want to see if they changed that status to closed. If so, I want to do some checks to see if I am going to allow that.

What I have so far is this

trigger CaseClosed on Case (before update) {

    // Check if the trigger's status is being changed to closed.
    List<Case> newCases = Trigger.new;
    Case newCase;

    for( Case c : newCases ) {
        if( Case.CaseNumber === c.CaseNumber ) {
            newCase = c;
        }
    }

My issue is that I don't know how to get the Id, CaseNumber, or whatever the unique identifier of a case is. In a trigger I've written previously I was able to use a UserInfo object and do UserInfo.getUserId() in the if statement. However, I don't see anything like that available for Case objects.

6

The ID of any record is always just Id.

For example, to see if the case was closed this transaction:

trigger doSomethingOnCases on Case (before update) {
    Case[] closingCases = new Case[0];
    for(Case record:Trigger.new)
        if(record.IsClosed && !Trigger.oldMap.get(record.Id).IsClosed)
            closingCases.add(record);
    // For each record, see if we'll allow the close.
    // closingCases now contains all cases that will be closed when transaction completes.
}

Every record in salesforce.com has an ID value. You can always depend on this field being present. It isn't documented under any specific object because it applies globally across all objects.

  • This is close to what I was looking for. However, how would I know which of those closing cases was the one being updated that caused this trigger to fire? @sfdcfox – EthanLWillis Sep 25 '13 at 16:20
  • That would be any that are in the list. From the UI, this list will only be 1 item large, but in the Data Loader, it could be a list of up to 200. – sfdcfox Sep 25 '13 at 16:22
  • Ah wait, if it was only one Case being updated then Trigger.new will only have that Case, right? – EthanLWillis Sep 25 '13 at 16:22
  • yes thank you! That's exactly what I was just thinking. – EthanLWillis Sep 25 '13 at 16:22
2

The answer asked for how to do this in a trigger, and sfdcfox gave a great answer.

It might be easier to use to create a Workflow Rule instead of a trigger. But this wholly depends on what you want to do once you identified Cases that changed their status to Closed. If it's simple like set the datetime field Closed_Datetime__c to the current time, workflows are great. If it's complex, stick to the trigger (though with the added maintenance cost of test code...).

How to write Workflow Rules: https://login.salesforce.com/help/doc/en/creating_workflow_rules.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.