1

I am creating a custom DML Log in our Salesforce sandbox, and I created a Class & method that fires based on an Apex Trigger Here's the class:

public class Util {

public static void logDmlDeleted(Sobject[] sObjList) {
    DML_Log__c[] logInsList = new List<DML_Log__c>();
    for (Sobject sobj : sobjList)
       logInsList.add(new DML_Log__c(SObject_Type__c = sobj.getSobjectType().getDescribe().getName(),               
                                 Name__c = (String) sobj.get('name'),
                                 SObject_Id__c = (String) sobj.get('Id')
                                 ));

    insert logInsList; 
    }
}

The SObject_Id__c was an addition I added to record the Id of the sObject. It looks like this is working, except for the fact that there is an additional string of characters randomly appended: 'MAU'.

Here's a standalone sObject String Id:

a0HJ0000007yBdQ

And here is how that Id is written to the DML Log field:

a0HJ0000007yBdQMAU

How can I adjust the statement in the method so only the string's virgin Id is passed into the field?

1
  • 1
    Searches and reports are case insensitive for normal text fields, by the way, so if you remove those three characters, you might make it harder to find the exact logs you're looking for. I strongly recommend that you do not do this.
    – sfdcfox
    Apr 27, 2016 at 3:09

1 Answer 1

3

SFDC has two forms of ID .

The API can return either a case-sensitive or a case-insensitive ID field value.

The case-insensitive ID is identical to the 15-character case-sensitive ID with three extra characters appended to indicate the case of each of the original 15 characters. When inserting or updating records, the API accepts either the 15-character case-sensitive ID or the 18-character case-insensitive ID.

You can truncate the last three digits with substring function or another would be to type cast to ID instead of string

SObject_Id__c = (Id) sobj.get('Id');

Note : Please declare SObject_Id__c as the Id data type

1
  • Thanks-- I declared the data type as an Id. Appreciate the clarification about 18 vs 15 char Id strings. I will leave it as untouched for now. Thanks!
    – GPP
    Apr 27, 2016 at 3:13

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