I have a custom checkbox field.

I need to know how to identify the last user that changed the custom checkbox field by using an apex code.

Is it possible? If so, how?


You can also create a new custom field, type = lookup to User, and set value to that lookup field only when somebody will be editing Your custom checkbox. That can be easily done in trigger, let me know if You need support with that.

It is not as straightforward solution like Adrian Larson gave to You, but Yu can save SOQL with that approach. Everyting depends what You need.

  • You can't add custom fields to the CustomField object...
    – Adrian Larson
    Mar 29 '18 at 12:58
  • @AdrianLarson sorry if I was not clear enough, I had on mind creating new custom field next to the custom checkbox mentioned by lemdev, on the same SObject. Not creating a custom field on CustomField object. Eg having fields: Custom_Checkbox__c and User_Who_Last_Time_Edited_Custom_Checkbox__c, and check in trigger if value from Trigger.new != Trigger.old for that field, and then just assign current user Id to User_Who_Last_Time_Edited_Custom_Checkbox__c. Hope I am clear now.
    – patryk
    Mar 30 '18 at 16:49

Just query for it.

List<CustomField> fields = [
    SELECT DeveloperName, LastModifiedBy.Name, LastModifiedDate
    FROM CustomField WHERE ...
  • This code snippet gaves me error: Compile failure on line 1, column 1: Invalid type: CustomField.
    – patryk
    Mar 30 '18 at 17:59

To query the Tooling API via Apex to gather field Last Modified date/user as well as other metadata, see Salesforce Field Auditor for a full solution and the following code snippet for a general method of sending the query, deserializing the JSON response into a map and then using the map to access the individual data points.

        // QUERY 2 - Get the Id, InlineHelpText, CreatedBy.Name, LastModified.Name, CreatedDate, LastModifiedDate on all CUSTOM fields of this object
        req.setEndpoint(instanceName + '/services/data/v48.0/tooling/query/?q=Select+Id,InlineHelpText,CreatedDate,LastModifiedDate,LastModifiedBy.Name,CreatedBy.Name+from+CustomField+Where+TableEnumOrId=\'' + objectId + '\'');
        HttpResponse res2 = h.send(req); 
        responseBody = res2.getBody();
        // Deserialize the response into an object and create a FieldAuditData object for each record in our response
        Map<String, Object> payload2 = (Map<String, Object>)JSON.deserializeUntyped(responseBody);
        for (Object data : (List<Object>)payload2.get('records'))
            Map<String, Object> record = (Map<String, Object>)data;
            Object createdByObject = (Object)record.get('CreatedBy');
            Map<String, Object> createdByMap = (Map<String, Object>)createdByObject;
            String createdName = (String) createdByMap.get('Name');
            Object lastModifiedByObject = (Object)record.get('LastModifiedBy');
            Map<String, Object> LastModifiedByMap = (Map<String, Object>)LastModifiedByObject;
            String lastModifiedName = (String) LastModifiedByMap.get('Name');
            // the ID for Custom field adds 3 characters to the end so they get removed here so that it matches its record from FieldDefinition query
            String fieldId = (String)record.get('Id');
            String formalizedFieldId = objectId + '.' + fieldId.substring(0,fieldId.length() - 3);
                FieldAuditData f = results.get(formalizedFieldId);
                f.helpText = (String)record.get('InlineHelpText');
                f.createdDate = formatDateTime((String)record.get('CreatedDate'));
                f.lastModifiedDate = formatDateTime((String)record.get('LastModifiedDate'));
                f.lastModifiedBy = lastModifiedName;
                f.createdBy = createdName;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.