Tuesday, 25 March 2014

Find and replace text - apex trigger or button

Question


I have an issue where by I need a temporary fix, I am trying to create a method of looking at a string of text on the opportunity and replacing all the $ with £ (custom text field)

A trigger example I found is

trigger YourTrigger on Solution (before update) { 
  for(Solution s : trigger.new) {
    s.replaceAll('Hello World','Goodbye World');
  }
}

but if possible I would rather have a button on the opportunity that could provide this functionality. I also need to limit this trigger to fire only on opptys that match a pre specified field criteria eg checkbox__c = true



Best Answer


Why would you need to this with a Trigger? You could do this with a Workflow Rule+ Field Update. Something like this:
  1. Setup | Create | Workflows & Approvals | Workflow Rules
  2. New Rule
  3. Select the Object: Opportunity
  4. Evaluation Criteria: created and every time it’s edited [2nd Option]
  5. Rule Criteria: formula evluates to true
  6. Formula
    AND(
      Checkbox_Field__c = TRUE,
      NOT(ISBLANK(Text_Field__c)),
      FIND('$', Text_Field__c) >= 1, 
      OR(
        ISNEW(), 
        ISCHANGED(Text_Field__c),
        ISCHANGED(Checkbox_Field__c)
      )
    )
  7. Click Save & Next
  8. From under Immediate Workflow Actions, click Add Workflow Action to select Field Update
  9. Select the Field to Update: Opportunity: Text Field
  10. Select Use a Formula to Set the New Value
  11. Formula
    SUBSTITUTE(Text_Field__c, '$', '£')
  12. Click Save
  13. Click Done
  14. Click Activate
So this one runs whenever some changes the value on the Text Field or the Checkbox/when a new record is created but provided that it has some value and not is EMPTY, the Checkbox is checked(aka TRUE), the Text Field has at least one Dollar sign($).

No comments:

Post a Comment