Hello Sir, I have faced same scenario in one of my interviews but in addition to this they have asked me , if 2 opportunities have same maximum amount then they both oppty name should populate over the account, can you please cover that scenario as well?
in that case you have to remove query limit and get loop over opportunities to get highest valued opportunities in another list or set and use that set to populate names as a comma separated values
Bro if we are writing this code in Trigger Class then where we should write the trigger context (trigger.isDelete) and what code we have to write under is delete. Pls clear it
Here's a different approach. Feedback and suggestions are welcomed trigger OpportunityTrigger on Opportunity (after insert, after update, after delete, after undelete) {
if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete)){ Set accIds = new Set(); for(Opportunity opp: Trigger.new){ accIds.add(opp.AccountId); }
List aggrList = [Select Name, AccountId, MAX(Amount) maxAmt from Opportunity where Amount!=null and AccountId IN: accIds group by Name, AccountId ORDER BY MAX(Amount) desc limit 1];
List accList = new List(); for(AggregateResult aggr: aggrList){ Id accId = (Id) aggr.get('AccountId'); String oppName = (String) aggr.get('Name'); Decimal maxOppAmount = (Decimal) aggr.get('maxAmt'); Account acc = new Account(Id= accId, Opp_having_Max_Amount__c=oppName); accList.add(acc); }
update accList; }
//-- For Delete
if(Trigger.isAfter && Trigger.isDelete){ Set accIds = new Set(); for(Opportunity opp: Trigger.old){ accIds.add(opp.AccountId); }
List aggrList = [Select Name, AccountId, MAX(Amount) maxAmt from Opportunity where Amount!=null and AccountId IN: accIds group by Name, AccountId ORDER BY MAX(Amount) desc limit 1];
List accList = new List(); for(AggregateResult aggr: aggrList){ Id accId = (Id) aggr.get('AccountId'); String oppName = (String) aggr.get('Name'); Decimal maxOppAmount = (Decimal) aggr.get('maxAmt'); Account acc = new Account(Id= accId, Opp_having_Max_Amount__c=oppName); accList.add(acc); }
i have confusion at 27 line that is else part else{parentIds.add(newopp.AccountId); used for insert, delete and undelete if oldmap = null, in delete operation old records are present in trigger.old. why should we take oldmap = null ;
Hi , The reason for setting oldMap to null in the else part of the code is to handle the case where the trigger is fired on an insert, delete, or undelete operation. In all these cases, there is no old record to compare with the new record, so the oldMap parameter is set to null to avoid a null pointer exception when trying to access the old record.
Use the map and set and avoid the inner soql query because if the inner query has record more than 10000 then you will get an error
Thank you for your suggestion 🙂
Thank you very much Badal bhai for adding real interview question. please add as much as possible trigger interview scenario questions.
Thank you so much Pankaj bhai
Thanks for video
Bro your code is to lengthy you can minimise line of code as Salesforce orgs limit on line codes in org
Discuss more such triggers your content is very good
Thank you Abhilash, more trigger scenarios coming soon dont worry
İf we don’t use last else in helper class for . İt would be correct?
Hello Sir, I have faced same scenario in one of my interviews but in addition to this they have asked me , if 2 opportunities have same maximum amount then they both oppty name should populate over the account, can you please cover that scenario as well?
in that case you have to remove query limit and get loop over opportunities to get highest valued opportunities in another list or set and use that set to populate names as a comma separated values
Very helpful ❤
hi your logic is not working if i not select any Tech farm when update can u chack please and reply me
I have one doubt Where we have received trigger.old value in Handler class from Trigger for After Delete operation
hi buddy we are passing it from our trigger in handler class
Is this for 2 years of experience?
Very nice sir
Hi, I used Aggregate Result query instead of nested SOQL query. For delete operation, my code is not working. Can you tell me why is not working?
Please show me your code
Will this code give right output when inserting mass opportunitues on multiple accounts? as parentIds set will contains I'ds for multiple accounts.
can we do this with the help of aggregate query
yes we can do it by using aggregate query
Superb
thank you so much
Bro if we are writing this code in Trigger Class then where we should write the trigger context (trigger.isDelete) and what code we have to write under is delete.
Pls clear it
Hi , I have one confusion here why we adding Name in MaxOpp ? it should be amount . On time 7.10
Hi Dharmesh, buddy according to scenario we need to show opportunity NAME that has the highest amount 🙂.
@@sfdcninjas Ok Got it , MaxOpp__c type should be string . Thank you so muchh!!
Fir whoch ur experience person this question is? Explain?
It was asked to 2.1 yr of experience
Thank you, please mention which exp person faced these question, please mention in the comment section so that we get clarity. Thank you
i am really Sorry lokesh but i can’t disclose candidate’s name
@@sfdcninjas no sir just tell is it experience question or not in the comment section, if exp then which level that's it
Can you please do a video on howto find top most account on an account
in Line no44 why im getting "Illegal assignment from String to Decimal" Error??
Hi Ankit can you please show your code it seems that you are assigning a String value to a Decimal variable
ankit check field type
@sfdcninja can you please share the code?
Hi Rahul , you can access code on salesforce geek website
@@sfdcninjas can you please provide the link here
Sir , are these scenarios are on fresher level?
these scenarios are for both fresher and experienced
sir plz provide word or pdf document so candidate can easy to save coding
Sure Ankit
Here's a different approach. Feedback and suggestions are welcomed
trigger OpportunityTrigger on Opportunity (after insert, after update, after delete, after undelete) {
if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete)){
Set accIds = new Set();
for(Opportunity opp: Trigger.new){
accIds.add(opp.AccountId);
}
List aggrList = [Select Name, AccountId, MAX(Amount) maxAmt from Opportunity
where Amount!=null and AccountId IN: accIds
group by Name, AccountId ORDER BY MAX(Amount) desc limit 1];
List accList = new List();
for(AggregateResult aggr: aggrList){
Id accId = (Id) aggr.get('AccountId');
String oppName = (String) aggr.get('Name');
Decimal maxOppAmount = (Decimal) aggr.get('maxAmt');
Account acc = new Account(Id= accId, Opp_having_Max_Amount__c=oppName);
accList.add(acc);
}
update accList;
}
//-- For Delete
if(Trigger.isAfter && Trigger.isDelete){
Set accIds = new Set();
for(Opportunity opp: Trigger.old){
accIds.add(opp.AccountId);
}
List aggrList = [Select Name, AccountId, MAX(Amount) maxAmt from Opportunity
where Amount!=null and AccountId IN: accIds
group by Name, AccountId ORDER BY MAX(Amount) desc limit 1];
List accList = new List();
for(AggregateResult aggr: aggrList){
Id accId = (Id) aggr.get('AccountId');
String oppName = (String) aggr.get('Name');
Decimal maxOppAmount = (Decimal) aggr.get('maxAmt');
Account acc = new Account(Id= accId, Opp_having_Max_Amount__c=oppName);
accList.add(acc);
}
update accList;
}
}
i have confusion at 27 line that is else part else{parentIds.add(newopp.AccountId); used for insert, delete and undelete if oldmap = null, in delete operation old records are present in trigger.old. why should we take oldmap = null ;
Hi , The reason for setting oldMap to null in the else part of the code is to handle the case where the trigger is fired on an insert, delete, or undelete operation. In all these cases, there is no old record to compare with the new record, so the oldMap parameter is set to null to avoid a null pointer exception when trying to access the old record.