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 existing code for only 1 primary CC

So i have the following trigger that is suppose to create a new CC record in SF and if primary update the old card marked as primary and make it so only 1 primary card can exist at a time. Not sure if it broke or im missing something but now we can have more than 1 primary cc selected in the related list. Can anyone help me figure out what is wrong with the code:
 
Trigger to set the Primary wine club card on the contact record.
// Check if the flag is set on both inserts and updates, and if so
// set those fields on contact.
//
trigger CredCardTrigger on authnet_credit_card__c (After Insert, After Update) { 
    List<authnet_credit_card__c > slist = trigger.New; 
    List<authnet_credit_card__c> oldPrimaryList = new List<authnet_credit_card__c>(); 
    List<Contact> contactsToUpdate = new List<Contact>(); 

    for ( authnet_credit_card__c a : slist ) {  
        if ( a.isPrimaryWineClubCard__c == true) { 
            system.debug('Copying in primary Auth id '+a.Payment_Id__c); 
            Contact c = New Contact(Id = a.Contact__c); 

            if (c.authnet_profile_id__c != null) { 
                if (c.authnet_profile_id__c != a.Profile_Id__c) { 
                    authnet_credit_card__c oldPrimary = new authnet_credit_card__c(Profile_Id__c = c.authnet_profile_id__c); 
                    system.debug('Unchecking old authnet primary id '+a.Profile_Id__c); 
                    oldPrimary.IsPrimaryWineClubCard__c = false; 
                    oldPrimaryList.add(oldPrimary); 
                } 
            } 

            c.authnet_profile_id__c = a.Profile_Id__c; 
            c.authnet_default_payment_id__c = a.Payment_Id__c; 
            c.Authnet_Ex_Date__c = a.Expiration_Date__c;
            contactsToUpdate.add(c); 

        } 
    } 

   if (!oldPrimaryList.isEmpty()) { 
        update oldPrimaryList; 

    } 
    if (!contactsToUpdate.isEmpty()) { 
        update contactsToUpdate; 

    } 

}

Ivan