function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Lightning UserLightning User 

Creating object with metadata API

Hi, I'm getting this error (Cannot create child objects through metadata create / update. The CustomField must named 'MyObject__c.MyField1__c' be created separately) when creating an object in salesforce. Anyone have an idea what the problem is?

Code:
<—php
header('P3P: CP=IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT');  
require_once ('soapclient/SforcePartnerClient.php');
require_once ('soapclient/SforceHeaderOptions.php');
require_once ('soapclient/SforceMetadataClient.php'); 
echo "Begin Test Setup\r\n";
try {
  // Create the fixtures.
  
  $mySforceConnection = new SforcePartnerClient();
  $mySoapClient = $mySforceConnection->createConnection('soapclient/partner.wsdl.xml');
  $loginResult = $mySforceConnection->login('username', 'password');
  
  $myMetadataConnection = new SforceMetadataClient('soapclient/metadata.wsdl.xml', $loginResult, $mySforceConnection);
  
  $customObject = new SforceCustomObject();
  $customObject->fullName = 'MyObject__c';
  $customObject->deploymentStatus = DEPLOYMENT_STATUS_DEPLOYED;
  $customObject->setDescription("A description");
  $customObject->setEnableActivities(true);
  $customObject->setEnableDivisions(false);
  $customObject->setEnableHistory(true);
  $customObject->setEnableReports(true);
  $customObject->setHousehold(false);
  $customObject->setLabel("My Custom Obj from PHP");
  
  $customField1 = new SforceCustomField();
  $customField1->setFullName('MyCustomField__c');
  $customField1->setDescription('Description of New Field');
  $customField1->setLabel('My Custom Field Label');
  $customField1->setType('Text');
  
  $customField = new SforceCustomField();
  $customField->setFullName('MyObject__c.MyField1__c');
  $customField->setDescription('Test Picklist');
  $customField->setLabel('Pij');
  $customField->setLength(15);
  $customField->setType('Text');
  $customField2 = new SforceCustomField();
  $customField2->setFullName('MyObject__c.MyField2__c');
  $customField2->setDescription('Test Picklist');
  $customField2->setLength(15);
  $customField2->setLabel('Pif');
  $customField2->setType('Text');
                       
  $customObject->nameField = $customField1;
  
  $customObject->pluralLabel = 'My Custom Objs from PHP';
  $customObject->sharingModel = SHARING_MODEL_READWRITE;
  $array = array ($customField, $customField2);
  
  $customObject->setFields($array);

  print_r($myMetadataConnection->create($customObject));
  
} catch (Exception $e) {
  echo $myMetadataConnection->getLastRequest();
  echo $e->faultstring;
}
–>
SuperfellSuperfell
You shouldn't be setting the name property of the objects name field (e.g. in your case remove the customField1->setFullName line)
Lightning UserLightning User
Hmm, I erased that line. I get the same error. It's funny though, in the sample it sets the fullname, I thought that seemed strange. If it helps, if I don't say $customObject->setFields($array); it'll work. Why is it not liking the way I'm creating custom fields?
SuperfellSuperfell
Oh, i missed that you'd done that. You can't create fields at the same time, you need to create the object, once that's done, you can create the fields.
Lightning UserLightning User
I see...but how would I go about doing that? I don't see an update function in SforceMetadataClient. I tried flipping the order of these statements, it wouldn't update the object though...

print_r($myMetadataConnection->create($customObject));
 
$customObject->setFields($array);

SuperfellSuperfell
just pass the array of customFields to the create method.
Lightning UserLightning User
Well, looking at the create method, it will take in an object, not a field. Here, take a look:

public function create($obj) {
    $encodedObj->metadata = new SoapVar($obj, SOAP_ENC_OBJECT, 'CustomObject', $this->namespace);
    
    return $this->sforce->create($encodedObj);
  }

I try to spoof a field as an object (otherwise it complains some properties aren't set), but the fields won't show up in the table. I will post my updated code:

Code:
<—php
header('P3P: CP=IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT');
require_once ('soapclient/SforcePartnerClient.php');
require_once ('soapclient/SforceHeaderOptions.php');
require_once ('soapclient/SforceMetadataClient.php');
echo "Begin Test Setup\r\n";
try {
// Create the fixtures.

$mySforceConnection = new SforcePartnerClient();
$mySoapClient = $mySforceConnection->createConnection('soapclient/partner.wsdl.xml');
$loginResult = $mySforceConnection->login('particle@twodimensions.com', 'ext187mv');

$myMetadataConnection = new SforceMetadataClient('soapclient/metadata.wsdl.xml', $loginResult, $mySforceConnection);

$customObject = new SforceCustomObject();
$customObject->fullName = 'MyObject__c';
$customObject->deploymentStatus = DEPLOYMENT_STATUS_DEPLOYED;
$customObject->setDescription("A description");
$customObject->setEnableActivities(true);
$customObject->setEnableDivisions(false);
$customObject->setEnableHistory(true);
$customObject->setEnableReports(true);
$customObject->setHousehold(false);
$customObject->setLabel("My Custom Obj from PHP");

$customField1 = new SforceCustomField();
$customField1->setDescription('Description of New Field');
$customField1->setLabel('My Custom Field Label');
$customField1->setType('Text');

$customField = new SforceCustomField();
$customField->setFullName('MyObject__c.MyField1__c');
$customField->setDescription('Test Picklist');
$customField->setLabel('Pij');
$customField->pluralLabel = 'Pijs';//setting another property
$customField->setLength(15);
$customField->setType('Text');
$customField->nameField = $customField1;//setting another property
$customField->deploymentStatus = DEPLOYMENT_STATUS_DEPLOYED;//setting another property
$customField->sharingModel = SHARING_MODEL_READWRITE;//setting another property
$customField2 = new SforceCustomField();
$customField2->setFullName('MyObject__c.MyField2__c');
$customField2->pluralLabel = 'Pifs';
$customField2->setDescription('Test Picklist');
$customField2->setLength(15);
$customField2->setLabel('Pif');
$customField2->nameField = $customField1;
$customField2->deploymentStatus = DEPLOYMENT_STATUS_DEPLOYED;
$customField2->sharingModel = SHARING_MODEL_READWRITE;
$customField2->setType('Text');

$customObject->nameField = $customField1;

$customObject->pluralLabel = 'My Custom Objs from PHP';
$customObject->sharingModel = SHARING_MODEL_READWRITE;
$array = array ($customField, $customField2);

print_r($myMetadataConnection->create($customObject));
print_r($myMetadataConnection->create($customField));
print_r($myMetadataConnection->create($customField2));

} catch (Exception $e) {
echo $myMetadataConnection->getLastRequest();
echo $e->faultstring;
}
–>

 



Message Edited by Gillberg on 05-19-2008 11:19 AM
SuperfellSuperfell
I don't know PHP, so no idea where that create method comes from, but its wrong, go look at the actual metadata API docs, the create call takes an array of metadata objects, which include customObjects and customFields (just like the regular api has a create method that takes an sObject, which can be one of many concrete types like account, contact etc).
Lightning UserLightning User
This function is in the php toolkit in SforceMetadataClient. It's strange that it can't distinguish between a custom object and a custom field.


Message Edited by Gillberg on 05-19-2008 12:15 PM