• venkata sainath
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
I have a requirement that I will have only one primary contact for an account and on clicking send email button then primary contact email should populate on TO:   automatically.

Can anyone help me to achieve this functionality.
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
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