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
mxalix258mxalix258 

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!

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Sean TanSean Tan

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:

 

 

for (object__c obj : recordsFromTrigger){
        if ((obj.Program__c != null && obj.Program__c.indexOf('XYZ') != -1) && (obj.Organization__c == 'ABC' || obj.Organization__c == 'XYZ') && obj.Stage__c == 'Signed'){
            intobrecord.put(obj.Contact__c, obj.Job__c);
        }

 However, this may not fix the problem if this isn't what's throwing the null pointer exception.

All Answers

Sean TanSean Tan

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.

 

for (Object__c ob : recordsFromTrigger){
	if (ob.picklist__c.indexOf('X') != -1){
		 Do something here;
	}

 

mxalix258mxalix258

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!

Vinit_KumarVinit_Kumar

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 :-

 

List<CustomSetting__C> mcs = CustomSetting__C.getall().values();// CustomSetting__C is the custom setting in your org

for (Object__c ob : recordsFromTrigger){
        if (ob.picklist__c== 'X Summer 2013' || ob.picklist__c == mcs[0].CustomSettingfield__c etc etc){ // Assuming CustomSettingfield__c is field on CustomSetting__C where the value is getting stored which needs to be comared with picklist value
             Do something here;
        }

 

 

 

Sean TanSean Tan

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:

 

for (Object__c ob : recordsFromTrigger){
	if (ob.picklist__c.indexOf('XYZ') != -1){
		 Do something here;
	}

 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

mxalix258mxalix258

Hmm..when I try that I get a null pointer exception on that line. Here is the code I am trying to use.

 

    for (object__c obj : recordsFromTrigger){
        if ((ojb.Program__c.indexOf('XYZ') != -1) && (obj.Organization__c == 'ABC' || obj.Organization__c == 'XYZ') && obj.Stage__c == 'Signed'){
            intobrecord.put(obj.Contact__c, obj.Job__c);
        }

 

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!

Sean TanSean Tan

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:

 

 

for (object__c obj : recordsFromTrigger){
        if ((obj.Program__c != null && obj.Program__c.indexOf('XYZ') != -1) && (obj.Organization__c == 'ABC' || obj.Organization__c == 'XYZ') && obj.Stage__c == 'Signed'){
            intobrecord.put(obj.Contact__c, obj.Job__c);
        }

 However, this may not fix the problem if this isn't what's throwing the null pointer exception.

This was selected as the best answer