Salesforce 中的未终止字符串文字错误 [英] unterminated string literal error in salesforce

查看:51
本文介绍了Salesforce 中的未终止字符串文字错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从 salesforce 类中获取一个值到一个 javascript 变量中.如果它是单行值,我可以获取该值,但是如果它的多行 textarea 会给出未终止的字符串文字错误

I am trying to get a value from salesforce class into a javascript variable. I am able to get the value if its a single line value, but if its multiline textarea it gives a unterminated string literal error

caseUpdate.Description = "{!ac__c.C_Info__c}";

在谷歌搜索一段时间后,我开始知道我们可以通过具有隐藏字段并使用 DOM 使用 document.getElement.Id 存储它来解决此问题.但是我在单击按钮时调用此代码,因此我将无法创建输入文本或隐藏字段值.任何机构都可以提供一种方法来做到这一点?

After googling for sometime i came to know we can have a workaround for this by having a hidden field and using DOM storing it using the document.getElement.Id. But i am calling this code on a button click so i would not be able to create a input text or hidden field value. Any body who can provide an way to do it?

谢谢普拉迪

推荐答案

是因为换行.合并字段未转义到输出流中,这意味着 CRLF 推入新行并中断 javascript 字符串.使用 div/input 技巧或使用 Apex 将字段中的 \r\n 替换为 <br/> 或最适合目的的任何内容.还要记住, " 也会终止你的 JS 字符串.

It is because of line breaks. merge fields are rendered unescaped into the output stream meaning that CRLFs push into a new line and break javascript strings. Either use the div/input trick or use Apex to replace \r\n's in the field with <br/> or whatever best suits the purpose. Also keep in mind that " will also terminate your JS string.

最简单的方法是在你的扩展中包含一个函数,然后你就可以全面使用它

The easiest way is to just include a function in your extension and then you can use it across board

public String FixNewLine(String s) {
    if (s != null) return s.replaceAll('\r\n', '<br/>').replaceAll('"', '\\"');
    return null;
} 

这篇关于Salesforce 中的未终止字符串文字错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆