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
Vishal Gaddi6Vishal Gaddi6 

PHP Integration

Hey Everyone !!!

 

Can we create Custom Object via PHP Script ??

 

Not Records, I want to create a new Custom Object into my Salesforce Database via PHp Script....  

Best Answer chosen by Admin (Salesforce Developers) 
Pat PattersonPat Patterson

You're right - the docs are a bit sparse on the PHP Metadata Client. Here is some sample code to create a custom object and a custom field on that object. Note that you will need to get the very latest PHP toolkit from https://github.com/developerforce/Force.com-Toolkit-for-PHP since I fixed a bug in creating custom fields just a few minutes ago.

 

<?php
// Change this to point to wherever you've put the soapclient directory
$soapclient = '../Force.com-Toolkit-for-PHP/soapclient';

require_once ($soapclient.'/SforcePartnerClient.php');
require_once ($soapclient.'/SforceMetadataClient.php');

// I use environment variables for credentials - this is good practice
// You can just put your username/password instead if you like
define("SF_SECURITY_TOKEN", getenv("SECURITY_TOKEN"));
define("SF_USERNAME", getenv("USERNAME"));
define("SF_PASSWORD", getenv("PASSWORD"));

define("SF_PARTNER_WSDL", $soapclient.'/partner.wsdl.xml');
define("SF_METADATA_WSDL", $soapclient.'/metadata.wsdl.xml');

// "" or "namespace__"
define("SF_NAMESPACE_PREFIX", getenv("NAMESPACE_PREFIX"));

$mySforceConnection = new SforcePartnerClient();
$mySoapClient = $mySforceConnection->createConnection(SF_PARTNER_WSDL);
$myLoginResult = $mySforceConnection->login(SF_USERNAME, 
  SF_PASSWORD.SF_SECURITY_TOKEN);

$myMetadataConnection = new SforceMetadataClient(SF_METADATA_WSDL, 
  $myLoginResult, $mySforceConnection);

$customObject = new SforceCustomObject();
$customObject->setFullName(SF_NAMESPACE_PREFIX.'ObjFromPHP__c');
$customObject->setDeploymentStatus(DEPLOYMENT_STATUS_DEPLOYED);
$customObject->setDescription("Custom Created Object from the Metadata API");
$customObject->setEnableActivities(true);
$customObject->setEnableDivisions(false);
$customObject->setEnableHistory(true);
$customObject->setEnableReports(true);
$customObject->setHousehold(false);
$customObject->setLabel("Custom Obj from PHP");
$customObject->setPluralLabel('My Custom Objs from PHP');
$customObject->setSharingModel(SHARING_MODEL_READWRITE);

$customField = new SforceCustomField();
$customField->setDescription('Description of name field');
$customField->setLabel('My Name Field Label');
$customField->setType('Text');

$customObject->setNameField($customField);

$customObjectResult = $myMetadataConnection->create($customObject);

echo "Created custom object - result:\n";
print_r($customObjectResult);

$sleep = 1;
while (!$customObjectResult->result->done) {
  sleep($sleep);
  $sleep *= 2;
  
  $customObjectResult = $myMetadataConnection->checkStatus(array($customObjectResult->result->id));  
  echo "Checked status - result:\n";
  print_r($customObjectResult);
}

if ($customObjectResult->result->state == 'Error') {
// You might want to do something other than die() die("Error creating custom object: ".$customObjectResult->result->statusCode. " ".$customObjectResult->result->message."\n"); } echo "Custom object created successfully\n"; $customField1 = new SforceCustomField(); $customField1->setFullName(SF_NAMESPACE_PREFIX.'ObjFromPHP__c.CustomFieldb__c'); $customField1->setDescription('Description of custom field'); $customField1->setLabel('My Custom Field Label'); $customField1->setType('Text'); $customField1->setLength('10'); $customFieldResult = $myMetadataConnection->create($customField1, 'CustomField'); echo "Created custom field - result:\n"; print_r($customFieldResult); $sleep = 1; while (!$customFieldResult->result->done) { sleep($sleep); $sleep *= 2; $customFieldResult = $myMetadataConnection->checkStatus(array($customFieldResult->result->id)); echo "Checked status - result:\n"; print_r($customFieldResult); } if ($customFieldResult->result->state == 'Error') { // You might want to do something other than die()
die("Error creating custom field: ".$customFieldResult->result->statusCode. " ".$customFieldResult->result->message."\n"); } echo "Custom field created successfully\n"; ?>

 

All Answers

PratzPratz

Yes. You cn do that. Check out Metadata API documentation.

Vishal Gaddi6Vishal Gaddi6

But all the sample codes available are in Java. However i need them in PHP only which i could not find anywhere.....

Pat PattersonPat Patterson

You're right - the docs are a bit sparse on the PHP Metadata Client. Here is some sample code to create a custom object and a custom field on that object. Note that you will need to get the very latest PHP toolkit from https://github.com/developerforce/Force.com-Toolkit-for-PHP since I fixed a bug in creating custom fields just a few minutes ago.

 

<?php
// Change this to point to wherever you've put the soapclient directory
$soapclient = '../Force.com-Toolkit-for-PHP/soapclient';

require_once ($soapclient.'/SforcePartnerClient.php');
require_once ($soapclient.'/SforceMetadataClient.php');

// I use environment variables for credentials - this is good practice
// You can just put your username/password instead if you like
define("SF_SECURITY_TOKEN", getenv("SECURITY_TOKEN"));
define("SF_USERNAME", getenv("USERNAME"));
define("SF_PASSWORD", getenv("PASSWORD"));

define("SF_PARTNER_WSDL", $soapclient.'/partner.wsdl.xml');
define("SF_METADATA_WSDL", $soapclient.'/metadata.wsdl.xml');

// "" or "namespace__"
define("SF_NAMESPACE_PREFIX", getenv("NAMESPACE_PREFIX"));

$mySforceConnection = new SforcePartnerClient();
$mySoapClient = $mySforceConnection->createConnection(SF_PARTNER_WSDL);
$myLoginResult = $mySforceConnection->login(SF_USERNAME, 
  SF_PASSWORD.SF_SECURITY_TOKEN);

$myMetadataConnection = new SforceMetadataClient(SF_METADATA_WSDL, 
  $myLoginResult, $mySforceConnection);

$customObject = new SforceCustomObject();
$customObject->setFullName(SF_NAMESPACE_PREFIX.'ObjFromPHP__c');
$customObject->setDeploymentStatus(DEPLOYMENT_STATUS_DEPLOYED);
$customObject->setDescription("Custom Created Object from the Metadata API");
$customObject->setEnableActivities(true);
$customObject->setEnableDivisions(false);
$customObject->setEnableHistory(true);
$customObject->setEnableReports(true);
$customObject->setHousehold(false);
$customObject->setLabel("Custom Obj from PHP");
$customObject->setPluralLabel('My Custom Objs from PHP');
$customObject->setSharingModel(SHARING_MODEL_READWRITE);

$customField = new SforceCustomField();
$customField->setDescription('Description of name field');
$customField->setLabel('My Name Field Label');
$customField->setType('Text');

$customObject->setNameField($customField);

$customObjectResult = $myMetadataConnection->create($customObject);

echo "Created custom object - result:\n";
print_r($customObjectResult);

$sleep = 1;
while (!$customObjectResult->result->done) {
  sleep($sleep);
  $sleep *= 2;
  
  $customObjectResult = $myMetadataConnection->checkStatus(array($customObjectResult->result->id));  
  echo "Checked status - result:\n";
  print_r($customObjectResult);
}

if ($customObjectResult->result->state == 'Error') {
// You might want to do something other than die() die("Error creating custom object: ".$customObjectResult->result->statusCode. " ".$customObjectResult->result->message."\n"); } echo "Custom object created successfully\n"; $customField1 = new SforceCustomField(); $customField1->setFullName(SF_NAMESPACE_PREFIX.'ObjFromPHP__c.CustomFieldb__c'); $customField1->setDescription('Description of custom field'); $customField1->setLabel('My Custom Field Label'); $customField1->setType('Text'); $customField1->setLength('10'); $customFieldResult = $myMetadataConnection->create($customField1, 'CustomField'); echo "Created custom field - result:\n"; print_r($customFieldResult); $sleep = 1; while (!$customFieldResult->result->done) { sleep($sleep); $sleep *= 2; $customFieldResult = $myMetadataConnection->checkStatus(array($customFieldResult->result->id)); echo "Checked status - result:\n"; print_r($customFieldResult); } if ($customFieldResult->result->state == 'Error') { // You might want to do something other than die()
die("Error creating custom field: ".$customFieldResult->result->statusCode. " ".$customFieldResult->result->message."\n"); } echo "Custom field created successfully\n"; ?>

 

This was selected as the best answer
Vishal Gaddi6Vishal Gaddi6

Hey Pat !!

 

Thanx for the code. Its really helpful and working fine.....

 

But i dont know y m getting following Message :

 

Strict Standards: Creating default object from empty value in C:\xampp\htdocs\Connection\soapclient\SforceMetadataClient.php on line 121

Pat PattersonPat Patterson

Just a little tweak required to make that file E_STRICT compliant... Grab it from GitHub again - I just fixed it - should be no warnings now :-)

 

Cheers,


Pat

Vishal Gaddi6Vishal Gaddi6

Hey Pat

 

Thanx again...

 

Can you refer me any artical or documentaion which tells the properties to be set while creating a Custom Field of different types ??

 

Like for Type 'Text', we simple need following properties:

setLenght();

 

i am facing some problem in inserting an Custom Field of type 'Number as i dont know the function of setting 'Decimal Places' becoz its a required field.

 

 

Thanx in advance

 

Pat PattersonPat Patterson

Custom fields are described at http://www.salesforce.com/us/developer/docs/api_meta/Content/customfield.htm - you should be able to figure out the SetXXX() calls - for Number you'll need setPrecision() and setScale() - like this:

 

$customField1 = new SforceCustomField();
$customField1->setFullName(SF_NAMESPACE_PREFIX.'ObjFromPHP__c.CustomFieldb__c');
$customField1->setDescription('Description of custom field');
$customField1->setLabel('My Custom Field Label');
$customField1->setType('Number');
// Precision 5, Scale 2 -> 234.56
$customField1->setPrecision('5');
$customField1->setScale('2');

$customFieldResult = $myMetadataConnection->create($customField1);

 

Cheers,


Pat

Vishal Gaddi6Vishal Gaddi6

Hey Pat !!!

Thts Wonderful.... Thanx a lot...

 

But i think Salesforce should provide a artical with details for each Field Type i.e. which properties are needed to be set with each Type... it would be more helpful... if its already there, then do let me know.....;-)

 

 

 

Regards

Vishal

SAKTHIVEL MSAKTHIVEL M

HI,

You can find the Basic Tutorials about PHP and Salesforce Implementation using Below URL

http://theblogreaders.com/integration-between-salesforce-and-php


TheBlogreaders.com
Blog | Salesforce Certified Administrator | Salesforce Certified Developer