I have an apex trigger (before insert/update) and a helper class to that trigger. The problem is: When creating an object record, the trigger should check if the AddedDate field is filled and if it's not - then assign it today's date and current time.
And when I create and update a Product object record, the trigger must check the length of the Description field, if the field is longer than 200 characters, I must trim it to 197 characters and add a triple to the end of the line. What am I doing wrong and how should I proceed?
My trigger:
trigger ProductTrigger on Product__c (before insert, before update) {
if(Trigger.isUpdate && Trigger.isAfter){
ProductTriggerHelper.producthandler(Trigger.new);
}
}
Trigger helper class:
public class ProductTriggerHelper {
public static void producthandler(List<Product__c> products) {
Schema.DescribeFieldResult F = Product__c.Description__c.getDescribe();
Integer lengthOfField = F.getLength();
//List<Product__c> prList = new list<Product__c>();
for(Product__c pr: products){
pr.AddedDate__c=system.today();
if (String.isNotEmpty(pr.Description__c)) {
pr.Description__c = pr.Description__c.abbreviate(lengthOfField);
}
}
}
}