You need to sign in to do that
Don't have an account?
himanshu huske 7
Trigger_Task_1
Hello, I am beginner. Help me with This,
1.When Should we use Map In Trigger.
2.Give me some trigger scenerios for account, contact, lead, opportunity in which Map can be used.
1.When Should we use Map In Trigger.
2.Give me some trigger scenerios for account, contact, lead, opportunity in which Map can be used.
Greetings to you!
1. When Should we use Map In Trigger : -
I always try to use Maps when working with an update trigger. Especially when I need to check if some field has changed value. Using maps I can loop through all old and new values in one loop, for example:
which is squared as heavy on CPU as just one loop. For example:
You update 100 records. First option would iterate over 100 elements and take the correct one in each iteration.
Second options would iterate 100 times through the oldList and 100 times through the newList inside each of the iterations. As a result, it would iterate through 100*100 = 10 000 elements.
For insert triggers it doesn't matter because you don't have Trigger.old or Trigger.oldMap anyway, so you won't be able to compare what has changed.
2. Give me some trigger scenarios for account, contact, lead, opportunity in which Map can be used : -
- For the above question , please use the below link : -
- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables_considerations.htm
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha.
All Answers
himanshu huske 7
1. A map is a collection of key-value pairs
Each unique key maps to a single value. Keys can be any primitive data type, while values can be a primitive, sObject, collection type or an Apex object. So depends upon the requirement, If we want to do any changes on values we go for map.
2. Using trigger populate the Account field on the Contact record (only insert scenario)
The above scenario can be done by both list and map, But with map, You can do it with a little amount of code.
Thank you.
1- Map is used in trigger to check previous value of record. Map also helps to make bulkify code.
There is a trigger code on Account to update ShippingPostalCode from BillingPostalCode if BillingPostalCode is updated than update the account's ShippingPostalCode fields
//trigger code
//Helper code I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Greetings to you!
1. When Should we use Map In Trigger : -
I always try to use Maps when working with an update trigger. Especially when I need to check if some field has changed value. Using maps I can loop through all old and new values in one loop, for example:
which is squared as heavy on CPU as just one loop. For example:
You update 100 records. First option would iterate over 100 elements and take the correct one in each iteration.
Second options would iterate 100 times through the oldList and 100 times through the newList inside each of the iterations. As a result, it would iterate through 100*100 = 10 000 elements.
For insert triggers it doesn't matter because you don't have Trigger.old or Trigger.oldMap anyway, so you won't be able to compare what has changed.
2. Give me some trigger scenarios for account, contact, lead, opportunity in which Map can be used : -
- For the above question , please use the below link : -
- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables_considerations.htm
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha.