i have desing a code for your requiremnt copy and paste this and create a new class in you org, this class have a method ("getCreatableFieldsSOQL") that we are going to call from trigger method is just taking a string as arrgument that is the name of object on which trigger in written
Apex Class :-
Public Class GetWritableFields { public static list<string> getCreatableFieldsSOQL(String objectName) {
String selects = ''; // Get a map of field name and field token Map<String, Schema.SObjectField> fMap = Schema.getGlobalDescribe().get(objectName.toLowerCase()).getDescribe().Fields.getMap(); list<string> selectFields = new list<string>();
if (fMap != null){ for (Schema.SObjectField ft : fMap.values()){ // loop through all field tokens (ft) Schema.DescribeFieldResult fd = ft.getDescribe(); // describe each field (fd) if (fd.isCreateable()){ // field is creatable selectFields.add(fd.getName()); } } } return selectFields;
} }
After saving this class there is a code you need to do in trigger Trigger Code :-
and then just use this code in you trigger in the end of code you will have a map "FieldChangeForTheRecord" which will have the Id of Record as key and the list of field Name change for that record
string selects =''; if (!WritableFields.isEmpty()){ for (string s:WritableFields){ selects += s + ','; } if (selects.endsWith(',')){selects = selects.substring(0,selects.lastIndexOf(','));}
} String IdSetStr = '('; for(<Your Object Name> TempObj : Trigger.New) { IdSetStr = IdSetStr + '\''+TempObj+'\''; IdSetStr = IdSetStr + ','; } if (IdSetStr.endsWith(',')) { IdSetStr = IdSetStr.substring(0,IdSetStr.lastIndexOf(',')); } IdSetStr = = IdSetStr +')'; String soql = 'SELECT ' + selects + ' FROM ' + objectName + ' WHERE id in' + IdSetStr; list<Sobject> UpdatedRecordSobjLst = Database.query(soql); Map<Id,List<string>> FieldChangeForTheRecord = new Map<Id,List<string>>(); for(String TempFieldName : WritableFields) { for(Sobject NewRecordObj : UpdatedRecordSobjLst) { Sobject TempOldSobject = (Sobject)Trigger.OldMap.get(NewRecordObj.Id); if(NewRecordObj.get(TempFieldName) != TempOldSobject.get(TempFieldName)) { if(FieldChangeForTheRecord.containskey()) { list<String> TempLst = FieldChangeForTheRecord.get(NewRecordObj.Id); TempLst.add(TempFieldName); FieldChangeForTheRecord.put(NewRecordObj.Id,TempLst); } else { list<String> TempLst = new list<String>(); TempLst.add(TempFieldName); FieldChangeForTheRecord.put(NewRecordObj.Id,TempLst); } } } } // FieldChangeForTheRecord Map will have the Id of Record as key and the list of field Name change for that record
If you like this answer please accept it.
copy and paste this and create a new class in you org, this class have a method ("getCreatableFieldsSOQL") that we are going to call from trigger
method is just taking a string as arrgument that is the name of object on which trigger in written
Apex Class :-
Public Class GetWritableFields
{
public static list<string> getCreatableFieldsSOQL(String objectName)
{
String selects = '';
// Get a map of field name and field token
Map<String, Schema.SObjectField> fMap = Schema.getGlobalDescribe().get(objectName.toLowerCase()).getDescribe().Fields.getMap();
list<string> selectFields = new list<string>();
if (fMap != null){
for (Schema.SObjectField ft : fMap.values()){ // loop through all field tokens (ft)
Schema.DescribeFieldResult fd = ft.getDescribe(); // describe each field (fd)
if (fd.isCreateable()){ // field is creatable
selectFields.add(fd.getName());
}
}
}
return selectFields;
}
}
After saving this class there is a code you need to do in trigger
Trigger Code :-
list<string> WritableFields = GetWritableFields.getCreatableFieldsSOQL('<Your Object Name>'); // Replace "<Your Object Name>"
and then just use this code in you trigger in the end of code you will have a map "FieldChangeForTheRecord" which will have the Id of Record as key and the list of field Name change for that record
string selects ='';
if (!WritableFields.isEmpty()){
for (string s:WritableFields){
selects += s + ',';
}
if (selects.endsWith(',')){selects = selects.substring(0,selects.lastIndexOf(','));}
}
String IdSetStr = '(';
for(<Your Object Name> TempObj : Trigger.New)
{
IdSetStr = IdSetStr + '\''+TempObj+'\'';
IdSetStr = IdSetStr + ',';
}
if (IdSetStr.endsWith(','))
{
IdSetStr = IdSetStr.substring(0,IdSetStr.lastIndexOf(','));
}
IdSetStr = = IdSetStr +')';
String soql = 'SELECT ' + selects + ' FROM ' + objectName + ' WHERE id in' + IdSetStr;
list<Sobject> UpdatedRecordSobjLst = Database.query(soql);
Map<Id,List<string>> FieldChangeForTheRecord = new Map<Id,List<string>>();
for(String TempFieldName : WritableFields)
{
for(Sobject NewRecordObj : UpdatedRecordSobjLst)
{
Sobject TempOldSobject = (Sobject)Trigger.OldMap.get(NewRecordObj.Id);
if(NewRecordObj.get(TempFieldName) != TempOldSobject.get(TempFieldName))
{
if(FieldChangeForTheRecord.containskey())
{
list<String> TempLst = FieldChangeForTheRecord.get(NewRecordObj.Id);
TempLst.add(TempFieldName);
FieldChangeForTheRecord.put(NewRecordObj.Id,TempLst);
}
else
{
list<String> TempLst = new list<String>();
TempLst.add(TempFieldName);
FieldChangeForTheRecord.put(NewRecordObj.Id,TempLst);
}
}
}
}
// FieldChangeForTheRecord Map will have the Id of Record as key and the list of field Name change for that record