如何使用Java将自定义字段添加到Salesforce中的现有对象

小的

我想将一个外部id字段添加到尚不存在的对象。

我需要的:

  • 连接到Salesforce。
  • 获取对象。
  • 获取每个对象的字段。
  • 检查字段之一是否是每个对象的外部ID。

我需要它做的是:

  • 如果外部ID字段不存在:请为此对象创建它。
  • 在我的过程(迁移过程)结束时删除它的另一种方法。

以下代码用于连接到salesforce org:

ConnectorConfig PartnerCfg = new ConnectorConfig();
PartnerCfg.setUsername(USERNAME);
PartnerCfg.setPassword(PASSWORD);
try {
    myConnection = com.sforce.soap.partner.Connector.newConnection(PartnerCfg);
} catch (ConnectionException e) {
    System.out.println("An error occured while connecting to the org.");
}

假定“字段”是每个对象的字段数组,而EXT_ID__C是包含“ Ext_Id__c”字符串的常量。

这是我到目前为止编写的代码:

customFieldExists = false;
for (int j = 0; j < fields.length; j++) {
    Field field = fields[j];
    if ("customField__c".equals(field.getName())) {
        customFieldExists = true;
    }
    // If field is the last of the object
    if (j == fields.length - 1) {
        if (customFieldExists == false) {
            CustomField customField = new CustomField();
            customField.setFullName(EXT_ID__C);
            customField.setLabel("Ext_Id");
            customField.setType(FieldType.Text);
            customField.setExternalId(true);
            customField.setLength(18);
            // Here Should come the code to upload the field and its properties
            // To salesforce org current object.
            System.out.println("Created customField__c field in object " + ObjectName);
        }
    }
}

如何将extId自定义字段推送到我的组织?

小的

经过多次尝试和失败之后,我终于对我的问题有了答案。

在连接try / catch中,更新以下代码:

try {
        myConnection = com.sforce.soap.partner.Connector.newConnection(PartnerCfg);
        metadataCfg.setSessionId(trgtConnection.getSessionHeader().getSessionId());
        metadataConnection = com.sforce.soap.metadata.Connector.newConnection(metadataCfg);
    } catch (ConnectionException e) {
            System.out.println("An error occured while connecting to the org.");
    }

然后,以下代码遍历每个对象,以检查我们要查找的字段是否存在,如果不存在,则创建该字段。假设sObjects变量是sObjects的数组。

DescribeSObjectResult[] objects = myConnection.describeSObjects(sObjects);
for (int i = 0; i < objects.length; i++) {
    // object #i is stored in desObj variable.
    DescribeSObjectResult desObj = objects[i];
    // Get the name of the sObject
    String objectName = desObj.getName();
    boolean customFieldExists = false;
    // Iterate through the fields to get properties of each field
    for (int j = 0; j < fields.length; j++) {
        Field field = fields[j];
        if ("customField__c".equals(field.getName())) {
            extIdExists = true;
        }
        // If field is the last of the object
        if (j == fields.length - 1) {
            // Create a new custom field
            CustomField customField = new CustomField();
            // Add its properties to the custom field
            customField.setFullName(objectName + "." + customField__c);
            customField.setLabel("customField");
            customField.setType(FieldType.Text);
            customField.setExternalId(true);
            customField.setLength(18);
            // Push it to the object
            metadataConnection.create(new Metadata[] {customField});
            System.out.println("Created customField__c field in object " + objectName);
        }
    }
}

这样,我们为组织中的对象创建了一个自定义字段(如果该对象尚不存在)。

可以修改它,并以各种方式使用它来创建多个自定义字段,具体取决于用例。希望能对您有所帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Paypal节点sdk将自定义字段添加到计费协议中

如何将自定义字段添加到Stripe Checkout的弹出窗体中

如何使用fontAwesome将自定义图标添加到Swift 4中的UIbutton

将自定义命令添加到qmake中的现有目标

如何使用openssl将自定义字段添加到证书

如何将自定义AxisItem添加到现有的PlotWidget?

如何以编程方式将自定义NSToolbarItem添加到现有工具栏

将自定义字段添加到子列表

Laravel-将自定义样式添加到现有app.scss

如何使用JSON-LD将自定义属性添加到“产品”类型中?

如何使用现有的线框主题将自己的CSS添加到Sitecore SXA自定义组件中?

如何在Silvershop中修改产品(将自定义字段添加到$ db)

将自定义字段属性添加到CsvHelper

如何使用cloudformation将自定义策略添加到AWS中的服务链接角色?

将自定义字段添加到后端表单

将自定义字段添加到索引结果

将自定义字段添加到Dokan上的现有表单(wordpress / woocoomerce)

将自定义字段添加到Django admin

将自定义样式添加到Mahapps.Metro现有样式

如何在Woocommerce中将自定义字段添加到类别中?

Rails将自定义路由添加到现有资源

如何在GSON解析之前将自定义字段添加到对象

如何在Java中将自定义颜色添加到数组中?

将自定义函数添加到 Python 中的现有类

将自定义字段添加到现有视图的问题

将自定义属性添加到现有视图

如何将自定义字段添加到扩展 AbstractUser 的模型中

如何将自定义映射添加到现有的 mapStruct 映射器?

如何将自定义类添加到 TinyMCE 中的自定义按钮?