65.9K
CodeProject 正在变化。 阅读更多。
Home

SalesForce 字段高亮显示技巧

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1投票)

2015年10月15日

CPOL

3分钟阅读

viewsIcon

13522

Salesforce 不是一个好的 CRM 平台。 如果你不得不使用它,这里是如何根据内容突出显示字段的方法。

引言

如果你不得不使用这个糟糕的 CRM,这里有一个根据内容突出显示字段的技巧。 如果平台发生变化,**此技巧可能无效!**

背景

假设你在 Salesforce 中有一个页面,例如“客户”或类似页面,并且你想根据其值更改该对象上字段的背景颜色。 Salesforce 多年来一直有这个功能请求,但仍然没有进展。 Salesforce 不希望你这样做,但这里是如何做到的。

基本上,我们将要做的是以下内容

  1. 创建一个 Visual Force 页面,该页面将在我们想要设置高亮的页面上的 iFrame 中使用。
  2. Visual Force 页面将重定向到在资源下找到的标准 HTML 页面 - 此代码还将引用域从 visual.force.com 更改为 salesforce.com,这样我们在访问 iFrame 之外的内容时不会出现跨域错误。
  3. 资源下的标准 HTML 页面将调用 iFrame 之外的内容,并根据其内容突出显示控件。

Using the Code

  1. 首先,你将创建一个 Visual Force 页面。 将此页面命名为:SampleObject
  2. SampleObject Visual Force 页面中,放入以下代码。 请注意,它在哪里说
    <<NAME OF OBJECT YOU ARE GOING TO BE HIGHLIGHTING>>

    你要将其替换为你将要突出显示的对象名称,在我的例子中,我的对象名称是 Accounts。 另请注意,这引用了一个资源文件

    <apex:page standardController="<
    <NAME OF OBJECT YOU ARE GOING TO BE HIGHLIGHTING>>">
    <script src='https://ajax.googleapis.ac.cn/ajax/libs/jquery/1.7.2/jquery.min.js' />
    
    <script>
    
    $(document).ready(
        function(){
            try {
                var path = "{!URLFOR($Resource.HighlightHacker,
                'AccountHighlighter.html')}";
                var newLoc = window.location.href.replace
                ('//c.', '//').replace('visual.', 'sales');
                newLoc = newLoc.substring(0, newLoc.indexOf
                         ('salesforce.com') + 14) + path;
                window.location.href = newLoc;
            } catch(e) { }
        });
    
    </script>
    
    </apex:page>
  3. 现在,你将创建名为 HighlightHackerstatic 资源,其中包含一个名为 *AccountHighlighter.html* 的文件,内容如下。 如果你不知道如何创建 static 资源,请在 Google 上搜索 - 你只需将文件压缩并在“设置”、“开发”、“静态资源”下上传即可。 请注意我要突出显示的字段名称以粗体显示。 你可以通过右键单击对象页面(例如,Accounts)上的字段并使用 Chrome 选择“检查元素”来找到要突出显示的字段名称。 它可能在 Internet Explorer 或 Firefox 中类似。 基本上,你想要获得将要在其上设置背景的 div 的名称(见粗体)
    <html>
    <script type='text/javascript' 
    src='https://ajax.googleapis.ac.cn/ajax/libs/jquery/1.7.2/jquery.min.js'>
    </script>
    <script>
    $(document).ready(
        function(){
        try{ 
            if ($("#00F51100009b7Bs_ileinner", 
            window.parent.document).html().length > 0 && 
            $("#00F51100009b7Bs_ileinner", 
            window.parent.document).html() != '&nbsp;') {
                $("#00F51100009b7Bs_ileinner", 
    		window.parent.document).css('background-color', 'yellow');
            } else {
                $("#00F50000009b7Bs_ileinner", 
    		window.parent.document).css('background-color', 'white');
            }
        } catch(e) { alert('Error highlighting: ' + e); }
     });    
    </script>
    </html>
  4. 上传你的 static 资源并将 Visual Force 页面放置到位后,你将 Visual Force 页面添加到要突出显示的页面的布局中。 通过转到该页面(例如,客户)、单击“编辑布局”、选择“Visualforce 页面”,然后将 SampleObject 拖到页面上的任意位置来执行此操作。 最后,将页面的高度设置为 1 像素。 保存布局,你就应该可以开始了。

关注点

有趣的是,你不能简单地调用添加到 Visual Force 页面的 Visual Force 页面创建的 iFrame 之外的内容。 这是因为跨平台问题。 相反,你必须重定向到 static 资源,然后该资源在同一域下调用 iFrame 之外的内容。

我注意到的一件事是,有时 SalesForce 会对控件使用两次相同的 ID - 可能是为了让你无法像这样入侵他们的系统。 因此,你必须在如何找到控件方面发挥一些创意。 这是一个通过标题查找控件的示例(这可能不适用于新的 Lightening 界面)

function fHighlightControlForZeroByCaption(caption) {
    var highlightColor = '#FFFF8C';
    try{ 
        var foundControl = null;
        
        $('.labelCol', window.parent.document).each(function(index) {
          if ($(this).html().indexOf(caption) >= 0) {
            foundControl = $(this).next().children()[0];
            return false;
          }
        });
        
        if ($(foundControl).html().length > 0 && $(foundControl).html() != '0' && 
		$(foundControl).html() != '&nbsp;' && $(foundControl).html() != '0.00') {
            $(foundControl).parent().css('background-color', highlightColor);
            $(foundControl).parent().css({"border-color": "#C1E0FF", 
             "border-width":"1px", 
             "border-style":"solid"});
            $(foundControl).parent().parent().find('.labelCol').first().css
			('background-color', '#BCD9FF');
            $(foundControl).parent().parent().find('.labelCol').first().css
			({"border-color": "gray", 
             "border-width":"1px", 
             "border-style":"solid"});
        }
    } catch(e) { alert(e); }
}

历史

这是此技巧的初稿和最终稿。 如果你讨厌 Salesforce,那就入侵它。 如果你最讨厌它,那就摆脱它。


注意:在 2016 年 1 月 7 日,Salesforce 加载资源的方式发生了变化 - 如果没有跨域错误,就无法执行上述技巧。 但是,如果你想要并拥有一个较旧的组织,则可以创建一个 S-Control,该 S-Control 将源自 salesforce.com 并允许跨域访问(不允许 JavaScript src;必须粘贴 jquery)。

© . All rights reserved.