Apex Triggers - 36 (Deloitte Salesforce Interview Question)
Вставка
- Опубліковано 28 лип 2024
- 💻 Join the Titan Community : Discover Exclusive Insights on LinkedIn / mycompany
💻 Explore the Power of Titan : Visit Our Official Website Now 🔗 titandxp.com/ - Наука та технологія
Thank You
😇
Thanks bro
I truly love your channel. Such creative videos you’ve on this channel. You’re working so hard... keep going buddy❤
Thank you ankit keep watching❤️
don't you think you should first always create trigger and then triggerhandler apex class
Ur doing great buddy .....and one more parallely do asynchronous apex scenario like batch apex future,queuable , schedule apex it will help ...
Sure buddy thanks
but Bhai this code cannot works on bulkification
You are doing great for the community can you please add the exception handling and as well as the test classes for a few triggers atleast.
Hi buddy sure upcoming series will on test classes
What is the experience of a candidate? for this scenario
Wrong Solution.. managerList is not populating correctly in case of bulk functionality. Suppose if someone bulk upload the users having different Team names then in that case managerList will fetch only 1 manager of any team & will assign to all users.
Hi , the managerMap will only store one manager per team due to the limit 1 in the SOQL query. To resolve this issue you can remove that limit 1. Thanks
@@sfdcninjas : What if that org contains more than 50,000 records
Good initiative, Badal. But I do see a few checks that are not at all needed and could be removed.
1. LN 8 of handler - no use of newUserList.isEmpty check as the list would never be null.
2. LN 38 of handler - no use of managerMap.isEmpty, just managerMap.containsKey(userObj.Team__c) is fine.
3, LN 42 of handler - no use of manager != null as it can never be null because you're never putting any null value in the map.
In LN 36 there should be another check of userObj.Assign_Manager__c
Also, isEmpty() is an empty check, not a null check.
Thanks for your support and suggestion sir
I agree with you, few null checks were not needed, here is my apprach for this problem with few lines of code:
handler class:
public with sharing class userManagerHandler {
public static void beforeInsert(List newUsers){
list managers=[SELECT Id, Name, Manager__c, Team__c, Assign_Manager__c FROM User where team__c!=null and isActive=true];
Map managerMap=new Map();
//putting team name and managers in a map
for(User use:managers){
if(!managerMap.containsKey(use.Team__c)){
managerMap.put(use.Team__c,use);
}
}
//testing team and assign managers
for(User users:newUsers){
if(users.Team__c!=null && users.Assign_Manager__c==true){
if(managerMap.containsKey(users.Team__c)){
users.Manager__c= managerMap.get(users.Team__c).id;
}else{
users.addError('No manager available from '+users.Team__c+' Team');
}
}
}
}
}
trigger:
trigger userManTrig on User (before insert) {
userManagerHandler.beforeInsert(Trigger.new);
}
WRONG Solution, Code will not work for bulkified scenario, it's a good practise to bulkfy triggers.