I want to add an External id field to an object that does not already have it.
What I need:
- Connect to the salesforce.
- Get objects available.
- Get the fields of each object.
- Check if one of the fields is the external id for each object.
What I need it to do:
- If the external id field does not exist: create it for this object.
- Another method that deletes it at the end of my processes (migration processes).
Following code is used to connect to 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.");
}
Assume that "Fields" is an array of fields for each object and EXT_ID__C is a constant that contains "Ext_Id__c" string.
Here is the code I've made so far:
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);
}
}
}
How do I push the extId custom field to my organization?