function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Meena25Meena25 

date format issue YYYY/MM to MM/YYYY

BillAndPayWrapper(BillingHistoryServiceSet__x billPay, String companyCode) {
            this.postDate      = billPay.PostingDate__c;
            this.invoice      = billPay.InvoiceNo__c;
            this.amount      = billPay.TotalDueAmt__c.setScale(2);
            this.billPeriod  = billPay.BillPeriod__c;

billPeriod is string and BillPeriod__c field under external object is data type text . It is external data and i get it as YYYY/MM. But i need to format it to MM/YYYY
Best Answer chosen by Meena25
Anthony McDougaldAnthony McDougald
Good Afternoon Meena25,
Hope that your day is off to an amazing start. Below is a way to format your data. Hope this helps and may God bless you abundantly.
BillAndPayWrapper(BillingHistoryServiceSet__x billPay, String companyCode) {
            this.postDate      = billPay.PostingDate__c;
            this.invoice      = billPay.InvoiceNo__c;
            this.amount      = billPay.TotalDueAmt__c.setScale(2);
            this.billPeriod  = billPay.BillPeriod__c;
//Format the date
this.billPeriod = this.billPeriod.right(2) + '/' + this.billPeriod.left(4);

Best Regards,
Anthony McDougald

All Answers

Anthony McDougaldAnthony McDougald
Good Afternoon Meena25,
Hope that your day is off to an amazing start. Below is a way to format your data. Hope this helps and may God bless you abundantly.
BillAndPayWrapper(BillingHistoryServiceSet__x billPay, String companyCode) {
            this.postDate      = billPay.PostingDate__c;
            this.invoice      = billPay.InvoiceNo__c;
            this.amount      = billPay.TotalDueAmt__c.setScale(2);
            this.billPeriod  = billPay.BillPeriod__c;
//Format the date
this.billPeriod = this.billPeriod.right(2) + '/' + this.billPeriod.left(4);

Best Regards,
Anthony McDougald
This was selected as the best answer
Meena25Meena25
Many Thanks, it worked. Also i have used setScale(2) for amount. however when the amount is 53, it shows me only 53 but i want it as 53.00 and when it is 53.5 i want it as 53.50.
setScale(2) doesn't work for me. Any thoughts?
Anthony McDougaldAnthony McDougald
Good Afternoon Meena,
Hope that your day is off to an amazing start. Is the amount field of type currency or number? Looking forward to helping you and may God bless you abundantly.
Best Regards,
Anthony McDougald
Meena25Meena25
Amount is datatype number
Meena25Meena25
public class BillAndPayWrapper {
        @AuraEnabled public DateTime postDate;
        @AuraEnabled public String   billPeriod;
        @AuraEnabled public Boolean  showSuccess;
        @AuraEnabled public String      invoice;
        @AuraEnabled public Decimal  amount;
        @AuraEnabled public String      amount_formatted;
        @AuraEnabled public String   docType;
        @AuraEnabled public String      pdfDocURL;
       
        BillAndPayWrapper(BillingHistoryServiceSet__x billPay, String companyCode) {
            this.postDate      = billPay.PostingDate__c;
            this.invoice      = billPay.InvoiceNo__c;
            this.amount      = billPay.TotalDueAmt__c.setScale(2);
            this.billPeriod  = billPay.BillPeriod__c;
            this.billPeriod = this.billPeriod.right(2) + '/' + this.billPeriod.left(4);

            if (amount<0) {
                amount=amount*-1;
                this.amount_formatted = '($' + String.valueOf(amount.format()) + ')';
                showSuccess=true;
            }
            else  this.amount_formatted = '$' + String.valueOf(amount.format());

please note: Amount field in external object datatype is number