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
Sean NolansSean Nolans 

Creating first Apex class

I have created a simply trigger that adds the values of two currency fields and renders the full amout into a total field.
I need assistance creating a class for this trigger so that I can deploy it

trigger Sum ON order__c(before insert, before update) {

    for (order__c o: Trigger.new) {
        if (Trigger.isUpdate && o.new_total_field__c != Trigger.oldMap.get(o.Id).new_total_field__c) {
            
            continue;       
        }
        o.new_total_field__c = o.hss_qty_price__c + o.sb_qty_price__c ;

    }
}

Any feedback would be greatly appreciated 

Thanks

D
Best Answer chosen by Sean Nolans
jigarshahjigarshah
Derek,

My bad please use the updated code below.
//Trigger Code
trigger OrderTrigger On order__c(before insert, before update) {

	if(Trigger.isBefore){
	
		for (order__c o :Trigger.new) {

			if (Trigger.isUpdate && o.new_total_field__c != Trigger.oldMap.get(o.Id).new_total_field__c) {
				continue;       
			}
			
			o.new_total_field__c = OrderService.calculateTotal(o.hss_qty_price__c, o.sb_qty_price__c);
		}//for
	}
}
 
//Apex Class
public with sharing class OrderService{

	public static Decimal calculateTotal(Decimal pHssQtyPrice, Decimal pSbQtyPrice){
		return pHssQtyPrice + pSbQtyPrice;
	}
}

All Answers

jigarshahjigarshah
Derek,

You can modify your existing code as follows.
//Trigger Code
trigger OrderTrigger On order__c(before insert, before update) {

	if(Trigger.isBefore){
	
		for (order__c o :Trigger.new) {

			if (Trigger.isUpdate && o.new_total_field__c != Trigger.oldMap.get(o.Id).new_total_field__c) {
				continue;       
			}
			
			o.new_total_field__c = OrderService.calculateTotal(hss_qty_price__c, o.sb_qty_price__c);
		}//for
	}
}
 
//Apex Class
public with sharing class OrderService{

	public static void Decimal calculateTotal(Decimal pHssQtyPrice, Decimal pSbQtyPrice){
		return pHssQtyPrice + pSbQtyPrice;
	}
}
Sean NolansSean Nolans
@jigarshah

thanks for this 
it looks great

however I got 

Error: Compile Error: Missing '<EOF>' at 'public' at line 18 column 1

what u think ?

 
jigarshahjigarshah
Derek,

My bad please use the updated code below.
//Trigger Code
trigger OrderTrigger On order__c(before insert, before update) {

	if(Trigger.isBefore){
	
		for (order__c o :Trigger.new) {

			if (Trigger.isUpdate && o.new_total_field__c != Trigger.oldMap.get(o.Id).new_total_field__c) {
				continue;       
			}
			
			o.new_total_field__c = OrderService.calculateTotal(o.hss_qty_price__c, o.sb_qty_price__c);
		}//for
	}
}
 
//Apex Class
public with sharing class OrderService{

	public static Decimal calculateTotal(Decimal pHssQtyPrice, Decimal pSbQtyPrice){
		return pHssQtyPrice + pSbQtyPrice;
	}
}
This was selected as the best answer