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
Carly Probasco 14Carly Probasco 14 

Illegal assignment from date to string

I am trying to return either a string or a date field using the following apex, but i'm getting an error 'illegal assignment from date to string'.

public String RatingRelease {get;set;}
public Date Today {get;set;}
Today = System.today();

        //Returns the Rating Release date or the 'rating pending'
        IF (Today < currentQRpCase.Rating_Externally_Visible_Date__c){
            RatingRelease = 'Rating Pending';
        } else IF(Today > currentQRpCase.Rating_Externally_Visible_Date__c){
            RatingRelease = currentQRpCase.Rating_Externally_Visible_Date__c;
        } else {
            RatingRelease = '';
        }


 
Best Answer chosen by Carly Probasco 14
Christan G 4Christan G 4
Hi Carly, I hope you are well. Is RatingRelease field a text field? Assuming yes, please replace the following line of code under the first else if condition with the following:
RatingRelease = currentQRpCase.Rating_Externally_Visible_Date__c.format();
This will return the date field as a string.

If you have any issues, please feel free to reach out to me. I hope it helps!

All Answers

Christan G 4Christan G 4
Hi Carly, I hope you are well. Is RatingRelease field a text field? Assuming yes, please replace the following line of code under the first else if condition with the following:
RatingRelease = currentQRpCase.Rating_Externally_Visible_Date__c.format();
This will return the date field as a string.

If you have any issues, please feel free to reach out to me. I hope it helps!
This was selected as the best answer
Carly Probasco 14Carly Probasco 14
Thank you for your reply! Rating Release will be used as a text output field in my Visualforce page.

I tried to run it again using the recommended update above, and now I'm getting a compile error on the first line that the comparison arguments must be compatible types: date, string, and then an error here 'RatingRelease = currentQRpCase.Rating_Externally_Visible_Date__c;' saying illegal assignment from date to string.

        //Returns the Rating Release date or the 'rating pending'
        IF (Today < currentQRpCase.Rating_Externally_Visible_Date__c.format()){
            RatingRelease = 'Rating Pending';
        } else IF(Today > currentQRpCase.Rating_Externally_Visible_Date__c){
            RatingRelease = currentQRpCase.Rating_Externally_Visible_Date__c;
        } else {
            RatingRelease = '';
        }

I appreciate your help! I"m new :)
Christan G 4Christan G 4
No worries. Oh I think you input the solution in the wrong place. I please view the edited code below:
public String RatingRelease {get;set;}
public Date Today {get;set;}
Today = System.today();

        //Returns the Rating Release date or the 'rating pending'
        IF (Today < currentQRpCase.Rating_Externally_Visible_Date__c){
            RatingRelease = 'Rating Pending';
        } else IF(Today > currentQRpCase.Rating_Externally_Visible_Date__c){
            RatingRelease = currentQRpCase.Rating_Externally_Visible_Date__c.format();
        } else {
            RatingRelease = '';
        }
Carly Probasco 14Carly Probasco 14
I surely did! Thanks again, it works perfectly :)
 
Christan G 4Christan G 4
Anytime! Please choose my response as the best answer so that this can be marked as solved. If you have any other questions, please feel free to reach out! :)