1

I have a workflow, and I need to convert this into Apex code, could you help me up please?

This is the code i have for now, but i need to contemplate the fact the user is entering the "Intro", and convert it into an space " "

map<id,string> mapOld = new map<id,string>();
map<id,string> mapNew = new map<id,string>();
list<Id> changedList= new list<Id>;

for (Opportunity opp: trigger.old) { 
    mapOld.put(opp.Id, opp.description);
}

for (Opportunity opp: trigger.new) {              
    mapNew.put(opp.Id, opp.description);
    if (mapOld.get(opp.Id) =! opp.Description)
        changedList.add(opp.Id);
}

for (Opportunity opp : changedList){
    if (opp.description.lenght() <= 655) {                        
        opp.Description_Test__c = opp.description
    }
    else {
        opp.Description_Test__c = opp.description.subString(0,655);
        opp.Description_Test__c = opp.Description_Test__c.subString(0, opp.Description_Test__c.lastIndexOf(' ')) + '...';
    }
}
update changedList;

And this is the formula i have in the workflow.

IF( 
    LEN(desc__c) > 644, 
    LEFT(SUBSTITUTE(desc__c, SUBSTITUTE($Label.LineBreak, "-", "")," "), 644) & ' (...)', 
    SUBSTITUTE(desc__c, SUBSTITUTE($Label.LineBreak, "-", "")," ") 
)
1

I think it's much easier with String.Abbreviate() (check https://resources.docs.salesforce.com/sfdc/pdf/salesforce_apex_language_reference.pdf)

Focusing on the last loop of your method:

for (Opportunity opp : changedList) {
    String description = opp.description;

    if (description.length () > 644)
    {
        description = description.replace ('\r', ' ');
        description = description.abbreviate (644);
    }
    else
    {
        description = description.replace ('\r', ' ');
    }
    opp.Description_Test__c = description;
}

I think that the logic above copies your workflow formula. I left it like that for clarity. In real production code you would use a constant for 644 (no magic numbers!) and you could move the description.replace ('\r', ' ') out of the if, as it's present in both cases. That makes the code so simple that you could even remove the if altogether:

for (Opportunity opp : changedList) {
    String description = opp.description;
    opp.Description_Test__c = description.replace ('\r', ' ').abbreviate(644);
}

You should add a few unit tests to verify that the behaviour is exactly the one you want.

  • Thanks for your answer, and what if I need to use the $Label.LineBreak ? I'f a make a test in anounymous, i get error using " $Label.LineBreak" – CrispuSF Jan 5 '16 at 14:49
  • What do you need $Label.LineBreak for? What is it? Is it a custom label? Assuming it's a custom label named 'Label', you can access it like this in Apex: Label.LineBreak. However, I don't see the point of storing the line break in a label. The purpose of labels is to create translatable resource strings, whereas the line break is a constant (which can be referenced as the '\r' string literal). It's not like you have a platform-dependent character, given that Salesfore is effectively your platform. – mkorman Jan 5 '16 at 15:03

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.