Posts

Showing posts with the label SObject

Obtain the name of the SObject from a given record ID

In Apex, you can obtain the name of the SObject from a given record ID using the getSObjectType () method provided by the Id class. Example Id recordId = '001XXXXXXXXXXXX'; // Replace this with the actual record ID // Get the SObject type from the record ID Schema.SObjectType sObjectType = recordId.getSObjectType(); // Get the name of the SObject String sObjectName = sObjectType.getDescribe().getName(); System.debug('SObject Name: ' + sObjectName); In this example: recordId.getSObjectType() returns the Schema.SObjectType of the object associated with the record ID. sObjectType.getDescribe().getName() retrieves the name of the SObject. This approach allows you to dynamically determine the SObject name from a record ID in your Apex code.

Lock Records Using Apex in Salesforce

Locking Statements In Apex, you can use FOR UPDATE to lock sObject records while they’re being updated to prevent race conditions and other thread safety problems. Account [] accts = [SELECT Id FROM Account LIMIT 2 FOR UPDATE]; Locking Consideration When records are locked by a client, that client can modify the record's field values in the database within the same transaction. Other clients must wait until the transaction is completed and the records are no longer locked before they can update the same records. However, other clients can still query the locked records. If a record is already locked by another client, a process will wait for a maximum of 10 seconds for the lock to be released before acquiring a new lock. If the wait exceeds 10 seconds, a QueryException is thrown. Similarly, if a record is locked by another client and the lock is not released within 10 seconds, a DmlException is thrown. If a client tries to modify a locked record, the update can succeed if the lock ...