+ Start a Discussion
SS KarthickSS Karthick 

Trigger.oldMap.keySet()

Hi folks
       Can anyone tell me what is trigger.oldmakp.keyset() and give me sample example for that

Please Help
Best Answer chosen by SS Karthick
NaveenReddyNaveenReddy
Hi,

 Please find below your answer with example.

"Trigger.oldMap : A map of IDs to the old versions of the sObject records.Note that this map is only available in update and delete triggers.
   example {0039000000exZdhAAE ,Contact1},{0039000000exZdhAfg,contact2},{0039000000exZdhBFE,contact3}

Trigger.oldMap.keyset() :Will give the Id's present in the Trigger.oldMap
  example   {0039000000exZdhAAE,0039000000exZdhAfg,0039000000exZdhBFE}"

Regards,
Naveen
SSE , Salesforce CI expert group
http://www.autorabit.com

All Answers

@anilbathula@@anilbathula@
Hi Karthick Saravanana,

Trigger.oldMap - A map of IDs to the old versions of the sObject records.Note that this map is only available in update and delete triggers.

Gothrough this link for more info on these:-

http://www.salesforcetutorial.com/salesforce-collections/
https://developer.salesforce.com/forums/ForumsMain?id=906F000000093hXIAQ
http://developer.force.com/cookbook/recipe/comparing-queries-against-trigger-old-and-trigger-new


Thanks
Anil.B
SS KarthickSS Karthick
 Hy anilbathula,
    I dont wanna links to study...
I have to know wen to use and how to use from your perspective

Thanks in advance
Hargobind_SinghHargobind_Singh
Karthick, 

It is hard to answer theoritical questions without a problem statement or a use-case. 

Generally, trigger.oldMap is used to get old-version of records, and is used in Update Triggers, to get the old values, either for comparison with new values or for any other purpose. 

The links that anilbathula has posted have the usage, with example as well, I would suggest you should read them and also read apex documentation for trigger.oldMap. 

- HS 

SS KarthickSS Karthick
Thanks anilbathula and hs1
Vijaya Kumar RegantiVijaya Kumar Reganti
Hi,

Trigger.oldMap.keySet();  will give the Id's present in the Trigger.oldMap.
It is a set type collection of all the Id's.
have a look at the following example,  change the DML events in the trigger events every time and see the debug logs.
you will understand which trigger context variable is available for which DML event.

trigger AccTrigger on Account (before insert){   
   
    system.debug('--Trigger.new-------------------'+Trigger.new);
    system.debug('--Trigger.old--------------------'+Trigger.old);
    system.debug('--Trigger.newMap-----------------'+Trigger.newMap);
    system.debug('--Trigger.oldMap------------------'+Trigger.oldMap);
   
  
}


Thanks,
Vijay
Vijaya Kumar RegantiVijaya Kumar Reganti
Hi,

Please find the below code where I have used the Map.keySet();

Code:
****************


trigger ConTrgToUpdateParent on Contact (after update) {

   
    Map<Id,Contact> Mp = new Map<Id,Contact>();
    List<Account> aLstToUpdate = new List<Account>();
   
    //indexed for loop
    for(integer i=0; i<trigger.new.size(); i++){
       
       
        //condition to check if the phone value is changed
        if(trigger.new[i].Phone != trigger.old[i].phone){           
           
            //Adding the Account Id's to a set
            //aIds.add(trigger.new[i].AccountId);
           // System.debug('Mp'+Mp.size()+'----'+Mp);
            System.debug('trigger.old[i].AccountId : '+trigger.old[i].AccountId);
            //System.debug('trigger.new[i] : '+trigger.new[i]);
           
            Mp.put(trigger.old[i].AccountId,trigger.new[i]);           
           
        }
        //Querying the Account records where the contact phone is changed..
        for(Account acc :[Select Id,phone FROM Account WHERE Id =: Mp.keySet()]){          
           
           
            //updating the account phone with the contact changed phone value..
            system.debug('Mp.get(acc.Id) :'+Mp.get(acc.Id));
            acc.phone = Mp.get(acc.Id).phone;
           
            //adding the account with changed phone to a list..
            //update acc;//
            aLstToUpdate.add(acc);
        }
        //updating the account list with new phone values
        update aLstToUpdate;
       
    }
}

Thanks,
Vijay
NaveenReddyNaveenReddy
Hi,

 Please find below your answer with example.

"Trigger.oldMap : A map of IDs to the old versions of the sObject records.Note that this map is only available in update and delete triggers.
   example {0039000000exZdhAAE ,Contact1},{0039000000exZdhAfg,contact2},{0039000000exZdhBFE,contact3}

Trigger.oldMap.keyset() :Will give the Id's present in the Trigger.oldMap
  example   {0039000000exZdhAAE,0039000000exZdhAfg,0039000000exZdhBFE}"

Regards,
Naveen
SSE , Salesforce CI expert group
http://www.autorabit.com

This was selected as the best answer
syed fayoomsyed fayoom
This Is Very Usefull Coding For All Thank U