How can I convert formula to APEX trigger?

I have already exceeded my 5000 compilation code, now I want to create trigger for following formula from custom object.

Formula 1 (Custom Field = Package Fee)

IF(ISPICKVAL(Package_Option__c,"One Number for Voice & Fax"), 38,
   IF(ISPICKVAL(Package_Option__c,"One Number for Voice & One One Number for Fax"), 61.75,
      IF(ISPICKVAL(Package_Option__c,"One Number for Fax"), 38.00,
         IF(ISPICKVAL(Package_Option__c,"One Number for Voice"), 38.00, 2)
      )
   )
)

Formula 2 (Custom Field = Discount Rate)

IF (ISPICKVAL ( Package_Duration__c , "3 Months"), (0/100),
    IF (ISPICKVAL ( Package_Duration__c , "6 Months"), (3/100),
        IF (ISPICKVAL ( Package_Duration__c , "12 Months"), (5/100),
            IF (ISPICKVAL ( Package_Duration__c , "24 Months"), (12/100), 0)
        )
    )
)

How can I fire trigger in both custom field or can I covert formula field to apex trigger.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You’ll probably want something like this:

trigger YourObjectTrigger on YourObject__c (before insert, before update) {

    for ( YourObject__c obj : Trigger.new ) {
        if (obj.Package_Option__c == "One Number for Voice & Fax"){
            obj.Package_Fee__c =  38;
        }else if (obj.Package_Option__c == "One Number for Voice & One One Number for Fax"){
            obj.Package_Fee__c =  61.75;
        }
        //etc etc

       //package duration is going to be the same:
       if (obj.Package_Duration__c == "3 Months"){
            obj.Discount_Rate__c =  (0/100);
        }
    }
}

Because this is a before trigger (before insert, before update) you don’t need to do anything else – the record saves automatically.

Method 2

Here’s how I would accomplish this using “custom settings”:

trigger X on Y (before insert, before update) {

    for(Y record: Trigger.new) {
        Fees__c fee = Fees__c.getValues(record.Package_Option__c);
        if(fee == null)
            fee = Fees__c.getValues('Default');
        }
        Discounts__c discount = Discounts__c.getValues(record.Package_Duration__c);
        if(discount == null) {
            discount = Discounts__c.getValues('Default');
        }
        if(fee != null) {
            record.Package_Fee__c = fee.Amount__c;
        }
        if(discount != null) {
            record.Discount_Rate__c = discount.Amount__c;
        }
    }
}

Fees__c and Discounts__c should be custom settings (Setup > Develop > Custom Settings), with a single custom field called “Amount__c”, type number, with the appropriate amount of decimal places (2, presumably).

This design lets you adjust values on the fly just by visiting setup, and not having to deploy new code. A value of “Default” should be supplied for each setting to make sure that your default value is applied.

Also, it goes without saying that X and Y need to be the name of the trigger and the name of the object the trigger fires on, respectively.

Method 3

If you are new to writing triggers, it’s always better to go through some examples. Trigger Context Variables are very important when you are writing a trigger.

For your question try below trigger (please note that this is untested and there may mistakes).
First create new two fields for in number type as you are updating them with numbers. Replace the object name and fields with yours’.

trigger UpdateDiscountPackage on MyCustomObject__c(before insert, before update){

    for(MyCustomObject__c o : Trigger.new){
        if(o.Package_Option__c != null || o.Package_Option__c != ''){
            if(o.Package_Option__c == 'One Number for Voice & Fax'){
                o.New_Package_Fee__c = 38;
            }
            else if(o.Package_Option__c == 'One Number for Voice & One One Number for Fax'){
                o.New_Package_Fee__c = 61.75;
            }
            else if(o.Package_Option__c == 'One Number for Fax'){
                o.New_Package_Fee__c = 38.00;
            }
            else if(o.Package_Option__c == 'One Number for Voice'){
                o.New_Package_Fee__c = 38.00;
            }
        }

        if(o.Package_Duration__c != null || o.Package_Duration__c != ''){
            if(o.Package_Duration__c == '3 Months'){
                o.New_Discount_Rate__c = 38;
            }
            else if(o.Package_Duration__c == '6 Months'){
                o.New_Discount_Rate__c = 61.75;
            }
            else if(o.Package_Duration__c == '9 Months'){
                o.New_Discount_Rate__c = 38.00;
            }
            else if(o.Package_Duration__c == '12 Months'){
                o.New_Discount_Rate__c = 38.00;
            }
        }
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x