Apex Triggers - 31 (Infosys Interview Trigger Scenario)
Вставка
- Опубліковано 6 чер 2023
- 💻 Join the Titan Community : Discover Exclusive Insights on LinkedIn / mycompany
💻 Explore the Power of Titan : Visit Our Official Website Now 🔗 titandxp.com/
These are the 2 methods by which we can fetch Cases related to account in this video I have used aggregate query method to fetch related Cases.
Method - 1 (Using Inner SOQL query)
accList = [SELECT Id, (SELECT Id FROM Cases WHERE IsClosed = true) FROM Account WHERE Id IN :accIds];
Integer closedCaseCount = acc.Cases.size();
Method - 2 (Using Map)
for (Case closedCase : [SELECT AccountId FROM Case WHERE AccountId IN :accIds AND IsClosed = true])
{
if (accountCaseCountMap.containsKey(closedCase.AccountId))
{
accountCaseCountMap.put(closedCase.AccountId, accountCaseCountMap.get(closedCase.AccountId) + 1);
}
else {
accountCaseCountMap.put(closedCase.AccountId, 1);
}
}
Integer closedCaseCount = accountCaseCountMap.containsKey(acc.Id) ? accountCaseCountMap.get(acc.Id) : 0; - Наука та технологія
Great code until line 42. We are unnecessarily querying account again and looping it. We could have done it in 2-3 lines. You don’t need to query account again, you may create an instance and assign Id. Thanks.
Can you also make videos on not only triggers but also on apex classes which are used for different functionalities. That will be very helpful.
Hi , Yes i have that in my plan dont worry you wil get it soon
bro no doubt you are doing great but please give a good description of scenario here you are telling update accoun ton basis of closed cases so we are assuming it will be on account object you would have tp give better description like when we change or insert cases then update account this looses motivation bcoz our reasoning fails due to wrong questions
Hello can you please let us know how you are getting this scenarios , if the questions are available in more content it will help to practice as our own as well
Hi Prithvi, I have reached out to experienced professionals and individuals who have undergone interviews at various companies in order to gather insights and information regarding different scenarios.
When aggregating the case object is it not possible to write sub query of account.?
Hi buddy sorry for late reply actually it is not a good practice to use sub query
@sfdc ninja
At what experience level do they ask this type of questions?
Hi Rajnish , this question was asked to a person with 1.8 years of experience.
@@sfdcninjas
Thanks for reply :)
Write a Test Class For that Triggers.
Hi Venkatesh , yes buddy i have that in my plan there will be playlist of test classes for all trigger scenarios
Same
don’t worry guys there will be a series on test classes
Sir plz make a video on interview questions asked in various IT companies .
Please Can you share code
Hi Hareesh , all codes will be available on website soon don't worry.
@@sfdcninjas thanks
Try the below one the below code will work in Bulk for the all the Trigger Scenario's.
Trgr: ->
----
trigger AccountRatingTrgr on Case(after insert, after update, after delete, after undelete){
if(Trigger.isAfter){
if(Trigger.isUpdate){
AccountRatingTrgrCls.AccountRatingTrgrMethod(Trigger.New,Trigger.OldMap);
}else if(Trigger.isDelete){
AccountRatingTrgrCls.AccountRatingTrgrMethod(Trigger.Old,null);
}else{
AccountRatingTrgrCls.AccountRatingTrgrMethod(Trigger.New,null);
}
}
}
Modular Class: ->
-------------
public class AccountRatingTrgrCls{
public static void AccountRatingTrgrMethod(List caseList,Map oldCaseMap){
String accountRating;
Set setOfAccountIds = new Set();
Map accountMap = new Map();
List accountList = new List();
List accountListToUpdate = new List();
if(caseList!=null && caseList.size()>0){
for(Case cses : caseList){
if(cses.IsClosed__c){
if(oldCaseMap==null){
setOfAccountIds.add(cses.AccountId);
}else{
if(cses.AccountId != oldCaseMap.get(cses.Id).AccountId){
setOfAccountIds.add(cses.AccountId);
setOfAccountIds.add(oldCaseMap.get(cses.Id).AccountId);
}else{
setOfAccountIds.add(cses.AccountId);
}
}
}
}
if(setOfAccountIds.size()>0 && setOfAccountIds!=null){
for(AggregateResult agr : [Select AccountId, Count(Id) countOfCaseIds FROM Case WHERE AccountId IN: setOfAccountIds GROUP BY AccountId]){
accountMap.put((Id)agr.get('AccountId'),(Integer)agr.get('countOfCaseIds'));
}
}
accountList = [Select Id,Rating FROM Account WHERE ID IN: setOfAccountIds];
System.debug('accountMap: '+accountMap);
if(accountList.size()>0 && accountList!=null){
for(Account accounts : accountList){
if(!accountMap.isEmpty() && accountMap!=null){
accountRating='';
if(accountMap.containsKey(accounts.Id)){
if((Integer)accountMap.get(accounts.Id)==0){
accountRating = '';
}else if((Integer)accountMap.get(accounts.Id)>0 && (Integer)accountMap.get(accounts.Id)2 && (Integer)accountMap.get(accounts.Id)6){
accountRating = 'Hot';
}
accountListToUpdate.add(new Account(Id=accounts.Id,Rating=(String)accountRating));
}else{
accountListToUpdate.add(new Account(Id=accounts.Id,Rating=''));
}
}else{
accountListToUpdate.add(new Account(Id=accounts.Id,Rating=''));
}
}
}
if(accountListToUpdate.size()>0 && accountListToUpdate!=null){
UPDATE accountListToUpdate;
}
}
}
}
in my Case Status field CLOSED picklist is not showing.
Only New, Working, Escalated is visible.
How to view Closed option?
Hi Shubham , I have already explained that issue in video please see from 14:00