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
AnjaneyluAnjaneylu 

Trigger.oldMap.keySet()

Hi Experts,
what is the meaning of Trigger.oldMap.keySet(),  When we will use this trigger.  and what  is the function of this trigger..
if possible explain with simple example..
Please help me,

Thanking you & Regards,
Anji reddy
Best Answer chosen by Anjaneylu
sandeep sankhlasandeep sankhla
Hi Anji,
 
trigger accountTrigger on Account (after update, after delete) 
{
	if(Trigger.isupdate)
	{
		Map<Id, Integer> mapAccIdToCount = new Map<Id, Integer>();
		
		for(Account opp : Trigger.New) 
		{
				
			//If status and product family both updated to correct value then count will increase to 1
			if((opp.status!=trigger.oldmap.get(opp.id).status && opp.status == 'Shipped') &&
			 (opp.Asset_Product_Family__c!=trigger.oldmap.get(opp.id).Asset_Product_Family__c && opp.Asset_Product_Family__c == 'SAN Storage Array'))
			{
					//logic
			}
			
		}
		}

In above code example I am checking is status value is changed to some specific value and product family too..then only i am doing my processing..else not..

because for example say I want to create one task when account status is shipped...

so if I will not add the filter then it will create task everytime whenever I will update the anything on account..

but by comparing old value with new value we can check if status is changed or not 

let me knwo if it is clear to you..
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 

All Answers

sandeep sankhlasandeep sankhla
Hi anji...

When you update the record then you will get old map and new map in trigger which means, you get old values of the record and then new values of the record...

Trigger.oldMap.keySet() -- this will contain id of all the records those are updated..

old value we use normally to compare things based on our business logic..

lets take an example:

you want to update the account field only when status is changed...

now if you will not update the status but you updated the other field in account, this time trigger will get fire as you have written trigger on after update event of account..

so in code you can check the condition if Trigger.oldMap.status != trigger.newmap().status   -- which means if status is updated then only your trigger logic should execute..

let me know if you need more elaboration..

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
AnjaneyluAnjaneylu
Thankyou so much Sandeep,

Nice explanation..
Could you Please explain with some simple trigger..

Regards,
Anji
 
sandeep sankhlasandeep sankhla
Hi Anji,
 
trigger accountTrigger on Account (after update, after delete) 
{
	if(Trigger.isupdate)
	{
		Map<Id, Integer> mapAccIdToCount = new Map<Id, Integer>();
		
		for(Account opp : Trigger.New) 
		{
				
			//If status and product family both updated to correct value then count will increase to 1
			if((opp.status!=trigger.oldmap.get(opp.id).status && opp.status == 'Shipped') &&
			 (opp.Asset_Product_Family__c!=trigger.oldmap.get(opp.id).Asset_Product_Family__c && opp.Asset_Product_Family__c == 'SAN Storage Array'))
			{
					//logic
			}
			
		}
		}

In above code example I am checking is status value is changed to some specific value and product family too..then only i am doing my processing..else not..

because for example say I want to create one task when account status is shipped...

so if I will not add the filter then it will create task everytime whenever I will update the anything on account..

but by comparing old value with new value we can check if status is changed or not 

let me knwo if it is clear to you..
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
This was selected as the best answer
sanath reddy 12sanath reddy 12
if(opp.Status__c != oppOldMap.get(opp.Id).Status__c){ 
                        pr.Status__c = opp.Status__c; 
Meaning for this Can anyone help me