0

I have a process that takes a case id from a url param through js, cases the id correctly (as ids are case sensitive), and passes it to an apex method through an action function.

This process has been working for months and with no change to any of the environments, we received incidents across partial, full, and dev sandboxes as well as production. Partial and full are on summer as well as one dev, the other dev recently refreshed to validate against is on spring.

However, the method is no longer casing the Id correctly, it is not capitalizing the 4th character in the Id. The I should be capitalized, when the I is capitalized, it finds the record and the query proceeds as expected.

This can be seen in the debug:

DEBUG|Before repair: 5003i000001gaieqa2
DEBUG|After repair: 5003i000001gAIEQA2

system.debug('Before repair: ' + caseIdToGetContactId);
caseIdToGetContactId = repairCasing(caseIdToGetContactId);
system.debug('After repair: ' + caseIdToGetContactId);

The logic I am using to correct the casing (obtained from a Stack Exchange I found some time ago, but can no longer locate):

static String CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ012345';

static List<Boolean> getBitPattern(String c)
{
    Integer index = CHARS.indexOf(c.toUpperCase());
    List<Boolean> result = new List<Boolean>();
    for (Integer bitNumber = 0; bitNumber < 5; bitNumber++)
        result.add((index & (1 << bitNumber)) != 0);
    return result;
}

static String repairCasing(String input)
{
    if (String.isBlank(input) || input.length() != 18) return input;

    List<Boolean> toUpper = new List<Boolean>();
    toUpper.addAll(getBitPattern(String.valueOf(input.substring(15, 15))));
    toUpper.addAll(getBitPattern(String.valueOf(input.substring(16, 17))));
    toUpper.addAll(getBitPattern(String.valueOf(input.substring(17, 18))));

    String output = '';
    for (Integer i = 0; i < 15; i++)
    {
        String c = String.valueOf(input.substring(i, i+1));
        output += toUpper[i] ? c.toUpperCase() : c.toLowerCase();
    }
    output += input.substring(15, 18).toUpperCase();
    return output;
}
3
  • Why do you need to I have a process that takes a case id from a url param through js, cases the id correctly (as ids are case sensitive), and passes it to an apex method through an action function. What does your Apex do? normal selects work without dealing with this problem client side at all. Pass either 15 or 18, it will work fine. – zaitsman Jun 3 '20 at 4:45
  • @zaitsman The apex queries related records based on the passed Id. The specific use case passes the case id as a URL param in a new primary console tab and on the Visualforce Page opened in the new primary console tab it grab that URL param, passes it to the Apex Class, and renders the queried information on to the VFP. Is there some way to pass it to the Apex class of that opened tab without using the URL that I'm overlooking? – S.B. Jun 3 '20 at 14:32
  • can you show the signature of your Apex method? For me if i declare the method argument as Id myId (not String myId) i do not need to handle conversion – zaitsman Jun 4 '20 at 0:14
0

The root cause involves two items.

(1) The first 4-5 characters were never being cased correctly:

This: 
toUpper.addAll(getBitPattern(String.valueOf(input.substring(15, 15))));

Should be:  
toUpper.addAll(getBitPattern(String.valueOf(input.substring(15, 16)))); 

(2) Previously the instance Id of the record id (4th of 5th characters according to the stack link below did not contain any alphabetical characters and was purely numeric so the casing on the 4th and 5th characters was irrelevant.

What are Salesforce ID's composed of?

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.