function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jasonrtaylorjasonrtaylor 

Trigger - Trying to trim leading characters

I am trying to trim leading characters from a string then using that string value in my where clause of my SQL query. I have found several references to a "TrimStart" function in java and .NET, but neither syntax appears to work in the trigger. See the commented code for the .NET "TrimStart" syntax. Does anyone have ideas how to trim leading characters and zeros.

 

Here is my code

trigger LookupBanker on Lead (before insert, before update) { Lead[] leads = Trigger.new; for(Lead lead:leads){ if (lead.employee_id__c!=null){ string origin = lead.employee_id__c; /* string originwoz = origin; originwoz = originwoz.TrimStart('0'); */ Account c = [Select Id, BID__pc FROM Account WHERE RecordTypeId = '012600000000000000' AND BID__pc = :originwoz][0]; lead.Banker_Name__c = c.Id; } } }

 


 

SteveBowerSteveBower

Hi, Apex is not Java or .Net, it is it's own thing.  Check out the methods on the String primitive in the Apex documentation, the trim() method seems to be what you're looking for.

 

Note, you also have a Select statement within your For loop.  Not a good idea, read about Bulk-safety.

 

Note, you're also hard coding the ID of a record type.  Probably not a good idea either as they aren't portable.  Better to query it.

 

So, I think you want (Note, I 'm just typing it in here, so there may be typos etc.):

 

 

trigger LookupBanker on Lead (before insert, before update) {

id recTypeId = [select id from recordtype where sobjecttype='Account' and name='the record type name' limit 1].id;

List<String> accountsToFind = new List<String>();

 

// build a list of the Origin Strings for which you want to find the Accounts.

for (Lead l: trigger.new) {

  if (l.employee_id__c != null) accountsToFind.add(l.employee_id__c.trim();

}

 

// Now, get a List of the Accounts that you're interested in using.

List<Account> accounts = new List<Account>(

[select id, BID_pc from Account where RecordtypeId = :recTypeId and BID_pc in :accountsToFind]

);

 


// First, build a helper Map so you don't have to re-loop through the accounts multiple times.

// Mapping the BID_PC values to the Account object's Id.

Map<String, Id> helper = new Map<String, Account>();

for (Account a: accounts) helper.put(a.BID_pc, a.Id);


// And now go through the leads, lookup and assign the Account Id to the Bankers Name.

for (lead l: trigger.new) {

l.Banker_Name__c = helper.get(l.employee_id__c.trim(),BID_pc);

}

 

But, as always, I could be dead wrong.   Hope it helps, Steve.

 

 

JimRaeJimRae

Steve is right about the code optimization suggestions.

Regarding the original question, you had 2 parts, if I read correctly, trim leading spaces and zeros.

 

Here is a little code snippet example of doing both, using Regex and replaceall.  Someone could probably come up with a better regex that takes care of all of the trim and the leading zeros, or you could probably google search more info on regex expressions to trim leading characters and zeros.

 

 

String[] tests = new String[]{'0000000030494690', '012', '0', '0000', ' 0007000'};
for(String s : tests) {
System.debug(s.trim().replaceAll('^0+(?!$)', ''));
}

 

 

 

Message Edited by JimRae on 11-16-2009 05:05 PM
SteveBowerSteveBower

I forgot to trim leading zero's and not just whitespace.  If you're sure the rest of the data in the field is integers, then you could just convert it to an Integer and then convert it back to a string.

 

However, if you're not sure, then you need to use the String methods, perhaps split?  I'd play with something like:

 

String x = 'whatever that origins field was'; 

 

List<String> xes = s.split('^[0\ ]*',2);

 

String newString = xes[1];

 

although I'd have to check and see if that really does what I think it should.

 

Best, Steve.

doughauck-corpdoughauck-corp
Answering another ancient thread, but as always, it's for those who come looking like I did:

You can accomplish a TrimStart() method in Apex via:  string.substring(string.indexOfAnyBut(' ')).  The substring() method returns all the characters from the specified position forward, and the indexOfAnyBut() method finds the position of the first character that is not in the list you give it. 

Note that the AnyBut search string can take multiple characters and is not order specific, so you could do ' 0' or '0 ' and still get the same results.  However, it does not have an IgnoreCase version, so you will need to use a ToLowerCase() method if you are searching for letters: string.substring(string.toLowerCase().indexOfAnyBut('abc'))

As an example, the code:
string test = '  000123456';
system.debug('test: \'' + test.substring(test.indexOfAnyBut(' ')) + '\'');
system.debug('test: \'' + test.substring(test.indexOfAnyBut('0')) + '\'');
system.debug('test: \'' + test.substring(test.indexOfAnyBut(' 0')) + '\'');
system.debug('test: \'' + test.substring(test.indexOfAnyBut('0 ')) + '\'');
will return the following:
test: '000123456'        //  Remove leading spaces
test: '  000123456'      //  Would remove leading zeroes, but spaces come before zeroes
test: '123456'           //  Remove leading spaces and zeroes
test: '123456'           //  Remove leading spaces and zeroes (to show AnyBut order unimportant)

Just don't ask me for a clean way to do a TrimEnd() method, because there is no LastIndexOfAnyBut() method.  ;-D

Best,
Doug

P.S. - On a more general note, if you can't answer the question being asked, please just don't respond.  It's perfectly fine if you give a wrong answer, or if you misunderstood the question, but do try to limit your answers to the question being asked.  It drives me bonkers when someone asks a question about one thing, and instead of a solution gets a critique of everything else that's wrong with his code, or some version of "why would you want to do that?"  Aside from the fact that it makes you look like a pedantic schmuck, it also clutters up the thread with a bunch of unrelated junk that those who come looking later will have to dig through, to see if there's a relevant solution buried in there somewhere.  It's not helpful, so please just don't do it.  Thanks.