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
Felix Jong Seok ChaeFelix Jong Seok Chae 

Get a record that has been updated from apex trigger

So I have rest api written in java and it can update a case record.
The problem is in the "after update" trigger, I don't know the command to get a update case record.
For "after insert" trigger, I knoew I can simply have "Case c = Trigger.New[0]" to get a new inserted case record.
Any help would be appreciated.
HARSHIL U PARIKHHARSHIL U PARIKH
Can't you just get an Id of the record right before it's about or subject to update inside the trigger?

It's like when you put your DML on List of records who are subject to update, those are the records are part of After Update.
Felix Jong Seok ChaeFelix Jong Seok Chae
Hi Govind,
so one of the functions in java rest will take a casenumber for the case I want to update as an input, but I don't know how Apex trigger can receive that case number. Like you mentioned, my plan was to write sql command like  Case c =  [select caseid from case where casenumber = casenumberFromJava][0], but don't know how I can bring casenumberFromJava from java.
HARSHIL U PARIKHHARSHIL U PARIKH
I am not too familier any picture besides Apex but here is what I was trying to say...

Let's say you are counting Case Records on Account and Contact such as Number of Cases on Account and Number of Cases on Contacts.

see the trigger below,
Trigger CaseRecordCount on Case(After Insert, After Update, After Delete, After UnDelete){
    
    List<ID> AccountIds = New List<ID>();
    List<ID> ContactIds = New List<ID>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        For(Case c: Trigger.New){
            AccountIds.add(c.AccountId);
            ContactIds.add(c.ContactId);
        }
     }
    If(Trigger.IsDelete){
        For(Case c: Trigger.Old){
            AccountIds.add(c.AccountId);
            ContactIds.add(c.ContactId);
        }
     }
     
     List<Account> AccountListToUPdate = New List<Account>();
     List<Contact> ContactListToUpdate = New List<Contact>();
     
     List<Case> CS1 = [Select Subject, Contact.Account.Owner.ID FROM Case];
     
     For(Account act: [Select Total_Cases__c, (Select ID FROM Cases) FROM Account WHERE ID = :AccountIds]){
     act.Total_Cases__c = act.Cases.size();

     AccountListToUpdate.add(act);
     }
     
     For(Contact con: [Select Total_Cases__c, (Select ID FROM Cases) FROM Contact WHERE ID = :ContactIds]){
     con.Total_Cases__c = con.Cases.size();
     ContactListToUpdate.add(con);
     }
     
     try{
         Update AccountListToUpdate;
         Update ContactListToUpdate;
     }
     Catch(Exception E){
         System.Debug('Error Message: ' + e.getMessage());
     }
}
Did you notice the lists AccountListToUPdate and ContactListToUpdate are the lists who contains the records who are about the get updated.

So, you can say something like
For(Account EveryAccountFromList : AccountListToUPdate ){
   
       System.debug('Updating Account Id Is: " + EveryAccountFromList.Id);
}
And same thing for the contact list such as
 
For(Contact EveryContactFromList : ContactListToUpdate ){
   
       System.debug('Updating Contact Id Is: " + EveryContactFromList.Id);
}

Hope this helps!