0

The following code puts a comma in the year. How to get it to stop?

    Integer currentYear = System.Today().year() ;    
    Community_Plan_Goal_Setting__c cpgs = new Community_Plan_Goal_Setting__c(year__c=currentYear.format());

    listGoals.add(cpgs);
1
  • 1
    Is your year__c field a text field?
    – tugce
    Aug 15, 2016 at 6:44

3 Answers 3

2
Community_Plan_Goal_Setting__c cpgs = new Community_Plan_Goal_Setting__c(year__c = String.valueOf(System.Today().year()));

listGoals.add(cpgs);

Above code works just fine if you're looking for a shorter solution.

0

Use String.replace

Integer currentYear = System.Today().year() ; 
String strYear = currentYear.format().replace(',','');   
    Community_Plan_Goal_Setting__c cpgs = new Community_Plan_Goal_Setting__c(year__c=strYear);

listGoals.add(cpgs);
4
  • Would Community_Plan_Goal_Setting__c cpgs = new Community_Plan_Goal_Setting__c(year__c=String.valueOf(System.Today().year())); be a shorter solution to this?
    – tugce
    Aug 15, 2016 at 6:47
  • of course. just added the line for the ease of readability
    – highfive
    Aug 15, 2016 at 6:49
  • This code also only works if the user's locale happens to use commas. For example, "100.000 00" is a valid locale format, but not what you're looking for.
    – sfdcfox
    Aug 15, 2016 at 6:55
  • Thanks for the point @sfdcfox, didn't aware much about locale
    – highfive
    Aug 15, 2016 at 7:00
0

No, you don't need to use currentYear.format().

You can directly assign year__c = String.valueOf(currentYear)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .