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
SFDC 2017SFDC 2017 

Dynamically field update check using custom settings

Hi All,

I need a help regarding Custom Settings.In Contact we have fields like Firstname,lastname,Email,City etc.So i will store this specified fields in a custom settings (because in future these fields may be change so storing in custom setting )So whenever only this specified field values are updated in contact then only i need to create a record into another object .If other fields are updated it shouldnot trigger.I am not sure how to compare these field changes through Custom Settings and object Contact.

Anyone can give me some idea to get this scenario done.

Thanks in advance
Best Answer chosen by SFDC 2017
Asif Ali MAsif Ali M
Please find the below code snippet to check whether the fields in fieldSet are changed or not. You need to implement the method isFieldChanged(triggerOldRecord, triggerRecord)
 
// triggerRecord is your Contact record
// triggerOldRecord is your old contact Record
// TrackUpdate is the fieldset name on Contact

Boolean isChanged = false;
Schema.FieldSet fieldSet = Schema.SObjectType.Contact.fieldSets.getMap().get('TrackUpdate');
Schema.DescribeSObjectResult describe = triggerRecord.getSObjectType().getDescribe();
for(Schema.FieldSetMember fsMember : fieldSet.getFields()) {
    String fieldName = fsMember.getFieldPath() 
    if (isFieldChanged(triggerOldRecord, triggerRecord, fieldName) == true) {
        isChanged = true;
    }
}

system.debug('Field Changed'+isChanged);


if ( isChanged == true) {

// Execute the below code when fields are updated.
}





 

All Answers

Asif Ali MAsif Ali M
Hi SFDC 2017,

If your need is to trigger a piece of code only when some FIELD values are changed on Contact then here is the approach.
  1. Create a FieldSet on the Contact and name it appropriately.
  2. Add the fields you want to track updates to this new FieldSet
  3. Programatically retrive the FieldSet fields and check if it is modified or not and do whatever you want to do on modify.
Syntax for retriving FieldSet:
Schema.FieldSet fieldsToTrack = Schema.SObjectType.Contact.fieldSets.getMap().get('<FieldSetName>');






 
SFDC 2017SFDC 2017
Thanks Asif. But i have not used field set before. Can you provide any sample code for my better understanding if you don't mind
Asif Ali MAsif Ali M
Please find the below code snippet to check whether the fields in fieldSet are changed or not. You need to implement the method isFieldChanged(triggerOldRecord, triggerRecord)
 
// triggerRecord is your Contact record
// triggerOldRecord is your old contact Record
// TrackUpdate is the fieldset name on Contact

Boolean isChanged = false;
Schema.FieldSet fieldSet = Schema.SObjectType.Contact.fieldSets.getMap().get('TrackUpdate');
Schema.DescribeSObjectResult describe = triggerRecord.getSObjectType().getDescribe();
for(Schema.FieldSetMember fsMember : fieldSet.getFields()) {
    String fieldName = fsMember.getFieldPath() 
    if (isFieldChanged(triggerOldRecord, triggerRecord, fieldName) == true) {
        isChanged = true;
    }
}

system.debug('Field Changed'+isChanged);


if ( isChanged == true) {

// Execute the below code when fields are updated.
}





 
This was selected as the best answer
SFDC 2017SFDC 2017
Thanks Asif:)