You need to sign in to do that
Don't have an account?
LorrMc
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 :-)
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 :-)
Change Contact__c to var.ContactID
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
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.
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.
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