+ Start a Discussion
Dhilip DussaDhilip Dussa 

Please Help me on this before update, before insert, after insert, after update trigger

Hi,

Here I have a requirement to set the record as a primary record

currently, I'm using case object in that case object I created a picklist field values are True and false

I create any new case I would be the primary record need to update remaining records false I did it but

i perform before update operation any record need to update that record as primary = true remaining records will be false anyone please help me on this, please

public class CaseController {
    
    public static Boolean isFirstTime = true;
    
    public Static void setPrimary(List<Case> caseList){
        for(Case c : CaseList){
            c.Primary__c = 'True';
        }
    }
    
    public Static void updatePrimary(List<Case> caseList){
        Set<Id> cIds = new Set<Id>();
        Set<Id> conIds = new Set<Id>();
        
        for(Case c : caseList)
        {            
            cIds.add(c.Id);
            conIds.add(c.contactid);
            
        }
        List<Case> cRecords = [Select Id,Primary__c,contactId From Case Where ContactId In :conIds];
        
        List<Case> cToUpdate = new List<Case>();
            for(Case c : cRecords)
            {
                if(!cIds.contains(c.Id))
                {
                    if(c.Primary__c=='True')
                    {
                        c.Primary__c = 'False';
                        cToUpdate.add(c);
                    }
                }
            }
         if (!cToUpdate.isEmpty()){
            update cToUpdate;
         }
        }
}

ANUTEJANUTEJ (Salesforce Developers) 
Hi Dilip,

Are you getting any error??

I think the code you have written is correct and might run properly Can you check once and ensure if the flow of the code is going properly??

Regards,
Anutej