salesforce - 文本区域上的换行符

标签 salesforce apex-code visualforce

我有一个名为 Current_Address__c 的自定义字段,它的数据类型为 textarea。

我需要以下面的格式填充此字段。即街道后的换行符和 zip 后的另一个换行符。

街道
邮政编码
国家

城市国家邮政编码国家等的值取自联系对象。我不想将其用作公式字段。所以我需要在我的 Controller 中填充它并在我的 VF 页面上显示它。

我正在尝试使用下面的代码添加换行符

this.customobj.Current_Address__c = currentStreet + '\\n ' + currentCity + ' ' + currentState  + ' ' + currentZIP  + '\\n ' + currentCountry ;

我也使用了\n 而不是\n。

它仍然在一行而不是 3 行中显示该字段

编辑

我使用以下代码完成了这项工作。我会接受 mathews 的答案,因为它可以与输出字段一起使用。
                currentAddress = currentStreet;
            currentAddress += '\r\n';
            currentAddress += currentCity + + ' ' + currentState  + ' ' + currentZIP ;
            currentAddress += '\r\n';
            currentAddress += currentCountry;

这仅在您使用 += 时有效。
不知道为什么会这样

最佳答案

我想我发现了问题,您有两个转义字符斜杠( \\n ),但只需要一个,因为 \n 中的斜杠在这种情况下不需要转义。

此外,Salesforce 将新行另存为 \r\n .尝试这个:

this.customobj.Current_Address__c 
    = currentStreet + ' \r\n' 
    + currentCity + ' ' + currentState  + ' ' + currentZIP  + ' \r\n' 
    + currentCountry;

此方法在使用 时有效<apex:outputfield> 带有 sObject 字段。
<apex:outputtext value="{!myCustomSObject__c.Address__c}"/>

如果您使用不同的 Visualforce 组件,它将不起作用。使用 <apex:outputtext> 时,Visualforce 在 HTML 中呈现新行组件,但 HTML 忽略新行。如果您使用 <br/>标记,Visualforce 将其呈现为 &lt;br/&gt; .

我可以想出的最佳解决方案是使用禁用的 <apex:inputtextarea> 来呈现其中包含新行的变量(而不是 sObject 字段) .
<apex:inputtextarea value="{!myAddress}" disabled="true" readonly="true">
</apex:inputtextarea>

关于salesforce - 文本区域上的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10083472/

相关文章:

python - 使用 python 访问 Salesforce 数据

salesforce - Force.com 工作流程 - 它们与触发器有何不同?

javascript - 谷歌云端硬盘 HTTP 403 “Access Not Configured” 错误

salesforce - 检查是否存在或捕获异常?

html - Salesforce.com - Visualforce 和 ActiveX

salesforce - 检查某个扩展是否安装在 Salesforce 中

forms - 多个 Salesforce 在线潜在客户表单

php - 从 Web 服务制作 APEX 触发器 - 这可能吗?

javascript - 从 apex Controller 类返回数组并在 Salesforce 中的 javascript 中使用

salesforce - 视觉力(APEX) : Update record with known ID