Creating an Apex REST endpoint to receive updates for Opportunities. The caller of the endpoint will send the full Lightning URL for an Opportunity. For example: https://example.lightning.force.com/lightning/r/0061U00000Gs7RqQAJ/view.

In a report of Opportunities with their Ids, it looks like there are 3 extra chars on the end of the ID in the URL. For example, the above Opportunity has an Id of 0061U00000Gs7Rq rather than 0061U00000Gs7RqQAJ. I need the Id to populate a Lookup(Opportunity) field in an SObject.

Here's the code I'm using to extract the ID from a URL...

  private static ID getSalesforceId(String url) {
    Pattern urlPattern = Pattern.compile('.*/lightning/r/([a-zA-Z0-9]{15,18})/.*');
    Matcher urlMatcher = urlPattern.matcher(url);
    ID opportunityId;
    if (urlMatcher.matches()) {
      opportunityId = (ID) urlMatcher.group(1);
    return opportunityId;


  1. Is there an existing method I can call to extract the Id?
  2. If not, should I clip the last three characters of the Id portion of the URL passed to me or can I use that longer Id to set the value of a Lookup(Opportunity) field?

1 Answer 1


The longer id is a Case Insensitive version of the other one.

They are equivalent in all circumstances (other than string comparisons)

You can convert the Id if you like, like this:

String shortId = String.valueOf(longId).substring(0, 15);

But there is no need - what you have will work fine.

You must log in to answer this question.

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