function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vidya H 4Vidya H 4 

if isPrime field in Contact is checked then whenever the new contact is created isPrime field should check and previous contact's isPrime field should uncheck

for the above scenario i need to write trigger.please help
Best Answer chosen by Vidya H 4
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vidya,

Can you try the below apex trigger.
 
trigger IsPrimeContact on Contact (before insert) {
    List<Contact>conl=new List<Contact>();
    set<ID> accountid= new set<id>();
     for(Contact conlist:Trigger.new ){
        accountid.add(conlist.accountid);
    }
    for(Contact con:[select id,name,accountid from contact where accountid=:accountid]){
        con.isPrime__c=false;
        conl.add(con);
    }
    update conl;
    for(Contact conlist1:Trigger.new ){
        conlist.isPrime__c=true;
    }

}

If this solution helps, Please mark it as best answer.

Thanks,
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vidya,

Is it related to account's contact. Or totally only one contact should be ISPRIME across the org?

Thanks,
 
Vidya H 4Vidya H 4
@Sai Praveen Yeah related to account
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vidya,

Can you try the below apex trigger.
 
trigger IsPrimeContact on Contact (before insert) {
    List<Contact>conl=new List<Contact>();
    set<ID> accountid= new set<id>();
     for(Contact conlist:Trigger.new ){
        accountid.add(conlist.accountid);
    }
    for(Contact con:[select id,name,accountid from contact where accountid=:accountid]){
        con.isPrime__c=false;
        conl.add(con);
    }
    update conl;
    for(Contact conlist1:Trigger.new ){
        conlist.isPrime__c=true;
    }

}

If this solution helps, Please mark it as best answer.

Thanks,
 
This was selected as the best answer
mukesh guptamukesh gupta
Hi Vidya,

Please follow below code:-
 
trigger primCon on Contact ( before update, before insert) {
   List<Id> accounts=new List<Id>();
   
    for(contact c: Trigger.New){
        if( c.AccountId!=Null)
            accounts.add(c.AccountId);
            
    }
        List<contact> contacts= [select id, phone from contact WHERE Primary_contact__c= true AND AccountId IN:accounts];
  
         
    for(contact c:trigger.New){
        if(c.Primary_contact__c==true){
             for(contact con: contacts){
                con.Primary_contact__c=false;
                
               
            }
            update contacts;
          
        }
       
    }
       }

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
Aneesh AllumallaAneesh Allumalla

Trigger YourTriggerName on Contact(before insert, before update){
    List<Contact> conList = new List<Contact>();
        Set<Id> parentAccIds = new Set<Id>();
        String contactId ;
   
    If(trigger.IsInsert || Trigger.IsUpdate){
        for (Contact c : trigger.new){
            if(c.accountId != null){
                if(c.IsPrime__c == true){
                            parentAccIds.add(c.accountId);
                            contactId = c.Id;
                }
            }
        }
    }
        List<Contact> conList2 = [Select id, name, IsPrime__c from Contact where accontId IN: parentAccIds];
        if(conList2.size>0){
            for(Contact con : conList2){
                if (con.Id != contactId){
                    con.IsPrime__c == false;
                    conList.add(con);
                }
            }
        }
        update conList;
}
   
Please let me know if  you need any other help.

Please mark this as best answer if you find it helpful.