+ Start a Discussion
venkata sainathvenkata sainath 

Primary contact

trigger validationPrimarycontact1 on Contact (after insert,after update) {
List<Contact> acclist = new List<Contact>();
set<Id> accIdSet = new set<Id>();
Set<Id> ContactIds = new Set<Id>();
if(Trigger.IsUpdate){
for ( Contact s : trigger.new ){
        if(s.AccountId != null)
        accIdSet.add(s.AccountId);   
        Contact oldcon = Trigger.oldMap.get(s.Id);       
        if(oldcon.id != null)
        ContactIds.add(oldcon.id);
       
                                }

acclist=[select id, name,Account.name,Primary_Contact__c from Contact where AccountId IN : accIdSet AND Id NOT IN : ContactIds];

system.debug('*******Runish'+acclist);

List<Contact> conlist=new List<Contact>();
Contact c1=new Contact();
 if(checkRecursive.runOnce()){
        for ( Contact s : Trigger.new)

             {         
            if(s.Primary_Contact__c ==true)
                {
                  for(Contact a :acclist)
                      {
                         a.Primary_Contact__c =false;
                         c1=a;            
                      }
          
                    update c1; 
                }  
              }
           update acclist;
                       }
     }
        if(Trigger.isInsert){
                for ( Contact s : trigger.new ){
                        if(s.AccountId != null)
                        accIdSet.add(s.AccountId);   
                        Contact oldcon = Trigger.newMap.get(s.Id);       
                        if(oldcon.id != null)
                        ContactIds.add(oldcon.id);
       
                                                }

acclist=[select id, name,Account.name,Primary_Contact__c from Contact where AccountId IN : accIdSet AND Id NOT IN : ContactIds];

system.debug('*******Runish'+acclist);

List<Contact> conlist=new List<Contact>();
Contact c1=new Contact();

 if(checkRecursive.runOnce()){
for ( Contact s : Trigger.new)
         {      
            if(s.Primary_Contact__c ==true)
                {
                  for(Contact a :acclist)
                          {
                             a.Primary_Contact__c =false;
                              c1=a;            
                          }
                     update c1; 
        
        }  
        
       }
       update acclist;
       }
        
        }
         }


I am getting error that "Variable does not exist: checkRecursive",how to fix
Best Answer chosen by venkata sainath
v varaprasadv varaprasad
Hi venkat,

you are using checkRecursive class in your trigger to avoid it create below class.

 
public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}

I hope it helps you.

Thanks
Varaprasad
 

All Answers

v varaprasadv varaprasad
Hi venkat,

you are using checkRecursive class in your trigger to avoid it create below class.

 
public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}

I hope it helps you.

Thanks
Varaprasad
 
This was selected as the best answer
Alisha Mehta 9Alisha Mehta 9
Hi ,

Have you created the below class?

 public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
       if(run){
        run=false;
        return true;
       }
       else{
           return run;
       }
    }
}

Tru creating this Apex Class, then save trigger
venkata sainathvenkata sainath
Thanks a lot guys for your quick reply,Its working perfectly now @varaprasad @Alisha Mehta 9