Hot answers tagged

6 votes

CPU Time consumed by Apex String comparison

Note that part of your problem is that you're creating a very large string. Your code also consequently has a logic bug. You never clear out the str variable, so it grows in size, and that growth is ...
user avatar
  • 439k
5 votes

CPU Time consumed by Apex String comparison

I would say that it is the string concatenation that is causing the high CPU usage rather than the comparison itself - that's certainly our experience. I would always recommend using an object array ...
user avatar
  • 28.1k
5 votes
Accepted

Map or String: what do I have to use?

Using a map like that obfuscates the parameters, making it so developers have to read the method to know what parameters to use. Worse, these kinds of maps are case sensitive. I would strongly ...
user avatar
  • 439k
5 votes
Accepted

char vs String in Apex

I compared both of those methods to another method you may have overlooked: getChars. I got the following times in MS for each (lower is better): Method Time charAt 2512 substring 3338 getChars ...
user avatar
  • 439k
5 votes
Accepted

How to replace a SOQL query with a string?

Instead of for (List<AggregateResult> agg: query) you should have for (List<AggregateResult> agg: Database.query(query)). You can find additional detail regarding Dynamic SOQL here. Best ...
user avatar
5 votes

How to check if string is null or not?

Instead of checking for null and ''. Use System methods from String class. if (String.isNotBlank(txtImportSection1) && String.isNotBlank(txtImportSection2) && String....
user avatar
4 votes

how to replace the value in SET in apex

First, check if it is present, second, remove found element, third, put replacement: Set<String> mySet = new Set<String>(); mySet.add('this'); mySet.add('is'); mySet.add('the'); mySet.add('...
user avatar
3 votes

I want a trigger to change case status to close if it satisfies the conditions

A few notes to start things off Your try/catch in this code should be removed There is no need to check if updatedCase is empty before performing DML on it It is impossible for the code in your try/...
user avatar
  • 49.5k
3 votes
Accepted

What does the .substring(3) function do?

From the String class documentation substring(startIndex) Returns a new String that begins with the character at the specified zero-based startIndex and extends to the end of the String. So given ...
user avatar
  • 49.5k
3 votes
Accepted

Remove text from String between characters which appear more than once

You can use two splits which will work for both single set and multiple values. Also remember to use \\ as | gets treated as special character. String str = 'John Test | jtest@none.com'; List<...
user avatar
3 votes

How to check if string is null or not?

An alternative way to do this, with a bit less typing, is to (ab)use a Set. Add everything to the set, remove nulls and blanks, and check to see if the set is empty or not. Set<String> ...
user avatar
  • 49.5k
3 votes

How to pass ID array from LWC to Apex for VF page parameter

I discovered that the issue was coming from a character limitation on pdfPage.getParameters() (seemed to be limited at 205 characters for me) I got around it by querying using the id of parent object ...
user avatar
  • 436
2 votes

Not able to remove substring that is variable from a string

for this kind of task regex is the best instrument to use. Here is a link to test regex for your needs. You can also read an explanation for regex: "(!Contract.)(.*)(?=})" 1st Capturing ...
user avatar
2 votes
Accepted

Get element from list which contains element from another list

Unfortunately, since the second string is looking for a partial text match, I don't see a way to avoid looping over each element to compare them. If you had a full text element match, the inner loop ...
user avatar
  • 642
2 votes

Apex Strings : Pipe symbol (|) is converted to &#124; in string variable while querying

The error you're seeing is actually just how the platform likes to encode some characters as part of an error response, and has nothing to do with your problem. The error is telling you, quite ...
user avatar
  • 439k
2 votes

Map or String: what do I have to use?

Just some general notes: sfdcfox's answer is, as always, brilliant. Using a map like that isn't a great idea, and I'd go so far as to say it's a bad idea. I like your desire to let other developers ...
user avatar
  • 7,008
2 votes

Escaping quotes in csv for Data Loader

RFC 4180 specifies how (most) CSVs are encoded. In the file, a cell with ", ,, \r, or \n must be quoted. In other words, "foo, bar, fizz, buzz" should be the correct escape sequence. ...
user avatar
  • 439k
2 votes
Accepted

Removing leading zeroes in Date String

If you really want a locally formatted date, you can use Date.parse and Date.format: String shipDate = '04/06/2022'; System.debug(Date.parse(shipDate).format()); However, be aware that Date.parse ...
user avatar
  • 439k
2 votes

I want a trigger to change case status to close if it satisfies the conditions

First of all, == is case insensitive for string comparisons; you don't need to use toUpperCase() in your code. However, in my modified example, we'll be using it for efficiency by checking values in a ...
user avatar
  • 439k
2 votes

Marketing Cloud - SQL - Merge values from three columns into one

Assuming the fields are never null, it's simply: Concat(PO, ID, Domain) as NewColumn
user avatar
1 vote
Accepted

How can I generate arrays [...] to serialize an object? I want to make a simple JSON serialized

Assuming your question is how to generate arrays [...] using this coding pattern, the answer is to use new List<Object> for those e.g.: String payload = JSON.serialize( new Map<String, ...
user avatar
  • 131k
1 vote

How can I send a really large string value to a method?

You can't exceed the maximum heap size in one string. This is a governor limit. Make your string smaller so that it fits in Limits.getLimitHeapSize (which will be 6,000,000 in a unit test). Using just ...
user avatar
  • 439k
1 vote
Accepted

How we can convert contentVersion Title to string ,i am getting some type casting error

Welcome to SFSE, and as explained by @TSmith, please follow the guidelines of SFSE in all of the questions. There are multiple best practices missed in the code but to solve the problem refer below. ...
user avatar
  • 417
1 vote
Accepted

Splitting E2C Source Email Addresses

Assuming you are referring to EmailMessage.ToAddress field - then because this is a text area long field (length 4000), you will not be able to use formula expressions. You will need to either: If ...
user avatar
  • 63k
1 vote
Accepted

How to replace __c in a string?

you can try to use https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_fields_describe.htm fieldMap.get(fieldName).getDescribe().getLabel();//It provides to get ...
user avatar
  • 426
1 vote
Accepted

How to handle validations from trigger to show as exception or toast on managed package layout

Your redirectToPage method does nothing, as it doesn't return the PageReference back to the client. Further, you can't both throw an exception and return a value. In addition, you should be throwing ...
user avatar
  • 439k
1 vote
Accepted

unescape String APEX

Don't JSON-encode your strings; the platform does this for you automatically. methodName({strLst: strLstLWC}); By double-encoding, you're making more work for yourself when you later have to double-...
user avatar
  • 439k
1 vote

Escaping quotes in csv for Data Loader

If you find it difficult to can always use .tsv file (tab delimitated file)
user avatar
1 vote

Map or String: what do I have to use?

Your syntax is quite incorrect. Assuming this is just a snippet I am ignoring those. You can go for a Map<String, String> solution here, as you just need one parameter to pass: Original Method: ...
user avatar
1 vote
Accepted

Match the substring in a string and append the substring with #tags

This is much easier and less code to implement by means of regex. You did a good attempt. A couple of minor changes are required. Take a look at the working online example https://regex101.com/r/...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible