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
Ivan WinzerIvan Winzer 

Update a checkbox on a record to uncheck all other existing records

So attached is the code i have in place that im looking to have uncheck a checkbox on a record if its in a list after a record has been updated or added. The class is also there to stop the infinate loop. But its still allowing for me to have multiple checked address records. Can anyone see what im missing...
Apex Code:

trigger PrimaryAddressValidation on ShipTo_Address__c (after insert, after update) {
	
   if (SingleExecution.hasAlreadyDone()) {
  		return;
	}
		SingleExecution.setAlreadyDone();
	
  Contact c=[select id from contact where id in(select Contact__c from ShipTo_Address__c where id in:trigger.new)];
  
  list<ShipTo_Address__c> addList=new list<ShipTo_Address__c>([select id from ShipTo_Address__c where Contact__c in:Trigger.newMap.keySet()]);
  
  Id selectedAddressId;
  
  list<ShipTo_Address__c> updateList=new list<ShipTo_Address__c>(); 
  
  for(ShipTo_Address__c add:Trigger.new)
   {
     if(add.Default_Shipping_Address__c==true)
     {
        c.OtherStreet = add.Address__c; 
        c.OtherCity = add.City__c; 
        c.OtherState = add.State__c; 
        c.OtherCountry = add.Country__c; 
        c.OtherPostalCode = add.ZIP__c; 
     }
   }
   
   for(ShipTo_Address__c aa:addList)
    {
      if(aa.Id!= selectedAddressId)
      {
           aa.Default_Shipping_Address__c=false;
           updateList.add(aa);
      }
    }
 
 update updateList;
 
 update c;
}
Class:

public with sharing class SingleExecution {

private static boolean blnAlreadyDone = false;

public static boolean hasAlreadyDone() {
    return blnAlreadyDone;
}

public static void setAlreadyDone() {
    blnAlreadyDone = true;
}

public static void forceResetAlreadyDone() {
    blnAlreadyDone = false;
}

}

Hopefully someone has dealt with this before. Also i have a primary billing checkbox as well. Can i add that into this same trigger or should i create a new one for that.
 
Best Answer chosen by Ivan Winzer
Roy LuoRoy Luo
I see. The issue is at Contact c = new Contact(Id=s.Id); The line was there and I was wondering how could that work. For new sObject, could not set Id. 

Try this:
trigger PrimaryAddressValidation on ShipTo_Address__c (before insert, before update, after insert, after update) {
	
  if(trigger.isBefore)
  {
  	Set<Id> contactIdsForShipping = new Set<Id>();
      Set<Id> contactIdsForBilling = new Set<Id>();

      Set<Id> shipToExcluded = new Set<Id>();
      Set<Id> billToExcluded = new Set<Id>();
      
      

      for(ShipTo_Address__c sa : Trigger.new)
      {
        
        if(trigger.isInsert)
        {
            if(sa.Default_Shipping_Address__c == true)
            {
              contactIdsForShipping.add(sa.Contact__c);
           
            }
            if(sa.Primary_Billing_Address__c == true)
            {
              contactIdsForBilling.add(sa.Contact__c);
            }
        }
        else
        {
          ShipTo_Address__c oldItem = trigger.oldMap.get(sa.Id);
          if(sa.Default_Shipping_Address__c && !oldItem.Default_Shipping_Address__c)
          {
              contactIdsForShipping.add(sa.Contact__c);
              shipToExcluded.add(sa.Id);
            
          }
          if(sa.Primary_Billing_Address__c && !oldItem.Primary_Billing_Address__c)
          {
              contactIdsForBilling.add(sa.Contact__c);
              billToExcluded.add(sa.Id);
          }       
         
        }
             
      }
     
      if(!contactIdsForShipping.isEmpty())
      {
          List<ShipTo_Address__c> toggleShippings = shipToExcluded.isEmpty()?
             [SELECT Id, Default_Shipping_Address__c FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForShipping AND Default_Shipping_Address__c=true]
             : [SELECT Id, Default_Shipping_Address__c FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForShipping AND Default_Shipping_Address__c=true AND Id NOT IN :shipToExcluded];

          for(ShipTo_Address__c a : toggleShippings)
          {
                  a.Default_Shipping_Address__c = false;
          }
          update toggleShippings;
      }

      if(!contactIdsForBilling.isEmpty())
          {
              List<ShipTo_Address__c> toggleBilling = billToExcluded.isEmpty()?
              [SELECT Id, Primary_Billing_Address__c FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForBilling AND Primary_Billing_Address__c=true]
              :[SELECT Id, Primary_Billing_Address__c FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForBilling AND Primary_Billing_Address__c=true AND Id NOT IN :billToExcluded];
              for(ShipTo_Address__c a : toggleBilling)
              {
                  a.Primary_Billing_Address__c = false;
              }
              update toggleBilling;
          }   

    }
    else //after events
    {       

          List<ShipTo_Address__c> saItemsOfInterest = new List<ShipTo_Address__c>();
          Set<Id> saContactIds = new Set<Id>();

         for ( ShipTo_Address__c s : trigger.new ) 
         {
            if(!s.Primary_Billing_Address__c && !s.Default_Shipping_Address__c) continue;
            saItemsOfInterest.add(s);
            saContactIds.add(s.Contact__c);
         }
         if(saItemsOfInterest.isEmpty()) return;

         Map<Id, Contact> contactsToUpdate = new Map<Id, Contact>([SELECT Id FROM Contact WHERE Id IN:saContactIds]);


         for ( ShipTo_Address__c s : saItemsOfInterest ) 
         { 
         	 Contact c = contactsToUpdate.get(s.Contact__c); 
         	 
         
            if ( s.Primary_Billing_Address__c == true) 
            { 
                  system.debug('***** UPDATING:'+c.Id+' '+s.Address__c+' '+s.City__c); 
        		      if (s.Address2__c != Null)
                  {
                    c.MailingStreet = String.Valueof(s.Address__c) + String.ValueOf(' ') + String.Valueof(s.Address2__c); 
        		      } 
                  else
                  {
        		        c.MailingStreet = s.Address__c;
                  }
                   c.MailingCity = s.City__c; 
                   c.MailingState = s.State__c; 
                   c.MailingCountry = s.Country__c; 
                   c.MailingPostalCode = s.ZIP__c; 
            		            

            } 
            if ( s.Default_Shipping_Address__c == true) 
            { 
                  system.debug('***** UPDATING:'+c.Id+' '+s.Address__c+' '+s.City__c); 
                  if (s.Address2__c != Null)
                  {
                      c.OtherStreet = String.Valueof(s.Address__c) + String.ValueOf(' ') + String.Valueof(s.Address2__c); 
        		      }
                  else
                  {
                    c.OtherStreet = s.Address__c;
                  } 
                   c.OtherCity = s.City__c; 
                   c.OtherState = s.State__c; 
                   c.OtherCountry = s.Country__c; 
                   c.OtherPostalCode = s.ZIP__c; 

            } 
         }
    } 
    update contactsToUpdate.values;
  }  
}

 

All Answers

Roy LuoRoy Luo
Your approach won't work. Static in apex is different from C++, C# or Java. Its scope is only with the request and won't be stateful across requests. SingleExecution.hasAlreadyDone() will always be false every time the trigger fires.

Static variables are only static within the scope of the request. They’re not static across the server, or across the entire organization.
More info here https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_static.htm
Ivan WinzerIvan Winzer
So are you saying i dont need the class, I could add in my flag into the trigger?
Roy LuoRoy Luo
Having a static flag won't help, no matter where you put it. You won't be able to rely on the static flag to get Single Execution. Not sure what you are trying to accomplish. If you define your business needs here, I might be able to help you.
Ivan WinzerIvan Winzer
So Contact is the parent and Shipto_Address is the child with the field Default_Shipping_Address & Primary_Billing_Address (checkboxs). I am trying to make it so if either of the checkboxes is true and a new record is added or updated that the box is unchecked so that there can only be one primary Shipping or BIlling address related to the contact.

so if Amy has 3 relates addresses and:

801 43rd st is primary shipping = true
919 lavy way is primary billing = true

and we add in:
4226 rosewood ave as primary shipping = true

now:
801 43rd st is primary shipping = false

hopefully i was able to clearly give an example of what i am trying to do.Thanks for your help...
Roy LuoRoy Luo
You would need to listen to before events. Try something like this:
 
trigger PrimaryAddressValidation on ShipTo_Address__c (before insert, before update) 
{  
	
    Set<Id> contactIdsForShipping = new Set<Id>();
    Set<Id> contactIdsForBilling = new Set<Id>();

    Set<Id> shipToExcluded = new Set<Id>();
    Set<Id> billToExcluded = new Set<Id>();

    for(ShipTo_Address__c sa : Trigger.new)
    {
      if(trigger.isInsert)
      {
          if(sa.Default_Shipping_Address == true)
          {
            contactIdsForShipping.add(sa.Contact__c);
          }
          if(sa.Primary_Billing_Address == true)
          {
            contactIdsForBilling.add(sa.Contact__c);
          }
      }
      else
      {
        ShipTo_Address__c oldItem = trigger.oldMap.get(sa.Id);
        if(sa.Default_Shipping_Address && !oldItem.Default_Shipping_Address)
        {
            contactIdsForShipping.add(sa.Contact__c);
            shipToExcluded.add(sa.Id);
        }
        if(sa.Primary_Billing_Address && !oldItem.Primary_Billing_Address)
        {
            contactIdsForBilling.add(sa.Contact__c);
            billToExcluded.add(sa.Id);
        }       
      }
    }
   
    if(!contactIdsForShipping.isEmpty())
    {
        List<ShipTo_Address__c> toggleShippings = shipToExcluded.isEmpty()?
           [SELECT Id, Default_Shipping_Address FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForShipping AND Default_Shipping_Address=true]
           : [SELECT Id, Default_Shipping_Address FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForShipping AND Default_Shipping_Address=true AND Id NOT IN :shipToExcluded];

        for(ShipTo_Address__c a : toggleShippings)
        {
                a.Default_Shipping_Address = false;
        }
        update toggleShippings;
    }

    if(!contactIdsForBilling.isEmpty())
    {
        List<ShipTo_Address__c> toggleBilling = billToExcluded.isEmpty()?
        [SELECT Id, Default_Shipping_Address FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForBilling AND Primary_Billing_Address=true AND Id]
        :[SELECT Id, Default_Shipping_Address FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForBilling AND Primary_Billing_Address=true AND Id NOT IN :billToExcluded];
        for(ShipTo_Address__c a : toggleBilling)
        {
            a.Primary_Billing_Address = false;
        }
        update toggleBilling;
    }         
}

 
Roy LuoRoy Luo
The previous post had minor typo. Use this:
 
trigger PrimaryAddressValidation on ShipTo_Address__c (before insert, before update) 
{  
	
    Set<Id> contactIdsForShipping = new Set<Id>();
    Set<Id> contactIdsForBilling = new Set<Id>();

    Set<Id> shipToExcluded = new Set<Id>();
    Set<Id> billToExcluded = new Set<Id>();

    for(ShipTo_Address__c sa : Trigger.new)
    {
      if(trigger.isInsert)
      {
          if(sa.Default_Shipping_Address == true)
          {
            contactIdsForShipping.add(sa.Contact__c);
          }
          if(sa.Primary_Billing_Address == true)
          {
            contactIdsForBilling.add(sa.Contact__c);
          }
      }
      else
      {
        ShipTo_Address__c oldItem = trigger.oldMap.get(sa.Id);
        if(sa.Default_Shipping_Address && !oldItem.Default_Shipping_Address)
        {
            contactIdsForShipping.add(sa.Contact__c);
            shipToExcluded.add(sa.Id);
        }
        if(sa.Primary_Billing_Address && !oldItem.Primary_Billing_Address)
        {
            contactIdsForBilling.add(sa.Contact__c);
            billToExcluded.add(sa.Id);
        }       
      }
    }
   
    if(!contactIdsForShipping.isEmpty())
    {
        List<ShipTo_Address__c> toggleShippings = shipToExcluded.isEmpty()?
           [SELECT Id, Default_Shipping_Address FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForShipping AND Default_Shipping_Address=true]
           : [SELECT Id, Default_Shipping_Address FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForShipping AND Default_Shipping_Address=true AND Id NOT IN :shipToExcluded];

        for(ShipTo_Address__c a : toggleShippings)
        {
                a.Default_Shipping_Address = false;
        }
        update toggleShippings;
    }

    if(!contactIdsForBilling.isEmpty())
    {
        List<ShipTo_Address__c> toggleBilling = billToExcluded.isEmpty()?
        [SELECT Id, Default_Shipping_Address FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForBilling AND Primary_Billing_Address=true]
        :[SELECT Id, Default_Shipping_Address FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForBilling AND Primary_Billing_Address=true AND Id NOT IN :billToExcluded];
        for(ShipTo_Address__c a : toggleBilling)
        {
            a.Primary_Billing_Address = false;
        }
        update toggleBilling;
    }         
}

 
Ivan WinzerIvan Winzer
Thanks Roy,

I found the extra AND In when trying to complie. This is great the code works and is keeing it from having more then one at a time awesome. Now one last question. So i add in the below code (that i already had working) so that it updates the contact fields when the checkbox is checked. I added it to your code and it works but if the address has both checkboxes checked it will only update the billing not the shipping. Should i just leave it as its own trigger or can it be added into this one. My added code is at the bottom. I also left the after functions in as we have an api integration setup with an outside tool that will update the address information.
 
trigger PrimaryAddressValidation on ShipTo_Address__c (before insert, before update, after insert, after update) {
	
	Set<Id> contactIdsForShipping = new Set<Id>();
    Set<Id> contactIdsForBilling = new Set<Id>();

    Set<Id> shipToExcluded = new Set<Id>();
    Set<Id> billToExcluded = new Set<Id>();
    
    Set<Id> contactsToProcess = new Set<Id>();
    list<Contact> contactsToUpdate = new list<Contact>();

    for(ShipTo_Address__c sa : Trigger.new)
    {
    	
    	
      
      if(trigger.isInsert)
      {
          if(sa.Default_Shipping_Address__c == true)
          {
            contactIdsForShipping.add(sa.Contact__c);
         
          }
          if(sa.Primary_Billing_Address__c == true)
          {
            contactIdsForBilling.add(sa.Contact__c);
          }
      }
      else
      {
        ShipTo_Address__c oldItem = trigger.oldMap.get(sa.Id);
        if(sa.Default_Shipping_Address__c && !oldItem.Default_Shipping_Address__c)
        {
            contactIdsForShipping.add(sa.Contact__c);
            shipToExcluded.add(sa.Id);
          
        }
        if(sa.Primary_Billing_Address__c && !oldItem.Primary_Billing_Address__c)
        {
            contactIdsForBilling.add(sa.Contact__c);
            billToExcluded.add(sa.Id);
        }       
       
      }
           
    }
   
    if(!contactIdsForShipping.isEmpty())
    {
        List<ShipTo_Address__c> toggleShippings = shipToExcluded.isEmpty()?
           [SELECT Id, Default_Shipping_Address__c FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForShipping AND Default_Shipping_Address__c=true]
           : [SELECT Id, Default_Shipping_Address__c FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForShipping AND Default_Shipping_Address__c=true AND Id NOT IN :shipToExcluded];

        for(ShipTo_Address__c a : toggleShippings)
        {
                a.Default_Shipping_Address__c = false;
        }
        update toggleShippings;
    }

    if(!contactIdsForBilling.isEmpty())
    {
        List<ShipTo_Address__c> toggleBilling = billToExcluded.isEmpty()?
        [SELECT Id, Primary_Billing_Address__c FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForBilling AND Primary_Billing_Address__c=true]
        :[SELECT Id, Primary_Billing_Address__c FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForBilling AND Primary_Billing_Address__c=true AND Id NOT IN :billToExcluded];
        for(ShipTo_Address__c a : toggleBilling)
        {
            a.Primary_Billing_Address__c = false;
        }
        update toggleBilling;
    }         


   for ( ShipTo_Address__c s : trigger.new ) { 
   	 Contact c = New Contact(Id = s.Contact__c); 
   	 
    if (!contactsToProcess.contains(c.id)){
   	 	
   	 
       if ( s.Primary_Billing_Address__c == true) { 
           system.debug('***** UPDATING:'+c.Id+' '+s.Address__c+' '+s.City__c); 
		if (s.Address2__c != Null){
           c.MailingStreet = String.Valueof(s.Address__c) + String.ValueOf(' ') + String.Valueof(s.Address2__c); 
		} else
		   c.MailingStreet = s.Address__c;
           c.MailingCity = s.City__c; 
           c.MailingState = s.State__c; 
           c.MailingCountry = s.Country__c; 
           c.MailingPostalCode = s.ZIP__c; 
    		contactsToUpdate.add(c);
          update c;              

       } 
       else 
        if ( s.Default_Shipping_Address__c == true) { 
          system.debug('***** UPDATING:'+c.Id+' '+s.Address__c+' '+s.City__c); 
          if (s.Address2__c != Null){
           c.OtherStreet = String.Valueof(s.Address__c) + String.ValueOf(' ') + String.Valueof(s.Address2__c); 
		} else
           c.OtherStreet = s.Address__c; 
           c.OtherCity = s.City__c; 
           c.OtherState = s.State__c; 
           c.OtherCountry = s.Country__c; 
           c.OtherPostalCode = s.ZIP__c; 
     		contactsToUpdate.add(c);
           update c;  

       } 
		contactsToProcess.add(c.id);
   	 }
   } 
	update contactsToUpdate;
}

 
Roy LuoRoy Luo
Wrap codes in before, after sections.
trigger PrimaryAddressValidation on ShipTo_Address__c (before insert, before update, after insert, after update) {
	
  if(trigger.isBefore)
  {
  	Set<Id> contactIdsForShipping = new Set<Id>();
      Set<Id> contactIdsForBilling = new Set<Id>();

      Set<Id> shipToExcluded = new Set<Id>();
      Set<Id> billToExcluded = new Set<Id>();
      
      Set<Id> contactsToProcess = new Set<Id>();
      list<Contact> contactsToUpdate = new list<Contact>();

      for(ShipTo_Address__c sa : Trigger.new)
      {
        
        if(trigger.isInsert)
        {
            if(sa.Default_Shipping_Address__c == true)
            {
              contactIdsForShipping.add(sa.Contact__c);
           
            }
            if(sa.Primary_Billing_Address__c == true)
            {
              contactIdsForBilling.add(sa.Contact__c);
            }
        }
        else
        {
          ShipTo_Address__c oldItem = trigger.oldMap.get(sa.Id);
          if(sa.Default_Shipping_Address__c && !oldItem.Default_Shipping_Address__c)
          {
              contactIdsForShipping.add(sa.Contact__c);
              shipToExcluded.add(sa.Id);
            
          }
          if(sa.Primary_Billing_Address__c && !oldItem.Primary_Billing_Address__c)
          {
              contactIdsForBilling.add(sa.Contact__c);
              billToExcluded.add(sa.Id);
          }       
         
        }
             
      }
     
      if(!contactIdsForShipping.isEmpty())
      {
          List<ShipTo_Address__c> toggleShippings = shipToExcluded.isEmpty()?
             [SELECT Id, Default_Shipping_Address__c FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForShipping AND Default_Shipping_Address__c=true]
             : [SELECT Id, Default_Shipping_Address__c FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForShipping AND Default_Shipping_Address__c=true AND Id NOT IN :shipToExcluded];

          for(ShipTo_Address__c a : toggleShippings)
          {
                  a.Default_Shipping_Address__c = false;
          }
          update toggleShippings;
      }
    }
    else //after events
    {

          if(!contactIdsForBilling.isEmpty())
          {
              List<ShipTo_Address__c> toggleBilling = billToExcluded.isEmpty()?
              [SELECT Id, Primary_Billing_Address__c FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForBilling AND Primary_Billing_Address__c=true]
              :[SELECT Id, Primary_Billing_Address__c FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForBilling AND Primary_Billing_Address__c=true AND Id NOT IN :billToExcluded];
              for(ShipTo_Address__c a : toggleBilling)
              {
                  a.Primary_Billing_Address__c = false;
              }
              update toggleBilling;
          }         


         for ( ShipTo_Address__c s : trigger.new ) { 
         	 Contact c = New Contact(Id = s.Contact__c); 
         	 
          if (!contactsToProcess.contains(c.id)){
         	 	
         	 
             if ( s.Primary_Billing_Address__c == true) { 
                 system.debug('***** UPDATING:'+c.Id+' '+s.Address__c+' '+s.City__c); 
      		if (s.Address2__c != Null){
                 c.MailingStreet = String.Valueof(s.Address__c) + String.ValueOf(' ') + String.Valueof(s.Address2__c); 
      		} else
      		   c.MailingStreet = s.Address__c;
                 c.MailingCity = s.City__c; 
                 c.MailingState = s.State__c; 
                 c.MailingCountry = s.Country__c; 
                 c.MailingPostalCode = s.ZIP__c; 
          		contactsToUpdate.add(c);
                update c;              

             } 
             else 
              if ( s.Default_Shipping_Address__c == true) { 
                system.debug('***** UPDATING:'+c.Id+' '+s.Address__c+' '+s.City__c); 
                if (s.Address2__c != Null){
                 c.OtherStreet = String.Valueof(s.Address__c) + String.ValueOf(' ') + String.Valueof(s.Address2__c); 
      		} else
                 c.OtherStreet = s.Address__c; 
                 c.OtherCity = s.City__c; 
                 c.OtherState = s.State__c; 
                 c.OtherCountry = s.Country__c; 
                 c.OtherPostalCode = s.ZIP__c; 
           		contactsToUpdate.add(c);
                 update c;  

             } 
      		contactsToProcess.add(c.id);
         	 }
         } 
      	update contactsToUpdate;
    }
}

 
Roy LuoRoy Luo
Should be this:
trigger PrimaryAddressValidation on ShipTo_Address__c (before insert, before update, after insert, after update) {
	
  if(trigger.isBefore)
  {
  	Set<Id> contactIdsForShipping = new Set<Id>();
      Set<Id> contactIdsForBilling = new Set<Id>();

      Set<Id> shipToExcluded = new Set<Id>();
      Set<Id> billToExcluded = new Set<Id>();
      
    

      for(ShipTo_Address__c sa : Trigger.new)
      {
        
        if(trigger.isInsert)
        {
            if(sa.Default_Shipping_Address__c == true)
            {
              contactIdsForShipping.add(sa.Contact__c);
           
            }
            if(sa.Primary_Billing_Address__c == true)
            {
              contactIdsForBilling.add(sa.Contact__c);
            }
        }
        else
        {
          ShipTo_Address__c oldItem = trigger.oldMap.get(sa.Id);
          if(sa.Default_Shipping_Address__c && !oldItem.Default_Shipping_Address__c)
          {
              contactIdsForShipping.add(sa.Contact__c);
              shipToExcluded.add(sa.Id);
            
          }
          if(sa.Primary_Billing_Address__c && !oldItem.Primary_Billing_Address__c)
          {
              contactIdsForBilling.add(sa.Contact__c);
              billToExcluded.add(sa.Id);
          }       
         
        }
             
      }
     
      if(!contactIdsForShipping.isEmpty())
      {
          List<ShipTo_Address__c> toggleShippings = shipToExcluded.isEmpty()?
             [SELECT Id, Default_Shipping_Address__c FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForShipping AND Default_Shipping_Address__c=true]
             : [SELECT Id, Default_Shipping_Address__c FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForShipping AND Default_Shipping_Address__c=true AND Id NOT IN :shipToExcluded];

          for(ShipTo_Address__c a : toggleShippings)
          {
                  a.Default_Shipping_Address__c = false;
          }
          update toggleShippings;
      }
   

      if(!contactIdsForBilling.isEmpty())
      {
              List<ShipTo_Address__c> toggleBilling = billToExcluded.isEmpty()?
              [SELECT Id, Primary_Billing_Address__c FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForBilling AND Primary_Billing_Address__c=true]
              :[SELECT Id, Primary_Billing_Address__c FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForBilling AND Primary_Billing_Address__c=true AND Id NOT IN :billToExcluded];
              for(ShipTo_Address__c a : toggleBilling)
              {
                  a.Primary_Billing_Address__c = false;
              }
              update toggleBilling;
          }         
  }
    else //after events
    {
          Map<Id, Contact> contactMap = new Map<Id, Contact>();


         for ( ShipTo_Address__c s : trigger.new ) 
         { 
            if(!contactMap.containsKey(s.Contact__c))
            {
               contactMap.put(s.Contact__c, new Contact(Id = s.Contact__c));
            }
         	 Contact c = contactMap.get(s.Contact__c); 
         	 
          if ( s.Primary_Billing_Address__c == true) 
          { 
                 system.debug('***** UPDATING:'+c.Id+' '+s.Address__c+' '+s.City__c); 
      		  if (s.Address2__c != Null)
            {
                c.MailingStreet = String.Valueof(s.Address__c) + String.ValueOf(' ') + String.Valueof(s.Address2__c); 
            }
            else
            {
      		      c.MailingStreet = s.Address__c;
            }
                 c.MailingCity = s.City__c; 
                 c.MailingState = s.State__c; 
                 c.MailingCountry = s.Country__c; 
                 c.MailingPostalCode = s.ZIP__c; 
               // update c;              

             }
          } 
           
           if ( s.Default_Shipping_Address__c == true) 
           { 
                system.debug('***** UPDATING:'+c.Id+' '+s.Address__c+' '+s.City__c); 
                if (s.Address2__c != Null)
                {
                 c.OtherStreet = String.Valueof(s.Address__c) + String.ValueOf(' ') + String.Valueof(s.Address2__c); 
      		      }
                else
                {
                 c.OtherStreet = s.Address__c; 
                }

                 c.OtherCity = s.City__c; 
                 c.OtherState = s.State__c; 
                 c.OtherCountry = s.Country__c; 
                 c.OtherPostalCode = s.ZIP__c; 
                 //update c;  

             } 
      		
         	 }
         } 
      	update contactMap.values;
    }
}

 
Ivan WinzerIvan Winzer
Thanks Roy. Did get an error though saying that all of my c.other fields are not valid vairables. I think its because the Contact c = new.... is no longer apart of the last if statement
Roy LuoRoy Luo
checking the matching '{', '}'. The compiler should have told you where.
Ivan WinzerIvan Winzer
So all the { } match up, it seems as though this part of the code is not being seen by the last if statement:
if(!contactMap.containsKey(s.Contact__c))
            {
               contactMap.put(s.Contact__c, new Contact(Id = s.Contact__c));
            }
         	 Contact c = contactMap.get(s.Contact__c);

 
Roy LuoRoy Luo
what's the error anyway? Is the compiler complaining or run time error? If it complies, then it is a permission issue, i.e. your account might not have the permissions to access the contact fields. 
Roy LuoRoy Luo
Please mark this post 'solved' to clear it out my tracking stack. Thanks.
Ivan WinzerIvan Winzer
The error read: Description Resource Path Location Type
Compilation error: Variable does not exist: c.Id PrimaryAddressValidation.trigger /Sanbox/src/triggers line 109 Force.com compilation problem
 
Roy LuoRoy Luo
I see. The issue is at Contact c = new Contact(Id=s.Id); The line was there and I was wondering how could that work. For new sObject, could not set Id. 

Try this:
trigger PrimaryAddressValidation on ShipTo_Address__c (before insert, before update, after insert, after update) {
	
  if(trigger.isBefore)
  {
  	Set<Id> contactIdsForShipping = new Set<Id>();
      Set<Id> contactIdsForBilling = new Set<Id>();

      Set<Id> shipToExcluded = new Set<Id>();
      Set<Id> billToExcluded = new Set<Id>();
      
      

      for(ShipTo_Address__c sa : Trigger.new)
      {
        
        if(trigger.isInsert)
        {
            if(sa.Default_Shipping_Address__c == true)
            {
              contactIdsForShipping.add(sa.Contact__c);
           
            }
            if(sa.Primary_Billing_Address__c == true)
            {
              contactIdsForBilling.add(sa.Contact__c);
            }
        }
        else
        {
          ShipTo_Address__c oldItem = trigger.oldMap.get(sa.Id);
          if(sa.Default_Shipping_Address__c && !oldItem.Default_Shipping_Address__c)
          {
              contactIdsForShipping.add(sa.Contact__c);
              shipToExcluded.add(sa.Id);
            
          }
          if(sa.Primary_Billing_Address__c && !oldItem.Primary_Billing_Address__c)
          {
              contactIdsForBilling.add(sa.Contact__c);
              billToExcluded.add(sa.Id);
          }       
         
        }
             
      }
     
      if(!contactIdsForShipping.isEmpty())
      {
          List<ShipTo_Address__c> toggleShippings = shipToExcluded.isEmpty()?
             [SELECT Id, Default_Shipping_Address__c FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForShipping AND Default_Shipping_Address__c=true]
             : [SELECT Id, Default_Shipping_Address__c FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForShipping AND Default_Shipping_Address__c=true AND Id NOT IN :shipToExcluded];

          for(ShipTo_Address__c a : toggleShippings)
          {
                  a.Default_Shipping_Address__c = false;
          }
          update toggleShippings;
      }

      if(!contactIdsForBilling.isEmpty())
          {
              List<ShipTo_Address__c> toggleBilling = billToExcluded.isEmpty()?
              [SELECT Id, Primary_Billing_Address__c FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForBilling AND Primary_Billing_Address__c=true]
              :[SELECT Id, Primary_Billing_Address__c FROM ShipTo_Address__c WHERE Contact__c IN :contactIdsForBilling AND Primary_Billing_Address__c=true AND Id NOT IN :billToExcluded];
              for(ShipTo_Address__c a : toggleBilling)
              {
                  a.Primary_Billing_Address__c = false;
              }
              update toggleBilling;
          }   

    }
    else //after events
    {       

          List<ShipTo_Address__c> saItemsOfInterest = new List<ShipTo_Address__c>();
          Set<Id> saContactIds = new Set<Id>();

         for ( ShipTo_Address__c s : trigger.new ) 
         {
            if(!s.Primary_Billing_Address__c && !s.Default_Shipping_Address__c) continue;
            saItemsOfInterest.add(s);
            saContactIds.add(s.Contact__c);
         }
         if(saItemsOfInterest.isEmpty()) return;

         Map<Id, Contact> contactsToUpdate = new Map<Id, Contact>([SELECT Id FROM Contact WHERE Id IN:saContactIds]);


         for ( ShipTo_Address__c s : saItemsOfInterest ) 
         { 
         	 Contact c = contactsToUpdate.get(s.Contact__c); 
         	 
         
            if ( s.Primary_Billing_Address__c == true) 
            { 
                  system.debug('***** UPDATING:'+c.Id+' '+s.Address__c+' '+s.City__c); 
        		      if (s.Address2__c != Null)
                  {
                    c.MailingStreet = String.Valueof(s.Address__c) + String.ValueOf(' ') + String.Valueof(s.Address2__c); 
        		      } 
                  else
                  {
        		        c.MailingStreet = s.Address__c;
                  }
                   c.MailingCity = s.City__c; 
                   c.MailingState = s.State__c; 
                   c.MailingCountry = s.Country__c; 
                   c.MailingPostalCode = s.ZIP__c; 
            		            

            } 
            if ( s.Default_Shipping_Address__c == true) 
            { 
                  system.debug('***** UPDATING:'+c.Id+' '+s.Address__c+' '+s.City__c); 
                  if (s.Address2__c != Null)
                  {
                      c.OtherStreet = String.Valueof(s.Address__c) + String.ValueOf(' ') + String.Valueof(s.Address2__c); 
        		      }
                  else
                  {
                    c.OtherStreet = s.Address__c;
                  } 
                   c.OtherCity = s.City__c; 
                   c.OtherState = s.State__c; 
                   c.OtherCountry = s.Country__c; 
                   c.OtherPostalCode = s.ZIP__c; 

            } 
         }
    } 
    update contactsToUpdate.values;
  }  
}

 
This was selected as the best answer
Ivan WinzerIvan Winzer
Thanks Roy your awesome. This now complies and works. One thing i do notice is now i cant do a bulk update on the backend. Im wondering if i need to add in an update to the list (saItemsOfInterest) or the id set of (saContactIds) after the for loop to allow for bulk loads to take place. Im going to try and see if that allows me to data load.

Thanks again for all your help
Ivan WinzerIvan Winzer
Oh and not sure how to do the kuddos but i liked all your responses :)
Roy LuoRoy Luo
If you figure out how to do kuddos and tell me the steps, I will kuddos you.