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 

Need a Code to Create Custom Object via PHP Script

Hey

 

I am using PHP to connect to Salesforce API and connection is established successfully. I have done the Insertion n Query part over Standard and Custom Objects.

 

I need some code to create a Custom Object in Salesforce via PHP Script.

If anyone can help,.....

Best Answer chosen by Admin (Salesforce Developers) 
NishnaNishna

Hi the code below creates a custom object in salesforce.

 

<?php
require_once('soapclient/SforcePartnerClient.php');
require_once('soapclient/SforceMetadataClient.php');

try {
    $mySforceConnection = new SforcePartnerClient();
    $mySoapClient = $mySforceConnection->createConnection('soapclient/Partnetwsdl.xml');
    $loginResult = $mySforceConnection->login('xxxx@xx.com', 'cc20127B8fw0x92YerGiV4zak5ucMc');
    
    $myMetadataConnection = new SforceMetadataClient('soapclient./metadata.xml', $loginResult, $mySforceConnection);

 

//Create object
    $customObject = new SforceCustomObject();
    $customObject->fullName = 'CustomObjFromPHP__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");
    $customField = new SforceCustomField();
    $customField->setFullName('MyCustomFieldb__c');
    $customField->setDescription('Description of New Field');
    $customField->setLabel('My Custom Field Label');
    $customField->setType('Text');
        
    $customObject->nameField = $customField;
    
    $customObject->pluralLabel = 'My Custom Objs from PHP';
    $customObject->sharingModel = SHARING_MODEL_READWRITE;
    print_r($myMetadataConnection->create($customObject));

 

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

?>

 

Regards,

Nishna

All Answers

Pat PattersonPat Patterson

Hi Vishal,

 

For the REST API, Create/Read/Update/Delete are all covered in this article - http://developer.force.com/cookbook/recipe/interact-with-the-forcecom-rest-api-from-php

 

For the SOAP API, there is sample code here - http://wiki.developerforce.com/index.php/PHP_Toolkit_20.0_Samples

 

Cheers,

 

Pat

Vishal Gaddi6Vishal Gaddi6

Hey Pat !!! Thanx  for replying...

 

Can u refer me some code to use Bulk API as my website would be public and there will be so many API Calls to the server.

Vishal Gaddi6Vishal Gaddi6

Hey Pat, I found the following code to create a Custom Object in JAVA.

 

}

private void createCustomObject() {
        CustomObject co = new CustomObject();
        String name = "My Custom Object";
        co.setFullName("MyCustomObject" + "__c");
        co.setDeploymentStatus(DeploymentStatus.Deployed);
        co.setDescription("Created by the Metadata API Sample");
        co.setEnableActivities(true);
        co.setLabel(name);
        co.setPluralLabel(co.getLabel() + "s");
        co.setSharingModel(SharingModel.ReadWrite);
        CustomField nf = new CustomField();
        nf.setType(FieldType.Text);
        nf.setDescription("The custom object identifier on page layouts, " +
                "related lists etc");
        nf.setLabel("My Custom Object");
        nf.setFullName("MyCustomObject" + " __c");
        // The name field appears in page layouts, related lists, and elsewhere. 
    
        co.setNameField(nf);

        try {
            AsyncResult[] ars = metadatabinding.create(new CustomObject[] { co });
            if (ars == null) {
                System.out.println("The object was not created successfully");
                return;
            }
            
            String createdObjectId = ars[0].getId();
            String[] ids = new String[] {createdObjectId};
            boolean done = false;
            long waitTimeMilliSecs = ONE_SECOND;
            AsyncResult[] arsStatus = null;
            
            /** * After the create() call completes, we must poll the results * of the checkStatus() call until it indicates that the create * operation is completed. */ 
    
            while (!done) {
                arsStatus = metadatabinding.checkStatus(ids);
                if (arsStatus == null) {
                    System.out.println("The object status cannot be retrieved");
                    return;
                }
                done = arsStatus[0].isDone();
                if (arsStatus[0].getStatusCode() != null )  {
                    System.out.println("Error status code: " +
                            arsStatus[0].getStatusCode());
                    System.out.println("Error message: " + arsStatus[0].getMessage());
                }
                Thread.sleep(waitTimeMilliSecs);
                // double the wait time for the next iteration 
    
                waitTimeMilliSecs *= 2;
                System.out.println("The object state is " + arsStatus[0].getState());
            }
            
            System.out.println("The ID for the created object is " +
                    arsStatus[0].getId());
        }
        catch (Exception ex) {
            System.out.println("\nFailed to create object, error message was: \n" 
                 + ex.getMessage());
            getUserInput("\nHit return to continue...");
        }
     
But i am unable to find a PHP code for this.... Let me know if you find aomething..
NishnaNishna

Hi the code below creates a custom object in salesforce.

 

<?php
require_once('soapclient/SforcePartnerClient.php');
require_once('soapclient/SforceMetadataClient.php');

try {
    $mySforceConnection = new SforcePartnerClient();
    $mySoapClient = $mySforceConnection->createConnection('soapclient/Partnetwsdl.xml');
    $loginResult = $mySforceConnection->login('xxxx@xx.com', 'cc20127B8fw0x92YerGiV4zak5ucMc');
    
    $myMetadataConnection = new SforceMetadataClient('soapclient./metadata.xml', $loginResult, $mySforceConnection);

 

//Create object
    $customObject = new SforceCustomObject();
    $customObject->fullName = 'CustomObjFromPHP__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");
    $customField = new SforceCustomField();
    $customField->setFullName('MyCustomFieldb__c');
    $customField->setDescription('Description of New Field');
    $customField->setLabel('My Custom Field Label');
    $customField->setType('Text');
        
    $customObject->nameField = $customField;
    
    $customObject->pluralLabel = 'My Custom Objs from PHP';
    $customObject->sharingModel = SHARING_MODEL_READWRITE;
    print_r($myMetadataConnection->create($customObject));

 

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

?>

 

Regards,

Nishna

This was selected as the best answer
ruddraruddra

Hi,

   Its not working for me,if i have to do any changes please let me know.

Pat PattersonPat Patterson

Hi ruddra,

 

What are you trying to do? What code are you using? How is it not working? What error do you see?

 

Cheers,


Pat