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
LorrMcLorrMc 

Trigger to mark all previous records as inactive if new record created.

I am soooo not used to triggers so I found this one and tried to edit it to meet my needs but I am getting the error below. 

Error: Compile Error: Variable does not exist: contact__c at line 20 column 35

I have created a new object that will be a related list on the contact, what I need is that when a new record is created all previous records are marked inactive, I have a tick box Active which should only be ticked for the current record. 

Please see trigger below or if anyone can think of a better all advice is welcome. 

trigger TriggeronCarePlan on Care_Plan_Details__c (before insert, before update) {
    
    TriggerCarePlanHandler handler =  new TriggerCarePlanHandler();
    
   if(trigger.isUpdate || trigger.isInsert)
   {
       handler.UpdateActivestatus(Trigger.new);
       
   }
 public class TriggerCarePlanHandler {
    
    set<id> allContactIDs = new set<id>();
    List<Care_Plan_Details__c> allrelatedCarePlans = new List<Care_Plan_Details__c>();
    List<Care_Plan_Details__c> updateRecords = new List<Care_Plan_Details__c>();
    
  public void  UpdateActivestatus (List<Care_Plan_Details__c> Triggernew)
    {        
        for (Care_Plan_Details__c var : Triggernew)
        {
            allContactIDs.add(var.contact__c);
        }        
    
       allrelatedCarePlans = [select id, Active_Inactive__c,contact__c from Care_Plan_Details__c where contact__c in:allContactIDs ];
     
       
       for (Care_Plan_Details__c var : allrelatedCarePlans) 
           {
                   Care_Plan_Details__c record = new Care_Plan_Details__c();
                    record.Id = var.id;
                    Record.Active_Inactive__c = false;
               updateRecords.add(record);               
           }
        
        if (!updateRecords.isEmpty())
        { 
            update updateRecords;
        }
    }
}}

Thank you :-)
 
VamsiVamsi
Hi,

Change Contact__c to var.ContactID 
LorrMcLorrMc
Hi Vamsi, 
Thank you for response, I have done that and it has now returned the following error. 
Error: Compile Error: Variable does not exist: ContactID at line 20 column 35 

trigger TriggeronCarePlan on Care_Plan_Details__c (before insert, before update) {
    
    TriggerCarePlanHandler handler =  new TriggerCarePlanHandler();
    
   if(trigger.isUpdate || trigger.isInsert)
   {
       handler.UpdateActivestatus(Trigger.new);
       
   }
 public class TriggerCarePlanHandler {
    
    set<id> allContactIDs = new set<id>();
    List<Care_Plan_Details__c> allrelatedCarePlans = new List<Care_Plan_Details__c>();
    List<Care_Plan_Details__c> updateRecords = new List<Care_Plan_Details__c>();
    
  public void  UpdateActivestatus (List<Care_Plan_Details__c> Triggernew)
    {        
        for (Care_Plan_Details__c var : Triggernew)
        {
            allContactIDs.add(var.ContactID );
        }        
    
       allrelatedCarePlans = [select id, Active_Inactive__c,contact__c from Care_Plan_Details__c where contact__c in:allContactIDs ];
     
       
       for (Care_Plan_Details__c var : allrelatedCarePlans) 
           {
                   Care_Plan_Details__c record = new Care_Plan_Details__c();
                    record.Id = var.id;
                    Record.Active_Inactive__c = false;
               updateRecords.add(record);               
           }
        
        if (!updateRecords.isEmpty())
        { 
            update updateRecords;
        }
    }
}}

Lorr
VamsiVamsi
Does Care_Plan_Details__c object has relationship with contact if so make sure to insert the API name of the contact
VamsiVamsi
add a debug statement for the below code for (Care_Plan_Details__c var : Triggernew) { system.debug('care pla details'+' '+ var); // see what are all the details are being populated for contact. //allContactIDs.add(var.ContactID ); }
LorrMcLorrMc
Hi 
Yes there is a master detail relationship between the Care Plan Details and the Contact, as it is the standard contact is the API not contact__s also I have just tried this but same error received. 
When you say debug statement do I just add
 (Care_Plan_Details__c var : Triggernew) { system.debug('care pla details'+' '+ var); // see what are all the details are being populated for contact. //allContactIDs.add(var.ContactID ); } 
below 
for (Care_Plan_Details__c var : Triggernew)
        {

sorry I am really new to this. 
Than you again for your help.
 
VamsiVamsi
Yes simply add that debug statement and view the log in developer console
LorrMcLorrMc
Ok, I have done that and now I am getting an error as below, have I added the statement incorrectly?
Error: Compile Error: Unexpected token 'public'. at line 10 column 2


trigger TriggeronCarePlan on Care_Plan_Details__c (before insert, before update) {
    
    TriggerCarePlanHandler handler =  new TriggerCarePlanHandler();
    
   if(trigger.isUpdate || trigger.isInsert)
   {
       handler.UpdateActivestatus(Trigger.new);
       
   }
 public class TriggerCarePlanHandler {
    
    set<id> allContactIDs = new set<id>();
    List<Care_Plan_Details__c> allrelatedCarePlans = new List<Care_Plan_Details__c>();
    List<Care_Plan_Details__c> updateRecords = new List<Care_Plan_Details__c>();
    
  public void  UpdateActivestatus (List<Care_Plan_Details__c> Triggernew)
    {        
        for (Care_Plan_Details__c var : Triggernew)
        {
            allContactIDs.add(var.ContactID );
        }  
           (Care_Plan_Details__c var : Triggernew) { system.debug('care pla details'+' '+ var); // see what are all the details are being populated for contact. //allContactIDs.add(var.ContactID ); }
    
       allrelatedCarePlans = [select id, Active_Inactive__c,contact__c from Care_Plan_Details__c where contact__c in:allContactIDs ];
     
       
       for (Care_Plan_Details__c var : allrelatedCarePlans) 
           {
                   Care_Plan_Details__c record = new Care_Plan_Details__c();
                    record.Id = var.id;
                    Record.Active_Inactive__c = false;
               updateRecords.add(record);               
           }
        
        if (!updateRecords.isEmpty())
        { 
            update updateRecords;
        }
    }
}}

thank you for your time with this.
VamsiVamsi
Hi, I tried changing the Care_Plan_Details__c to Case to test the class and its working fine for me ... change contact__c to ContactID in the below query as well allrelatedCarePlans = [select id, Active_Inactive__c,contact__c from Care_Plan_Details__c where contact__c in:allContactIDs ];
LorrMcLorrMc
Hi Thnak you , 
the contactID seems to be fine  but error is now highlighting the 'Public' part, so sorry about this I am really at a loss with this. 
Error: Compile Error: Unexpected token 'public'. at line 9 column 2
VamsiVamsi
Hi, I'm not sure what causing it to throw at the public keyword. You can contact me at mvamshikrishna99@gmail.com so that we can continue from there..
LorrMcLorrMc
Thank you :-)