PHP code example of cnsdose / salesforce-php

1. Go to this page and download the library: Download cnsdose/salesforce-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cnsdose / salesforce-php example snippets


        // config/app.php
        'providers' => [
            // ...
            \CNSDose\Salesforce\Providers\SalesforceProvider::class
            // ...
        ]
        

        // bootstrap/app.php
        $app->register(\CNSDose\Salesforce\Providers\SalesforceProvider::class);
        

        function config(string $key, $default = null)
        {
            $configs = [
                'salesforce' => 

$result = Account::build()
    ->where('Type', 'LIKE', "'Customer%'")
    ->where('CurrencyIsoCode', "'USD'")
    ->where('CreatedDate', '>=', '2019-01-23T01:01:01.000Z')
    ->where('Site', '=', null)
    ->query();

$result = Account::build()
    ->select('*')
    ->resolve(Contact::class, 'Contacts')
    ->query();

$contact = Contact::build()
    ->where(
        'AccountId',
        'IN',
        Account::build()
            ->select('Id')
            ->where('Name', "'AccountExample1'")
            ->orWhere('Name', "'AccountExample2'")
    )
    ->query();
$result = BaseRecordModel::build()
    ->select('Id')
    ->select(
        BaseRecordModel::build()
            ->select('Id')
            ->from('Account.Contacts')
    )
    ->from('Account')
    ->query();

// $result: original JSON
$result = BaseRecordModel::build()
    ->queryRaw('SELECT Id FROM Account');

// $result: Account[]
$result = Account::build()
    ->queryRaw('SELECT Id FROM Account');

$result = Account::queryById('0010o00002Cn41XXXX');

$contact = new Contact();
$contact->FirstName = 'Sheep';
$contact->LastName = 'Shaun';
$contact->Email = '[email protected]';
$contact->create();

/**
 * @var Contact $contact
 */
$contact = Contact::build()
    ->where('LastName', "'Shaun'")
    ->query()[0];
$contact->FirstName = 'Lamb';       // changes after object instantiation are recorded
$contact->upsert();

$contact = new Contact([            // we do not need to query first
    'Id' => $contact->Id,
]);
$contact->FirstName = 'Mince';
$contact->upsert();

$contact->delete();                 // Id is 

[
    'custom_fields' => [
        \CNSDose\Salesforce\Models\Sales\Custom::class => [
            'Float__c' => 'number:16,2',
            'Time__c' => 'time',
            'SystemModstamp' => null,
        ],
    ],
]

GenerateRecordModel::addTypeRule('type1', 'string', null);
GenerateRecordModel::addTypeRule('type2', '\\Carbon\\Carbon', 'date');
GenerateRecordModel::addTypeRule('type3', 'float', function ($field) {
    return sprintf('number:%s,%s', $field['precision'] - $field['scale'], $field['scale']);
});

// Create
// Same as Java example in https://developer.salesforce.com/docs/atlas.en-us.216.0.api_meta.meta/api_meta/meta_createMetadata.htm
$customObject = new CustomObject();
$customObject->setFullName('MyCustomObject1__c');
$customObject->setDeploymentStatus(DeploymentStatus::DEPLOYED());
$customObject->setDescription('Created by the Metadata API');
$customObject->setEnableActivities(true);
$customObject->setLabel('MyCustomObject1 Object');
$customObject->setPluralLabel('MyCustomObject1 Objects');
$customObject->setSharingModel(SharingModel::READ_WRITE());

$nameField = new CustomField();
$nameField->setType(FieldType::TEXT());
$nameField->setLabel('MyCustomObject1__c Name');
$nameField->setIsNameField(true);

$customObject->setNameField($nameField);

$customObject->create();

// Read & Update
$customObject = CustomObject::read('MyCustomObject1__c');
$customObject->setDescription('Updated description!');
$customObject->update();

// Rename
$customObject->rename('MyCustomObject2__c');

// Upsert
$customObject->setLabel('MyCustomObject1 Object');
$customObject->setPluralLabel('MyCustomObject1 Objects');
$customObject->upsert();

// Delete
$customObject->delete();