You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于Salesforce Apex日期处理的编译错误求助:变量不存在hello

Apex编译错误排查:Variable does not exist: hello

错误原因

方法getDay声明的返回值类型是Integer,但实际返回的是Date类型变量hello。Apex是强类型语言,要求方法返回值的类型必须与声明的类型严格匹配,这种类型不匹配导致编译器抛出看似变量不存在的错误(本质是类型校验失败引发的编译异常)。

解决方案

根据实际需求选择以下两种修改方式:

方式1:返回日期的天数(Integer类型)

如果需要返回日期对应的天数(比如示例中的11),调用Date类的day()方法获取整数类型的天数,与方法声明的返回类型匹配:

public with sharing class Solution {
  private String dateAsString = '2019-01-01 00:00:00';

  public Integer getDay(Date someDate) {
    Date hello = Date.newInstance(2024, 7, 11);
    return hello.day();
  }
}

方式2:修改方法返回值为Date类型

如果实际需要返回Date对象,直接修改方法的返回值类型为Date即可:

public with sharing class Solution {
  private String dateAsString = '2019-01-01 00:00:00';

  public Date getDay(Date someDate) {
    Date hello = Date.newInstance(2024, 7, 11);
    return hello;
  }
}

内容的提问来源于stack exchange,提问作者user2021924

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 07:52:05