+ Start a Discussion
JustinWilliams2381JustinWilliams2381 

render amount with currency format on a apex chatter post

I am creating an apex generated chatter post to put big deals into a big deals group.  I want to put information about the deal including the amount.  I am having issues trying to get the amount to display in its currency format.  I am pushing it to a group so chatter free users can see it too.

 

trigger PostBigDeal on Opportunity (before update) {
    
    //vairables for use later
    List<Opportunity> OppIds = new List<Opportunity>();
    List<FeedItem> feeds = new list<FeedItem>();
            
    for( opportunity opp: Trigger.New){
        	
        if ((opp.Big_Deal_Post__c == true) && (trigger.oldMap.get(opp.id).Big_Deal_Post__c == false )){    
            oppIds.add(opp);
	        }
        }
    
    if (oppIds.size() != 0) {
        
        for( opportunity BigOpp: [SELECT Id, owner.name, Subscription_Type__c, amount FROM opportunity WHERE Id IN :OppIds]){
   
            FeedItem post = new FeedItem();
            post.ParentId = '0F9Q00000004SiM'; //eg. Opportunity id, custom object id..
            post.Body = 'A New ' + BigOpp.Subscription_Type__c + ' has been sold for ' + BigOpp.amount + '.  Congratulation to ' + BigOpp.Owner.Name + ' for their hard work!';
            post.LinkUrl = 'https://cs3.salesforce.com/' + BigOpp.Id;
            feeds.add(post);
        }
        if (feeds.size() > 0) {
        Database.insert(feeds,false); //notice the false value. This will allow some to fail if Chatter isn't available on that object
    }
    }    
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Marko LamotMarko Lamot

Value is stored as decimal and you have to format the value to be presented as currency.

here is one option (you have also other ways; browse this forum):

Decimal rA = BigOpp.amount;

List<String> args = new String[]{'0','number','###,###,##0.00'}; String s = String.format(rA.format(), args);

All Answers

Marko LamotMarko Lamot

Value is stored as decimal and you have to format the value to be presented as currency.

here is one option (you have also other ways; browse this forum):

Decimal rA = BigOpp.amount;

List<String> args = new String[]{'0','number','###,###,##0.00'}; String s = String.format(rA.format(), args);
This was selected as the best answer
JustinWilliams2381JustinWilliams2381

Works perfectly, thank you.  All I had to figure out what to do was get the currency symbol.  It finally hit me just to add it as text in front of the number.