You need to sign in to do that
Don't have an account?
Accessing string values from sObject field objects
Hi, I am relatively new to Apex and have a query regaring accessing string values from and sObject.
I am trying to return all of the records in a custom object which essentially has two picklist fields Office and RLT and place the results into a map.
To do this I am using a for loop to iterate through the records in my custom sObject.
The problem I have is how to return the field Object for each iteration of the for loop and how to get that object as a string, so I can put it into the map.
Also my use of the DescribeFieldResult is invalid.
My code is as follows:
for (Offices__c O : [SELECT Office__c, RLT__c FROM Offices__c]) {
Schema.DescribeFieldResult fOffice = Offices_c.Office_c.getDescribe();
String sOffice;
sOffice = fOffice.getDefaultValue();
Schema.DescribeFieldResult fRLT = Offices_c.RLT_c.getDescribe();
String sRLT;
sRLT = fRLT.getDefaultValue();
OfficeRLTMapping.put(sOffice,sRLT);
}
Thanks
Jim
What you're doing there is getting the exact same value repeatedly, namely the default value of the picklists. What you need to do is much simpler:
for (Offices__c O : [SELECT Office__c, RLT__c FROM Offices__c]) {
OfficeRLTMapping.put(O.Office.__c,O.RLT__c);
}
If you want to get another field, just add it to your query (right after the SELECT), and then you can reference it by O.<fieldname>. I reference it by O because you declare it that way in your for loop:
for (Offices__c O : [SELECT Office__c, RLT__c FROM Offices__c])
All Answers
What you're doing there is getting the exact same value repeatedly, namely the default value of the picklists. What you need to do is much simpler:
for (Offices__c O : [SELECT Office__c, RLT__c FROM Offices__c]) {
OfficeRLTMapping.put(O.Office.__c,O.RLT__c);
}
If you want to get another field, just add it to your query (right after the SELECT), and then you can reference it by O.<fieldname>. I reference it by O because you declare it that way in your for loop:
for (Offices__c O : [SELECT Office__c, RLT__c FROM Offices__c])
Hi werewolf,
Thanks so much for your reply. I have implemented the for loop as per your suggestion and this appears to be working. Previous implementation of the code had String values added to a Map for about 35 Offices, so this loop will save that many lines of code!
What I am still slightly hazy on is what exactly was wrong with my initial attempt. Why exactly was I getting the same value repeatedly and getting the default value from the picklist?
If you could elaborate on that, I would really appreciate it.
Jim
The problem with what you wrote there was this:
sOffice = fOffice.getDefaultValue();
That does not do what you thought it did. In fact, that method call gets the default value of the picklist you're asking about. That default value can surely be useful at times, but what you wanted was the currently selected value of the picklist for the record you're querying -- that's what the code I wrote does. As that's a much more common operation, the code for that is much shorter, as you can see.
Thanks a lot. That makes a lot of sense.
Jim
Here is post that tries to clear the confusion around default values
http://www.tgerm.com/2010/01/salesforce-apex-default-values.html