You need to sign in to do that
Don't have an account?
Scalable apex code
Hi,
I am running into a bit of an issue with an apex trigger. Inside the apex class that gets called from the trigger, I filter on some of the records I want to work with like so:
for (Object__c ob : recordsFromTrigger){
if (ob.picklist__c== 'X Summer 2013' || ob.picklist__c == 'X Fall 2013' etc etc){
Do something here;
}
But in this case, one of the criteria will change pretty frequently. It is a picklist on the object that will be constantly updated depending on the time of year. For example 'X Summer 2013' 'X Fall 2013'. How do I go about filtering on if that field INCLUDES 'X' and ignores the date portion? Is this possible someway? I'm just wary of hardcoding the individual picklists in and think it will require a lot of maintenance that will be forgotten.
thank you!
Hmm strange, even if it was a formula as long as the formula returned text you shouldn't be getting any null pointer exception.
Based off the line of code you're showing, if that's what's breaking then most likely you'll need to check if the obj.Program__c != null before checking the indexOf. Something like this:
However, this may not fix the problem if this isn't what's throwing the null pointer exception.
All Answers
Is the 'X' portion always going to be the same (aka that in of itself will be hardcoded, the only thing changing is the date portion)?
If so an indexOf search would work since the picklist values are just strings.
e.g.
Yeah, the 'x' portion should stay the same, the rest should vary depending on the year. In reality the picklist will be something like 'XYZ Summer 2013' how wold I change the indexOf to reflect capturing the 3 characters?
Thank you!
Hi,
I would suggest to use Custom Settings to store the value which you want to compare with the picklist Value and then use the custom settings in your code and use getall() method of it.Somethig like below :-
The indexOf method of a string simply finds the index of the value you want to search for in the target string, so you simply swap the 'X' Value with anything you want. For example:
See here for more details on the String API (and look at indexOf specifically for more details)
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_string.htm?SearchType=Stem&Highlight=strings|String|Strings|string|STRING
Hmm..when I try that I get a null pointer exception on that line. Here is the code I am trying to use.
In addition, it turns out the "Program" field is a formula that is rendered from a picklist, would this affect the functionality of indexOf?
Thank you for your help!
Hmm strange, even if it was a formula as long as the formula returned text you shouldn't be getting any null pointer exception.
Based off the line of code you're showing, if that's what's breaking then most likely you'll need to check if the obj.Program__c != null before checking the indexOf. Something like this:
However, this may not fix the problem if this isn't what's throwing the null pointer exception.