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
Aditya Pavan lakshmiAditya Pavan lakshmi 

Return type must be of Datetime

I am getting the error of return type must be of datetime.

public Datetime getcreatedDate( Datetime createdDate ) {
        if( createdDate != null ) {
            String timeVal = ' ';
            Decimal dateVal;
            dateVal=Math.Floor(Decimal.valueOf((Datetime.now().getTime()-createdDate.getTime() ))/(1000));//In Second
            if( dateVal >= 60 ) {
                dateVal=Math.Floor(dateVal/60); //In Minutes
                timeVal = timeValue(dateVal,' minutes ',' minute ');
                if(dateVal>=60 ){
                    dateVal=Math.Floor(dateVal/60); //In Hours
                    timeVal = timeValue(dateVal,' hours ',' hour ');
                    if(dateVal>=24){
                        dateVal= Math.Floor(dateVal/24); //In Days
                        timeVal = timeValue(dateVal,' days ',' day ');
                        system.debug( timeVal );
                        system.debug( dateVal );
                        if(dateVal >= 30){
                            dateVal = Math.Floor(dateVal/30);
                            timeVal = timeValue(dateVal,' months ',' month ');
                            system.debug( timeVal );
                            system.debug( dateVal );
                            if(dateVal >= 365){
                                dateVal = Math.Floor(dateVal/365);
                                timeVal = timeValue(dateVal,' years ',' year ');
                                system.debug( timeVal );
                                system.debug( dateVal );
                            }
                        }
                    }
                }
            }
            else {
                if(dateVal>1) {
                    timeVal = dateVal +' Seconds ago ';
                }
                else {
                   timeVal = dateVal +' Second ago ';
                }
            }
            return timeVal;
        }
        return null;
    }
 
 public String timeValue( Decimal dtValue,String plural,String singular ) {
    String timeStr = '';
    timeStr = String.valueOf(dtValue)+ (dtValue>1 ? plural : singular )+'ago';
    return timeStr;
 }
sharathchandra thukkanisharathchandra thukkani
you are returning string( timeVal = dateVal +' Seconds ago ';) and the method signature for getcreatedDate() is DateTime so the error
Aditya Pavan lakshmiAditya Pavan lakshmi
Thaks @sharathcahndra.I am changing the method signature but still facing some issues dateVal=Math.Floor(Decimal.valueOf((Datetime.now().getTime()-createdDate.getTime() ))/(1000)),If I change the method signature as a string then this line is getting error as [String].getTime()
SandhyaSandhya (Salesforce Developers) 
Hi Aditya,
 Your method signature and your return type must be same.
Change your method signature to string if you want to return the value as string or else if you want to return DateTime value then you need to change
timeVal variable to DateTime not string.

public String getcreatedDate( Datetime createdDate ) {
 String timeVal = ' ';
return timeVal;
or
public DateTime getcreatedDate( Datetime createdDate ) {
 Datetime timeVal;
return timeVal;


Please accept my solution as BestAnswer if my answer was helpful.It will make it available for other as the proper asolutionIf you felt I went above and beyond u can give me kudos using a star.

Thanks and Regards
sandhya


 
GauravGargGauravGarg

Hi Aditya, 

Please convert String into DateTime as mentioned in the example below:
DateTime yourDateVariable = DateTime.newInstanceGmt(Integer.valueOf(stringParts[5]), monthNames.get(stringParts[1]), Integer.valueOf(stringParts[2]), Integer.valueOf(timeParts[0]), Integer.valueOf(timeParts[1]), Integer.valueOf(timeParts[2]));

Let me know if you still face issue, hope this will help you. 

Thanks,

Gaurav
Email: gauravgarg.nmims@gmail.com
Skype: gaurav62990

sharathchandra thukkanisharathchandra thukkani
you need to change the signature of the method to: public String getcreatedDate( Datetime createdDate ) {
Aditya Pavan lakshmiAditya Pavan lakshmi
THanks @all for your replies

But Actually I need to show the difference of the days based on today and the creation of the record(i:e CreatedDate) field in salesforce .I tried in this way 
    public String dynamicDate( String createdDateOne ) {
        createdDateOne = String.valueOf(Today() (Today.daysBetween(createdDate))) + 'days ago';
        return createdDateOne;
    }
But Still I am unable to do as it is throwing error.as I need to show 7 days ago in this way ..
 
GauravGargGauravGarg

Hi Adtiya,

why didn't you tried formula field??

Today - CreatedDate;

Thanks,

Gaurav

Aditya Pavan lakshmiAditya Pavan lakshmi
No GauravGarg I need to do through Apex only 
GauravGargGauravGarg
Aditya,

Below is the formula to calculate Difference between two dates:
 
Total number of minutes = (LastModifiedDate-CreatedDate);

Thanks,
Gaurav
Email: gauravgarg.nmims@gmail.com
Skype: gaurav62990