You need to sign in to do that
Don't have an account?
Urgent help required for assigning variable name dynamically in Apex
Hi,
Please find below the scenario.
I have to work with dynamic SOQL. Because i may need to select diffent fields from the object at runtime.
So, consider,
String fieldString = 'name, id, email';
List<String> FieldList = new List<String();
fieldList.add('Name');
fieldList.add('Id');
fieldList.add('Email');
List<Contact> cList = Database.Query('Select '+fieldString +'From Contact limit 10');
As my fieldString contains 3 fields, i will have to form 3 maps in the below for loop.
If String contains 4 fields, i will have to create 4 maps accordingly.
//Creating dymnamic maps here according to the number of fields in fieldList.
for(integer i = 0; i< fieldList.size();i++){
Map<String,String> map+fieldList[i] = New Map<String,String>();
// i need something like this..so that i will have 3 maps here with the different map names.
But dynamic creatiion of variable name is not possible here in: map+fieldList[i] (to form map names like: mapName, mapId, mapEmail dynamically...);
}
So, is there any way to name the variables dynamically in Apex?
Thanks in advance for any help...!!!
Yes, so rather than trying to use the dotted notation to access the field, you'd use the get notation:
All Answers
No, you can't create the names dynamically.
When I've been in a similar scenario, I've gone one of two routes
(1) Store my maps in another map, that is keyed by the dynamically generated name.
Something like:
(2) Create a custom class that wraps a name and the associated map. You still need somewhere to store this though, so in effect its no different to the above, but it can make the code slightly easier to read:
Hi Bob,
Thanks for your reply.
But i need to implement extendedd functionality as explained below:
User will select email template from dropdown, all the templates will be based on same object. He has to pick one.
I need to get all merge fields used in this template..
Foe ex:
SampleTemplate is slected by user which is on Contact object,
So, i will get all merge fields used in this template and will match them with contact's field,
if it is a same field, i will put actual value in that field into the merge field place.
Map<String, Map<String, String>> allMap=new Map<String, Map<String, String>();
so, ContactList = [select email, id ,name from contact limit 10]
EmailTemplate e = [select body,htmlvalue,subject from EmailTemplate where name = 'SampleTemplate'];
// logic for getting all merge fields from teplate.
Suppose template contains, 3 merge fields,
so i need to implement like this:
for(Contact c : con:List)
{
for(String s : MergeFieldList){
if(s.equals(contactfield)){
//Say merge field name is email. so condition is true, now i need to create a map with name mapMail
and assign value as only contact email.
//Something like this:i have to create mapEmail in which need to put only contact.email as value.
allmap.put('map'+s,c.s);
//but c.s is not allowed as s is variable and not the contact field.So unable to put the value.
}
}
Please help me regarding this above explained scenario...
Help appriciated...!!!
c.s is a single instance of a string - you won't be able to put that into the allMap as the value has to be a Map with strings as keys and values.
Sorry, forgot to mention...
please observe below block...
for(Contact c : con:List)
{
for(String s : MergeFieldList){
if(s.equals(contactfield)){
//Say merge field name is email. so condition is true, now i need to create a map with name mapMail
and assign value as only contact email.
//Something like this:i have to create mapEmail in which need to put only contact.email as value.
Map newFieldMap = New <String,String>();
// IAM FACING PROBLEM IN BELOW LINE.. WHILE PUTTING DYNAMIC VALUE INTO INNERMAP..
//INNER MAP IS SINCLE INSTANCE ACCEPTING STRING BUT WILL NOT ALLOW ME TO PUT VALUE LIKE..
newFieldMap.put(s, c.s);
//COZ, HERE C.S IS GIVING ERROR.. AFTER '.' I CAN PUT ONLY FIELD NAME HARDCODED.. NOT THE VARIABLE S...
allmap.put('map'+s,newFieldMap);
}
}
Yes, so rather than trying to use the dotted notation to access the field, you'd use the get notation:
Yes, just was working on that and it executed successfully
Thanks a lot...