在Salesforce中,对象的LastModifiedDate字段是不能手动更改的。这个字段是由Salesforce自动维护的,它记录了对象最后一次被修改的时间。
Salesforce不允许这样做,因为这是为了确保数据的完整性。如果允许手动更改LastModifiedDate,那么可能会导致数据的不一致和不可靠性。
比如你在Apex中,手动给LastModifiedDate赋值的话,肯定会报下面的错误:
但有些时候,当LastModifiedDate作为条件写入SOQL中,比如LastModifiedDate<Today-30,测试的时候,就测不到这种情况,很是烦人!~
所以,福音来了:
从2022年10月开始,Salesforce在设定中增加了一个选项,可以控制这些所谓的Audit Fields。
上面的选项如果check上,可以通过如下方式,指定你想要的LastModifiedDate或者CreatedBy。
- Set Audit Fields upon Record Creation - Allow the User to set audit fields (like 'Created By' or 'Last Modified By') when you create a record via API importing tools like Data Loader.
- Update Records with Inactive Owners - Allow the User to update record owner and sharing-based records with inactive owners.
原文Link:Enable the 'Create Audit Fields' permission (salesforce.com)