Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date Columns Auto-Inclusion Issue in EloquentSalesForce 2.15 - 'No Such Column 'CreatedDate' on Entity #103

Open
bilelbekkouche opened this issue Nov 10, 2023 · 4 comments

Comments

@bilelbekkouche
Copy link

Description:
I have encountered an issue related to the handling of date columns in the latest version of the EloquentSalesForce package. After updating to version 2.15.3, I noticed that the base Salesforce model (Lester\EloquentSalesForce\Model) automatically includes 'CreatedDate' and 'LastModifiedDate' columns in the $dates property. However, in my Salesforce instance, these columns are not always present in every entity, leading to errors.

Issue:

  • The $dates property in the base Salesforce model includes 'CreatedDate' and 'LastModifiedDate,' causing issues when these columns are not present in Salesforce entities.

Expected Behavior:

  • The ability to customize or exclude specific date columns from the $dates property in my extended models, especially when these columns are not present in Salesforce entities.

Reproduction Steps:

  1. Update to the latest version of the EloquentSalesForce package.
  2. Extend the base Salesforce model in a custom model.
  3. Observe that 'CreatedDate' and 'LastModifiedDate' are automatically included in the $dates property, causing issues when these columns are not present in Salesforce entities.
  4. Laravel Version: 10
  5. EloquentSalesForce Package Version: 2.15.3

Error Message:

{
    "message": "\nContentDocumentId, LinkedEntityId, CreatedDate, LastModifiedDate, IsDeleted\n ^\nERROR at Row:1:Column:47\nNo such column 'CreatedDate' on entity 'ContentDocumentLink'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names."
}
@bilelbekkouche
Copy link
Author

bilelbekkouche commented Nov 11, 2023

Temporary Solution:

I've created a scope, either locally or globally, that excludes the CreatedDate and LastModifiedDate attributes for models that don't have them originally.

Another issue I encountered involves the isDeleted attribute, which is not present in all Salesforce entities. To address this, I simply added an asterisk (*) in the configuration to exclude the soft delete feature from all my models.

Please note that this is a temporary solution and might require a more robust approach in the future. Comments and suggestions are welcome.

Pending a package fix.

/**
 * Exclude specified columns from the query.
 *
 * @param \Illuminate\Database\Eloquent\Builder $query
 * @param array $value
 * @return \Illuminate\Database\Eloquent\Builder
 */
public function scopeExclude($query, array $value): Builder
{
    return $query->select(array_diff($this->columns, $value));
}

@Tamrael
Copy link

Tamrael commented Nov 13, 2023

Same problem here when using relations with eloquent.
This line seems to be the problem

if (!in_array('CreatedDate', $cols)) $cols[] = 'CreatedDate';

@Tamrael
Copy link

Tamrael commented Nov 13, 2023

My temporary fix for this issue is to add ->select(ContentDocumentLink::columns() to the relation which then removes the "*" from the select which results in the columns not being added

@bilel99
Copy link

bilel99 commented Nov 13, 2023

Issue Fix

Problem:

The issue was related to the automatic inclusion of default columns (['Id', 'CreatedDate', 'LastModifiedDate', 'IsDeleted']) for all models, even when they were not explicitly defined.

Fix:

I have addressed this issue locally by modifying the getModels method in the codebase. Now, the method checks if the model has explicitly defined columns, and if the wildcard '*' is requested. If so, it uses the explicitly defined columns from the model. Otherwise, it retrieves columns from the Salesforce service, including the default ones only when appropriate.

/**
 * {@inheritDoc}
 * file: Lester\EloquentSalesForce\Database\SOQLBuilder
 */
public function getModels($columns = ['*'])
{
    // If the model has explicitly defined columns and '*' is requested
    if (count($this->model->columns) && in_array('*', $columns)) {
        // Use the explicitly defined columns from the model
        $cols = $this->model->columns;
    } else {
        /**
         * If there are no explicitly defined columns in the model,
         * retrieve columns from the Salesforce service, including default ones ['Id', 'CreatedDate', 'LastModifiedDate', 'IsDeleted']
         */
        $cols = $this->getSalesForceColumns($columns);
    }

    return parent::getModels($cols);
}

Note:
As I am not a contributor, I am unable to provide a pull request. However, this fix addresses the issue locally, and I hope it helps in resolving the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants