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
BennettTranBennettTran 

trigger only when certain field is changed

Hello,

I have the following trigger but I only want the trigger to fire when the parent case field is changed.  Can someone please assist?  Thanks in advanced.





trigger UpdateChildCase on Case (before update) {

List<Id> parentCaseIds = new List<Id>();

RecordType rt = [select id from RecordType where DeveloperName='Integration' and SobjectType='Case'];

for(Case c : Trigger.new)
{
if(c.RecordTypeId==rt.id)
{
  parentCaseIds.add(c.ParentId);
}
}

//Query the parentcase Values,yoou need to query all fields which you want to be populated in yiur child case
Map<Id,Case> caseMap =new Map<Id,Case> ([select id, Average_visits_per_week__c, Biller_attending_training__c, Billing_company__c, Billing_contact_email__c, Billing_contact_first_name__c, Billing_Contact_Last_Name__c, Billing_Contact_Phone__c,
Billing_Integration_Choice__c, Billing_Integration_required__c, Clinic_Business_Hours__c, Clinic_ID__c, Clinic_Implementation_Contact_s__c, Clinic_Name__c, Clinic_Owner__c,
Clinic_Primary_Contact_s__c, Company_ID__c, Computer_Equipment__c, ContactId, Current_Billing_Method__c, Description, Document_Existing_Patients__c, eDoc_Only__c, EMR_or_Paper__c, Future_Billing_Software__c, Imports_Required__c,
Import_Contact_Email__c, Import_Contact_Name__c, Import_Type__c, Integration_Comments__c, Member_Implementation_Plan__c, Multiple_TaxIDs__c, Opportunity__c, Order_Confirmation_Date__c, Origin, Other_Decision_Maker_s__c, Preferred_Go_Live_Date__c,
Previous_Billing_Software__c, Previous_EMR_Software__c, Priority, PTA_Cosign_Required__c, Reason, Specialties__c, Startup_or_Existing_Clinic__c from Case where Id in:parentCaseIds]);

//populate the child case with related Parentcase values.

for(Case c : Trigger.new)
{
if(!caseMap.IsEmpty())
{
  c.Origin = caseMap.get(c.ParentId).Origin;
  c.Subject = caseMap.get(c.ParentId).Subject;
  c.Average_visits_per_week__c = casemap.get(c.ParentId).Average_visits_per_week__c;
  c.Biller_attending_training__c = caseMap.get(c.ParentId).Biller_attending_training__c;
  c.Billing_contact_email__c = caseMap.get(c.ParentId).Billing_contact_email__c;
  c.Billing_contact_first_name__c = caseMap.get(c.ParentId).Billing_contact_first_name__c;
  c.Billing_Contact_Last_Name__c = caseMap.get(c.ParentId).Billing_Contact_Last_Name__c;
  c.Billing_Contact_Phone__c = caseMap.get(c.ParentId).Billing_Contact_Phone__c;
  c.Billing_Integration_Choice__c = caseMap.get(c.ParentId).Billing_Integration_Choice__c;
  c.Billing_Integration_required__c = caseMap.get(c.ParentId).Billing_Integration_required__c;
  c.Clinic_Business_Hours__c = caseMap.get(c.ParentId).Clinic_Business_Hours__c;
}
}
}
Best Answer chosen by BennettTran
Vinit_KumarVinit_Kumar
Benett ,

Try below code :- 

trigger UpdateChildCase on Case (before update) {

List<Id> parentCaseIds = new List<Id>();

RecordType rt = [select id from RecordType where DeveloperName='Integration' and SobjectType='Case'];

for(Case c : Trigger.new)
{
if(c.RecordTypeId==rt.id && c.ParentId!=trigger.oldMap.get(c.id).ParentId)
{
  parentCaseIds.add(c.ParentId);
}
}

//Query the parentcase Values,yoou need to query all fields which you want to be populated in yiur child case
Map<Id,Case> caseMap =new Map<Id,Case> ([select id, Average_visits_per_week__c, Biller_attending_training__c, Billing_company__c, Billing_contact_email__c, Billing_contact_first_name__c, Billing_Contact_Last_Name__c, Billing_Contact_Phone__c,
Billing_Integration_Choice__c, Billing_Integration_required__c, Clinic_Business_Hours__c, Clinic_ID__c, Clinic_Implementation_Contact_s__c, Clinic_Name__c, Clinic_Owner__c,
Clinic_Primary_Contact_s__c, Company_ID__c, Computer_Equipment__c, ContactId, Current_Billing_Method__c, Description, Document_Existing_Patients__c, eDoc_Only__c, EMR_or_Paper__c, Future_Billing_Software__c, Imports_Required__c,
Import_Contact_Email__c, Import_Contact_Name__c, Import_Type__c, Integration_Comments__c, Member_Implementation_Plan__c, Multiple_TaxIDs__c, Opportunity__c, Order_Confirmation_Date__c, Origin, Other_Decision_Maker_s__c, Preferred_Go_Live_Date__c,
Previous_Billing_Software__c, Previous_EMR_Software__c, Priority, PTA_Cosign_Required__c, Reason, Specialties__c, Startup_or_Existing_Clinic__c from Case where Id in:parentCaseIds]);

//populate the child case with related Parentcase values.

for(Case c : Trigger.new)
{
if(!caseMap.IsEmpty() && c.ParentId!=trigger.oldMap.get(c.id).ParentId)
{
  c.Origin = caseMap.get(c.ParentId).Origin;
  c.Subject = caseMap.get(c.ParentId).Subject;
  c.Average_visits_per_week__c = casemap.get(c.ParentId).Average_visits_per_week__c;
  c.Biller_attending_training__c = caseMap.get(c.ParentId).Biller_attending_training__c;
  c.Billing_contact_email__c = caseMap.get(c.ParentId).Billing_contact_email__c;
  c.Billing_contact_first_name__c = caseMap.get(c.ParentId).Billing_contact_first_name__c;
  c.Billing_Contact_Last_Name__c = caseMap.get(c.ParentId).Billing_Contact_Last_Name__c;
  c.Billing_Contact_Phone__c = caseMap.get(c.ParentId).Billing_Contact_Phone__c;
  c.Billing_Integration_Choice__c = caseMap.get(c.ParentId).Billing_Integration_Choice__c;
  c.Billing_Integration_required__c = caseMap.get(c.ParentId).Billing_Integration_required__c;
  c.Clinic_Business_Hours__c = caseMap.get(c.ParentId).Clinic_Business_Hours__c;
}
}
}


All Answers

vmanumachu1.393650924860069E12vmanumachu1.393650924860069E12
You could write a condition at the beginning of your trigger to compare the Old and new values. Something like the below:

Map<Id,Case> oldCaseMap = Trigger.oldMap;
for(Case c : Trigger.new)
{
      Case oldcase = oldCaseMap.get(c.id);
      if(c.ParentId <> oldCase.ParentId)
      {

      }
}

Hope it helps. Please mark as best answer if it works. It helps others.
Vinit_KumarVinit_Kumar
Benett ,

Try below code :- 

trigger UpdateChildCase on Case (before update) {

List<Id> parentCaseIds = new List<Id>();

RecordType rt = [select id from RecordType where DeveloperName='Integration' and SobjectType='Case'];

for(Case c : Trigger.new)
{
if(c.RecordTypeId==rt.id && c.ParentId!=trigger.oldMap.get(c.id).ParentId)
{
  parentCaseIds.add(c.ParentId);
}
}

//Query the parentcase Values,yoou need to query all fields which you want to be populated in yiur child case
Map<Id,Case> caseMap =new Map<Id,Case> ([select id, Average_visits_per_week__c, Biller_attending_training__c, Billing_company__c, Billing_contact_email__c, Billing_contact_first_name__c, Billing_Contact_Last_Name__c, Billing_Contact_Phone__c,
Billing_Integration_Choice__c, Billing_Integration_required__c, Clinic_Business_Hours__c, Clinic_ID__c, Clinic_Implementation_Contact_s__c, Clinic_Name__c, Clinic_Owner__c,
Clinic_Primary_Contact_s__c, Company_ID__c, Computer_Equipment__c, ContactId, Current_Billing_Method__c, Description, Document_Existing_Patients__c, eDoc_Only__c, EMR_or_Paper__c, Future_Billing_Software__c, Imports_Required__c,
Import_Contact_Email__c, Import_Contact_Name__c, Import_Type__c, Integration_Comments__c, Member_Implementation_Plan__c, Multiple_TaxIDs__c, Opportunity__c, Order_Confirmation_Date__c, Origin, Other_Decision_Maker_s__c, Preferred_Go_Live_Date__c,
Previous_Billing_Software__c, Previous_EMR_Software__c, Priority, PTA_Cosign_Required__c, Reason, Specialties__c, Startup_or_Existing_Clinic__c from Case where Id in:parentCaseIds]);

//populate the child case with related Parentcase values.

for(Case c : Trigger.new)
{
if(!caseMap.IsEmpty() && c.ParentId!=trigger.oldMap.get(c.id).ParentId)
{
  c.Origin = caseMap.get(c.ParentId).Origin;
  c.Subject = caseMap.get(c.ParentId).Subject;
  c.Average_visits_per_week__c = casemap.get(c.ParentId).Average_visits_per_week__c;
  c.Biller_attending_training__c = caseMap.get(c.ParentId).Biller_attending_training__c;
  c.Billing_contact_email__c = caseMap.get(c.ParentId).Billing_contact_email__c;
  c.Billing_contact_first_name__c = caseMap.get(c.ParentId).Billing_contact_first_name__c;
  c.Billing_Contact_Last_Name__c = caseMap.get(c.ParentId).Billing_Contact_Last_Name__c;
  c.Billing_Contact_Phone__c = caseMap.get(c.ParentId).Billing_Contact_Phone__c;
  c.Billing_Integration_Choice__c = caseMap.get(c.ParentId).Billing_Integration_Choice__c;
  c.Billing_Integration_required__c = caseMap.get(c.ParentId).Billing_Integration_required__c;
  c.Clinic_Business_Hours__c = caseMap.get(c.ParentId).Clinic_Business_Hours__c;
}
}
}


This was selected as the best answer