2

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.

5
  • AFAIK there is no any feature called convert formula to trigger. You can just apply the same logic to populate a field. Create two new fields and populate them in the trigger with the same logic
    – highfive
    Sep 19 '14 at 4:14
  • Thank @highfive. Please can show me an example from the above code of lines.
    – Ali Abbas
    Sep 19 '14 at 4:17
  • @Vamsi Thanks for reviewing my code lines, but can you suggest me how I may can use APEX trigger rather than formula.
    – Ali Abbas
    Sep 19 '14 at 4:38
  • 1
    @AliAbbas Have you considered an alternate approach of using Workflow to update the field, instead of trigger. There are a number of advantages to this, you can use the same formula and also you will not need to write test for code coverage(which I don't know is good or bad).
    – Learner
    Sep 19 '14 at 4:45
  • Thank you @Learner I did try to use workflow but in this case is not appropriate because I am using picklist which actually populate in formula field. What if is it possible to populate total cost in Payment due option. (Compilation exceeded to 4,535) when I am using formula below. (Package_Subcription_Fee__c - Discouted_Price__c ) + (Subscription_Fee__c + Setup_Charges__c)
    – Ali Abbas
    Sep 19 '14 at 4:53
0

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.

4
  • trigger X on Y. Otherwise, a sound option.
    – sfdcfox
    Sep 19 '14 at 5:10
  • Oops - fixed now Sep 19 '14 at 5:12
  • @sfdcfox I like your custom settings solution as a nice improvement +1 Sep 19 '14 at 5:14
  • Our environment is tightly controlled. We can only deploy once every two months on a cycle with every other team. Custom settings lets us avoid that restriction for emergency tweaks, such as how often our integration calls fire, etc. Plus, it takes over 30 minutes to deploy even a single trigger (run all tests...), making it impractical to have code like this in production.
    – sfdcfox
    Sep 19 '14 at 5:26
3

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.

0
1

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;
            }
        }
    }
}
3
  • 1
    It will compile and run, but a few suggestions: (a) String fields are never equal to '' (blank string). They're always null. But if you really wanted to future-proof yourself, use String.isNotBlank instead. (b) Maps would make this code more maintainable in the long run. (c) You forgot the "default value" from the formula (just add an else). However, it is easy to read and follow.
    – sfdcfox
    Sep 19 '14 at 5:08
  • Nice catches @sfdcfox. Thanks for the suggestions. This is why we are in a community :)
    – highfive
    Sep 19 '14 at 5:11
  • Thank guys trigger is working now. Thanks for your help.
    – Ali Abbas
    Sep 19 '14 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.