+ Start a Discussion
Gouse Mohiddin 4Gouse Mohiddin 4 

How to get Max Number Incase of Insert & Delete Operation

Below Code Is Working For Insert Operation, Now I Need Max Number In the Case of Delete Operation How Can Get?


trigger SkillGroup_With_High_Rank on Skill_Group__c (after insert,before delete) {
        List<Id> AccnId = New List<Id>();
    public String Maxno;
   
    for(Skill_Group__c sk:Trigger.New){
        
        AccnId.add(sk.Account_Name__c); 
        system.debug('Accountssss'+AccnId);
    }
    list<aggregateResult> aggResults = [Select Max(Rank__c)RA from Skill_Group__c Where Account_Name__c In:AccnId ];
    
      system.debug('@@@@@@'+aggResults[0].get('RA'));
   Maxno = string.valueOf(aggResults[0].get('RA'));
    system.debug('$$$$$$$'+Maxno);
    for(Account ac: [Select id,Skill__c from Account where id In:AccnId]){
        ac.Skill__c = Maxno; 
    }
    }
AubryAubry
Gouse, for "before delete" triggers, you have to use Trigger.old, NOT Trigger.new.
Use one list, then populate it baed on what trigger type you are running:
trigger SkillGroup_With_High_Rank on Skill_Group__c (after insert,before delete) {
    List<Id> AccnId = New List<Id>();
    public String Maxno;

    List<Skill_Group__c> TriggerList = new List<Skill_Group__c>();
    if(trigger.isInsert) TriggerList.addAll(trigger.new);
    if(trigger.isDelete) TriggerList.addAll(trigger.old);
   
    for(Skill_Group__c sk:TriggerList){
        AccnId.add(sk.Account_Name__c); 
        system.debug('Accountssss'+AccnId);
    }
    list<aggregateResult> aggResults = [Select Max(Rank__c)RA from Skill_Group__c Where Account_Name__c In:AccnId ];
    
      system.debug('@@@@@@'+aggResults[0].get('RA'));
   Maxno = string.valueOf(aggResults[0].get('RA'));
    system.debug('$$$$$$$'+Maxno);
    for(Account ac: [Select id,Skill__c from Account where id In:AccnId]){
        ac.Skill__c = Maxno; 
    }
    }