如何在不使用APEX或工作流的情况下清理Salesforce中的文本字段

时间:2012-05-28 14:28:37

标签: javascript salesforce

Salesforce的专业版许可证不允许使用APEX代码或工作流程,除非单独购买。

我需要清理Salesforce中的文本,该文本是通过我无法控制的Web表单输入的。客户想要修复所有CAPS或缺少句子大写的文本。

我在这个问题Is it possible to add style to a field in Salesforce?上看到了这个答案,它在自定义侧栏组件中使用了javascript。它假定在​​用户将数据键入应用程序时进行转换。我的要求可能允许自定义salesforce按钮调用字段上的操作,因为数据将由自动化过程填充。

假设我要遵循相同的模式,我必须找到一种方法来可靠地检测和修复字符串中的错误格式。

在salesforce中使用javascript有一个很好的方法吗?

1 个答案:

答案 0 :(得分:2)

由于使用了正则表达式和AJAX工具包,我找到了一种接近我需要的方法。

我创建了一个自定义详细信息按钮,可以调用javascript OnClick。

代码获取当前的引导并在描述字段中搜索characther类[A-Z]中任何大写的> 2个字符的字符串。每次匹配一个字符串时,它将用自己的小写版本替换该字符串。

一旦字符串被“清理”,就可以更新潜在客户。

{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")} //adds the proper code for inclusion of AJAX toolkit
var url = parent.location.href; //string for the URL of the current page

var updateRecords = []; //array for holding records that this code will ultimately updated

  var re = new RegExp('[A-Z]{2,}', 'g');
  var inputString = "{!Lead.Description}";
  var matches = inputString.match(re);
  if(matches != null){
   for(var i = 0; i< matches.length;i++){
    inputString = inputString.replace(matches[i], matches[i].toLowerCase());
   }

  var update_Lead = new sforce.SObject("Lead"); //create a new sObject for storing updated record details
  update_Lead.Id ="{!Lead.Id}"; //set the Id of the selected Lead record
  update_Lead.Description = inputString;
  updateRecords.push(update_Lead); //add the updated record to our array

 }
 result = sforce.connection.update(updateRecords); //push the updated records back to Salesforce
 parent.location.href = url; //refresh the page

我将此代码基于我发现的Salesforce: Custom Button to Execute JavaScript

应该可以修改此代码以处理任何Salesforce对象并处理选定的字段。替换的代码可以移动到一个函数中,以使这更容易。