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
Adesh TiwariAdesh 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
CharuDuttCharuDutt
Hii Adesh
Try Below Trigger
trigger laptopConfig on Laptop__c  (before update,before update) {
    
	if(Trigger.IsBefore){
			if(Trigger.IsInsert){
				for(Laptop__c l : trigger.new){
				if(l.Hard_disk__c < '40gb' && l.Processor__c < '2 GHz' && l.RAM__c < '1 gb' && 
				l.Hard_disk__c != null && l.Processor__c != null && l.RAM__c != null){
						l.Capacity__c = 'Low';
					}
				}
			}else if(Trigger.IsUpdate){
			for(Laptop__c l : trigger.new){
				if(l.Hard_disk__c < '40gb' && l.Processor__c < '2 GHz' && l.RAM__c < '1 gb' && 
				l.Hard_disk__c != null && l.Processor__c != null && l.RAM__c != null && 
				l.Hard_disk__c != Hard_disk__c && l.Processor__c != Trigger.oldMap.get(l.Id).Processor__c && l.RAM__c != Trigger.oldMap.get(l.Id).RAM__c ){
						l.Capacity__c = 'Low';
					}
				}
		}
	}
}
Please Mark It As Best Answer If It Helps
Thank You!
Adesh TiwariAdesh Tiwari
Thanks for your response. But I do not want to write entire logic inside trigger handler class and not in trigger itself. Please my code :
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. 
Aparna Jaiswar 7Aparna Jaiswar 7
Hi, As you have same logic to be called in case of Insert/Update, It is best to write only one method for it and call it from your Trigger. There are many things you can improve in your code. Below is the Code with Salesforce Best Practices:

Trigger: LaptopTrigger
Handler: LaptopTriggerHandler
/*---------
Trigger:
---------*/
trigger LaptopTrigger on Laptop__c (before insert, before update){

    if(trigger.isBefore){
        if(trigger.isInsert){
            LaptopTriggerHandler.populateCapacity(trigger.new, null);
        }else if(trigger.isUpdate){
            LaptopTriggerHandler.populateCapacity(trigger.new, (Map<Id,Laptop__c>)trigger.oldMap);
        }
    }
}
/*----------------
Trigger Handler:
-----------------*/
public class LaptopTriggerHandler {

    public static void populateCapacity(List<Laptop__c> lstLaptop, Map<Id,Laptop__c> mapOldLaptopById){
        for(Laptop__c objLaptop : lstLaptop){
        
            /*objOldLaptop will be null for before insert and will have old value for before update*/
            Laptop__c objOldLaptop = null;
            if(mapOldLaptopById != null && mapOldLaptopById.containsKey(objLaptop.Id)){
                objOldLaptop = mapOldLaptopById.get(objLaptop.Id);
            }
            
            
            if(objOldLaptop==null //before Insert check
            || (objOldLaptop!=null //before update check
            && ((objLaptop.Hard_Disk__c != objOldLaptop.Hard_Disk__c) //checks if Hard_Disk__c value is changed
            || (objLaptop.Processor__c != objOldLaptop.Processor__c) //checks if Processor__c value is changed
            || (objLaptop.RAM__c != objOldLaptop.RAM__c) )) //checks if RAM__c value is changed
            && (objLaptop.Hard_Disk__c!=null && objLaptop.Hard_Disk__c < 40 
            && objLaptop.Processor__c!=null && objLaptop.Processor__c < 2  
            && objLaptop.RAM__c!=null && objLaptop.RAM__c < 1)){
                objLaptop.Capacity__c = 'Low';
            }
        }
    }
}




 
Adesh TiwariAdesh Tiwari
Hi Charu, thanks for ur effort but there is an error in your code, record is not saving and it is saying You cannot perform DML on trigger.new . Please advise. 
CharuDuttCharuDutt
 please show the error
Adesh TiwariAdesh Tiwari
Hi Aparna

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. 
Adesh TiwariAdesh Tiwari
Hi Charu 

Please find attached screenshot :

User-added image
Adesh TiwariAdesh Tiwari
Hii
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.