6

As a simple example, consider the following string, where I can assign it to an sObject field and it retains the original value, but after inserting the sObject, the value returned when querying the record from the database is different:

String MyString = EncodingUtil.base64Decode('AEhlbGxvLCBXb3JsZCE=').toString();
Account a = new Account(Name = MyString);
System.assertEquals(MyString,a.Name);

insert a;
Account a2 = [SELECT Id, Name FROM Account WHERE Id = :a.Id LIMIT 1];
System.assertEquals(MyString,a2.Name);

Is there any way of knowing in advance whether a string has characters that will be dropped when saving them into a text field?

I would like to save the data so that it is displayed as text, and also be sure that I have it stored intact.

1 Answer 1

4

Running this test:

@IsTest
private class MyTest {

    @IsTest
    static void test() {
        String MyString = EncodingUtil.base64Decode('AEhlbGxvLCBXb3JsZCE=').toString();
        Account a = new Account(Name = MyString);
        System.assertEquals(MyString,a.Name);

        insert a;
        Account a2 = [SELECT Id, Name FROM Account WHERE Id = :a.Id LIMIT 1];

        System.assertEquals(codePoints(MyString), codePoints(a2.Name));
        System.assertEquals(MyString,a2.Name);
    }

    private static Integer[] codePoints(String s) {
        Integer[] items = new Integer[] {};
        for (Integer i = 0; i < s.length(); i++) items.add(s.codePointAt(i));
        return items;
    }
}

fails with the message:

System.AssertException: Assertion Failed: Expected: (0, 72, 101, 108, 108, 111, 44, 32, 87, 111, ...), Actual: (72, 101, 108, 108, 111, 44, 32, 87, 111, 114, ...)

i.e. MyString starts with a code point of zero. Not too surprising that when that string has been pushed into a database and pulled back out that such a character is dropped somewhere along the line.

When I base 64 encode:

Hello, World!

I get this that works in the above code:

SGVsbG8sIFdvcmxkIQ==

not:

AEhlbGxvLCBXb3JsZCE=

that results in the zero code point.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .