You need to sign in to do that
Don't have an account?
Aditya 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;
}
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;
}
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
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
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 ..
Hi Adtiya,
why didn't you tried formula field??
Today - CreatedDate;
Thanks,
Gaurav
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