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
Jyosi jyosiJyosi jyosi 

Trigger works fine i write whole trigger on a object,but i am trying to write in apex class and call that method

Hello Everyone,

I am trying to write the code using context variable,
I wirote the trigger code it works fine,but i need to write the logic in Apex class and call that method in trigger

Here is my trigger code.

trigger Rephistorytracking on contact (after update) {
List<Schema.FieldSetMember> trackedFields =     SObjectType.contact.FieldSets.HistoryTracking.getFields();
if (trackedFields.isEmpty()) return;

 List<RepHistory__c> fieldChanges = new List<RepHistory__c>();

 List<string> apiNameList = new List<string>();        

if(Trigger.isUpdate){
for (contact aNew : trigger.new) {

contact aOld = trigger.oldmap.get(aNew.Id);

for (Schema.FieldSetMember fsm : trackedFields) {

 String fieldName  = fsm.getFieldPath();
String fieldLabel = fsm.getLabel();

if (aNew.get(fieldName) != aOld.get(fieldName)) {

String oldValue = String.valueOf(aOld.get(fieldName));
String newValue = String.valueOf(aNew.get(fieldName));
if (oldValue != null && oldValue.length()>255) oldValue = oldValue.substring(0,255);
 if (newValue != null && newValue.length()>255) newValue = newValue.substring(0,255); 

RepHistory__c aht = new RepHistory__c();
aht.name         = fieldLabel;
aht.apiName__c   = fieldName;
aht.ChangedBy__c = UserInfo.getUserName();
aht.OldValue__c  = oldValue;
aht.NewValue__c  = newValue;
aht.Contact__c=aOld.Id;

 apiNameList.add(aht.apiName__c);
 fieldChanges.add(aht);
}        
}
}
}
if (!fieldChanges.isEmpty()) {
 insert fieldChanges;
 }
 }

could any help me out how to write the whole logic in Apex Class

Thnaks for the help.

Regards,
Jyo
Rahul BorgaonkarRahul Borgaonkar
Check this link

http://www.forcexplore.com/2014/07/calling-apex-code-in-trigger-salesforce.html
 
AM2014AM2014
Hi,

You can write a public class

ex: -
Public with sharing class trackingService{

   public static void PopulateRepHistoryTracking(list<RepHistory__C historyRecords> {

   //do all your coding

}

}
and in the trigger

trigger Rephistorytracking on contact (after update) {

if(Trigger.isUpdate){

f(Trigger.isUpdate){
for (contact aNew : trigger.new) {

contact aOld = trigger.oldmap.get(aNew.Id);

for (Schema.FieldSetMember fsm : trackedFields) {

 String fieldName  = fsm.getFieldPath();
String fieldLabel = fsm.getLabel();

if (aNew.get(fieldName) != aOld.get(fieldName)) {

String oldValue = String.valueOf(aOld.get(fieldName));
String newValue = String.valueOf(aNew.get(fieldName));
if (oldValue != null && oldValue.length()>255) oldValue = oldValue.substring(0,255);
 if (newValue != null && newValue.length()>255) newValue = newValue.substring(0,255); 
trackingService.PopulateRepHistoryTracking(your list);
}

}
  Hope this helps you.



 
Jyosi jyosiJyosi jyosi
Thanks it got figured .I have code it