gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:11:02 26 4
gpt4 key购买 nike

我想将外部 id 字段添加到尚不具有该字段的对象。

我需要什么:

  • 联系销售人员。
  • 获取可用对象。
  • 获取每个对象的字段。
  • 检查其中一个字段是否是每个对象的外部 ID。

我需要它做什么:

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

以下代码用于连接到 salesforce 组织:

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.");
}

假设“Fields”是每个对象的字段数组,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);
}
}
}

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

可以根据用例以各种方式修改和使用它来创建多个自定义字段。希望对您有所帮助。

关于java - 如何使用 Java 将自定义字段添加到 salesforce 中的现有对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21882296/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com