+ Start a Discussion
AnnaTAnnaT 

Loop through fieldname

Please help me...

 

I have 20 E-mail fields named Email1__c, Email2__c, Email3__c,.....Email20__c.

What I want to do is loop thorugh these fields and input value.

 

Hereis my code.

Alerts__c alerts =new Alerts__c();
for(integer j=1;j==MailList.size();j++){
  String iStr = String.Valueof(j);
  String fieldname='Email'+iStr+'__c';
  alerts.get(fieldname)='test@test.com';
}

 But the following error occured.

 Expression cannot be assigned 

 

Please tell me how to solve this problem...

 

Thanks in advance for your help.

 

Anna

 

Best Answer chosen by Admin (Salesforce Developers) 
JHayes SDJHayes SD

Alerts__c alerts =new Alerts__c();
for(integer j=1;j==MailList.size();j++){
String iStr = String.Valueof(j);
String fieldname='Email'+iStr+'__c';
alerts.put(fieldname,'test@test.com');
}

All Answers

rohitrrohitr

Instead of looping directly put all the 20 fields and assign.

JHayes SDJHayes SD

Alerts__c alerts =new Alerts__c();
for(integer j=1;j==MailList.size();j++){
String iStr = String.Valueof(j);
String fieldname='Email'+iStr+'__c';
alerts.put(fieldname,'test@test.com');
}

This was selected as the best answer
AnnaTAnnaT

Thank you very much for your help!!

JHayes SDJHayes SD

You had the right idea :)