You need to sign in to do that
Don't have an account?
Adesh Tiwari
Hello friends! I am new to Apex, facing problem while writing code for the below scenario:
An Object Laptop is having fields Hard disk, Processor,RAM, and Capacity.
Requirement is to populate field Capacity as 'Low Capacity' when
Hard disk < 40GB, Processor < 2 GHz and RAM < 1 GB whenever record is Inserted/Edited.
I am facing issues when I write trigger for edit case, I am not getting how to compare trigger.oldMap and trigger.newMap here to populate the Capacity field only when it is edited to subsequently meet the criteria.
Please suggest solution using Trigger Only (as I want to do this by Trigger and not by Workflow Rule or Process Builder).
Thanks
Requirement is to populate field Capacity as 'Low Capacity' when
Hard disk < 40GB, Processor < 2 GHz and RAM < 1 GB whenever record is Inserted/Edited.
I am facing issues when I write trigger for edit case, I am not getting how to compare trigger.oldMap and trigger.newMap here to populate the Capacity field only when it is edited to subsequently meet the criteria.
Please suggest solution using Trigger Only (as I want to do this by Trigger and not by Workflow Rule or Process Builder).
Thanks
Try Below Trigger Please Mark It As Best Answer If It Helps
Thank You!
Trigger:
============
trigger TriggerLaptop on Laptop__c (before insert,before update) {
if(trigger.isBefore==True && trigger.isInsert==True){
Laptop.PopulateCapacityBefInsert(trigger.new);
}
if(trigger.isBefore==True && trigger.isUpdate==True){
Laptop.PopulateCapacityBefUpdate(trigger.new,trigger.oldMap);
}
}
Trigger Handler/Apex Class:
=======================
public class Laptop {
public static void PopulateCapacityBefInsert(list<Laptop__c> Laplist){
for(Laptop__c lap:Laplist){
if(lap.RAM_in_GB__c<1 && lap.Processor__c<2 && lap.Hard_Disk_in_GB__c<50){
lap.Capacity__c='Low Capacity';
} //closing 2nd if
}//closing 1st If
}//closing for each loop
}//closing method
public static void PopulateCapacityBefUpdate(list<Laptop__c> Laplistnew, Map<Id,Laptop__c> oldMap){
for(Laptop__c lap: Laplistnew){
if(lap.Capacity__c!=oldMap.get(lap.id).Capacity__c){
lap.Capacity__c='Low Capacity';
}//closing if
}//closing for each loop
}//closing BefUpdate method
}//closing class
could you please give me an idea that whats wrong in this code or how should I proceed.
Trigger: LaptopTrigger
Handler: LaptopTriggerHandler
Thanks for ur time and effort but your code is not behaving as required (populate the Capacity field only when it is edited to subsequently meet the criteria.) It is only working fine for insert operation.
Please find attached screenshot :
seems you have just removed update laplist. That I have already tried but after that it is only working fine for insert operation and not for Update.