SFDC CustomLabels vs. CustomMetadata

SFDC Custom Labels vs. Custom Metadata


Resource

Custom Metadata Allocations and Usage Calculations
Custom Labels
sObject Types

前言

最近项目在对一些共通Apex用到的常量在讨论有什么好的解决方案。
目前有几种方式:

常量类

建立一个常量类,所有的常量都定义为public static final。使用方法:常量类名.变量名

/*
 * 定数共通処理
 */
public with sharing class EX_CommonConstants {
    /** 会社コード */
    public static final String COMPANY_CODE = 'WZJ';
}

// 使用方法
system.debug(EX_CommonConstants.COMPANY_CODE);// output:WZJ

Custom Labels

Custom labels enable developers to create multilingual applications by automatically presenting information (for example, help text or error messages) in a user’s native language. Custom labels are custom text values that can be accessed from Apex classes, Visualforce pages, Lightning pages, or Lightning components. The values can be translated into any language Salesforce supports.
自定义标签使开发人员能够通过以用户的本机语言自动显示信息(例如,帮助文本或错误消息)来创建多语言应用程序。自定义标签是可以从 Apex 类、 Visualforce 页面、 Lightning 页面或 Lightning 组件访问的自定义文本值。这些值可以翻译成 Salesforce 支持的任何语言。
You can create up to 5,000 custom labels for your organization, and they can be up to 1,000 characters in length. Custom labels from managed packages don’t count toward this limit.
您可以为组织创建多达5,000个自定义标签,它们的长度可以达到1,000个字符。来自managed packages的自定义标签不计入此限制。

如何向应用程序添加自定义标签取决于用户界面。有关以下语法的详细信息,请参阅相应的开发人员指南。

  • In Apex use the System.Label.Label_name syntax.
  • In Visualforce, use the $Label global variable.
  • In Aura components, use the $Label.c.labelName syntax for the default namespace or $Label.namespace.labelName if your org has a namespace or to access a label in a managed package.
  • In Lightning web components, import the label using the @salesforce/label/namespace.Label_name syntax.
  • In Lightning App Builder component labels and attributes, use the {!$Label.customLabelName} expression.

添加Custom Labels

  • 簡単な説明:Short Description,给开发者看的,说明这个label的用途
  • 名前:Name 字段中,输入标签使用的名称。此值用于 Apex 和 Visualforce 页面,以引用自定义标签。名称必须只包含字母数字字符,以字母开头,不包含空格或双下划线,并且与 org 中的所有其他标签都是唯一的。
  • カテゴリ:Categories 类别 文本框中允许的字符总数为255个。
  • 値:Value 要显示的值,输入最多1000个字符的文本。这个值可以翻译成 Salesforce 支持的任何语言。
  • 言語:Language 语言不可自定义,与添加者当前设置的language有关,并且一旦添加后你也不能更改这个label的语言。
    特别说明:言語项,说明Customlabel可以用来做国际化。


使用方法

// 由常量类变成了 System.Label
system.debug(System.Label.COMPANY_CODE);// output:WZJ

在Apex里更新/创建 CustomLabels是否有可能?

不可能。
Custom labels are not standard sObjects. You cannot create a new instance of a custom label. You can only access the value of a custom label using system.label.label_name. For example:

String errorMsg = System.Label.generic_error;

Custom Metadata

Custom metadata is customizable, deployable, packageable, and upgradeable application metadata. First, you create a custom metadata type, which defines the form of the application metadata. Then you build reusable functionality that determines the behavior based on metadata of that type.
定制元数据是可定制的、可部署的、可打包的和可升级的应用程序元数据。首先,创建自定义元数据类型,该类型定义应用程序元数据的形式。然后构建基于该类型的元数据确定行为的可重用功能。

After you create a public custom metadata type, you or others can declaratively create custom metadata records that are defined by that type. When you package a public custom metadata type, customers who install the package can add their own records to the metadata type. Your reusable functionality reads your custom metadata and uses it to produce customized application behavior. For example, you can use custom metadata types for the following.
创建公共自定义元数据类型后,您或其他人可以以声明方式创建由该类型定义的自定义元数据记录。在打包公共自定义元数据类型时,安装该包的客户可以将自己的记录添加到元数据类型中。您的可重用功能读取您的自定义元数据并使用它来生成自定义应用程序行为。例如,可以为以下内容使用自定义元数据类型。

  • Mappings—Create associations between different objects, such as a custom metadata type that assigns cities, states, or provinces to particular regions in a country.
  • Business rules—Combine configuration records with custom functionality. Use custom metadata types along with some Apex code to route payments to the correct endpoint.
  • Master data—Let’s say that your org uses a standard accounting app. Create a custom metadata type that defines custom charges, like duties and VAT rates. If you include this type as part of an extension package, subscriber orgs can reference the master data.
  • Whitelists—Manage lists, such as approved donors and pre-approved vendors.
  • Secrets—Store information, like API keys, in your protected custom metadata types within a package.

Custom metadata rows resemble custom object rows in structure. You create, edit, and delete custom metadata rows in Metadata API or in Setup. Because the records are metadata, you can migrate them using packages or Metadata API tools.
自定义元数据行在结构上类似于自定义对象行。可以在 Metadata API 或安装程序中创建、编辑和删除自定义元数据行。因为记录是元数据,所以可以使用包或 Metadata API 工具迁移它们。

Usage Calculation
不限制个数,限制总长度为1000W个字符。

  • Usage is calculated in characters. You can store up to 10 million characters.
  • Standard fields like Label, Name, and Namespace, are included in your usage calculation but Description and Qualified API Name are NOT.
  • Long text area fields (up to 255 characters per long text area field for a given type) are included in the usage calculation.
  • Metadata relationship fields count as 15 characters in the usage calculation if their target is another custom metadata type, or 10 characters if the target is Entity Definition or Field Definition.
  • Picklists and Checkboxes both count as 10 characters.

Create Custom Metadata Type
创建Custom Metadata Type
image
创建完成
API参照名为:Account_Setting__mdt,__mdt,自定义Object为__c,Big Object为__b,Externl Object为__x.
image
image
image
image
使用方法:

// 就像普通的自定义Object一样,SOQL查询然后遍历取自己需要的值
List<Account_Setting__mdt> asm = [SELECT 
                                  Id, DeveloperName, MasterLabel, Language, NamespacePrefix, Label, QualifiedApiName 
                                  FROM Account_Setting__mdt];
for(Account_Setting__mdt tmp : asm){
    if('COMPANY_CODE'.equals(tmp.DeveloperName)){
        System.debug('COMPANY_CODE:'+tmp.MasterLabel); // WZJ
    }
}
原文地址:https://www.cnblogs.com/paynev/p/14757276.html