Apex Triggers - 12 (Trigger Interview Question)
ฝัง
- เผยแพร่เมื่อ 24 ม.ค. 2023
- 💻 Join the Titan Community : Discover Exclusive Insights on LinkedIn / mycompany
💻 Explore the Power of Titan : Visit Our Official Website Now 🔗 titandxp.com/ - วิทยาศาสตร์และเทคโนโลยี
Very useful
Keep it up brother, waiting for new videos .
ur videos so good and interview related
Thanks for your support brother
@@sfdcninjas aur videos kb daal rhe ho
Sir just out of curiosity sir I have never seen so many null check as u said it's best practices so while working in real-time is it required to use all the null check or u can ignore some
You can ignore null check but it is always a good practice to apply a null check to avoid null pointer exception in apex.
@@sfdcninjassir can we use safe navigation operator instead of this if statement
At the end can we create list and add acc like insert list ? Wil it has same.result?
yes you can use list or map for dml operations
Bro do some real-time batch apex example also
sure bhai
Hi bro, if we insert two case records with same account id at the same time then which record's case number will be populated in account? Please do answer
Hi Abhishek , if two case records with the same account ID are inserted at the same time, the case number of the last inserted case record will be populated in the "Latest_Case_Inserted__c" field of the corresponding account.
Thank you for this wonderful series. I have just tried another approach and its working fine too.
Code:
trigger LatestCaseInserted on Case (after insert) {
if(trigger.isAfter && trigger.isInsert){
Map caseMap = new Map();
for(Case cs : trigger.new){
if(cs.AccountId != null){
caseMap.put(cs.AccountId,cs);
}
}
List caseList = [Select Id,CaseNumber,AccountId from Case where AccountId IN : caseMap.keySet()];
Map accountUpdateMap = new Map();
for(Case cs : caseList){
Account ac = new Account();
ac.Id = cs.AccountId;
ac.Latest_Case_Inserted__c = caseMap.get(cs.AccountId).CaseNumber;
accountUpdateMap.put(ac.Id, ac);
}
if(!accountUpdateMap.isEmpty()){
update accountUpdateMap.values();
}
}
}
Nice one
why do u need caselist? u can iterate over trigger.new directly na? rest logic is fine using map is nice, but why did u query case list? we can use trigger.new and while new record will get in DB we can attach case number to acc field