Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have multiple fields that are named sequentially, i.e. nv001_c, nv002_c, nv003_c and so on till nv026_c. Is there a way for me to streamline the code below?

I know apex is different than VBA and apex is strongly typed but I am sure I made it harder for myself than it needed to be.

I have enclosed a small example of the code below. I am setting the scale for the individual fields dependent upon another variable.

// Set the Scale

If(mm == true){
    sc = 2;
} else {
    sc = 4;
}

//Nominal Values

if(toolResults[0].NV001__c != null){
    toolResults[0].NV001__c = toolResults[0].NV001__c.SetScale(sc);
}
if(toolResults[0].NV002__c != null){
    toolResults[0].NV002__c = toolResults[0].NV002__c.SetScale(sc);
}
if(toolResults[0].NV003__c != null){
    toolResults[0].NV003__c = toolResults[0].NV003__c.SetScale(sc);
}
if(toolResults[0].NV004__c != null){
    toolResults[0].NV004__c = toolResults[0].NV004__c.SetScale(sc);
}

Thanks in advance.

share|improve this question
    
if (mm){, you don't need to check if (true == true). –  dphil Mar 24 at 15:48
    
Thanks @mast0r. I need to remember this one as well. Makes things simpler. –  KeithPou Mar 24 at 19:36

1 Answer 1

up vote 5 down vote accepted

All SObjects support map-like get and put methods (see sObject Class) so the fields can be accessed using strings for the names:

SObject sob = toolResults[0];
for (Integer i = 1; i <= 26; i++) {
    String field = (i < 10 ? 'NV00' + i : 'NV0' + i) + '__c';
    if (sob.get(field) != null) {
        Decimal d = (Decimal) sob.get(feld);
        sob.put(field, d.setScale(sc));
    }
}

PS One version of the field names in an array (see comment discussion); with only 26 I would just type them in as below (mainly for the clarity it brings to the code) but you could create the array with a loop if you wanted to.

String[] fields = new String[] {
        'NV001__c',
        'NV002__c',
        'NV003__c',
        ...
        };
SObject sob = toolResults[0];
for (String field : fields) {
    Object o = sob.get(field);
    if (o != null) {
        sob.put(field, ((Decimal) o).setScale(sc));
    }
}
share|improve this answer
    
This is a good approach but it could perhaps be made more readable by moving the field names into a field array and iterating over that. –  Phil Rymek Mar 24 at 15:26
    
@PhilRymek Fair enough, particularly if the field names were used multiple times. Hopefully the get/put is the key take away here though. –  Keith C Mar 24 at 15:41
    
Thanks. I knew there was a better way to do this. This makes perfect sense. Phil, what would the code look like if the field names were moved into an Array? Keith C, thanks for your information. I think the big takeaway is the get/put. –  KeithPou Mar 24 at 17:04
    
Thanks, that makes sense now. I appreciate all of the advice from both of you. –  KeithPou Mar 24 at 18:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.