4

I have a function as below:

public PageReference resendEmails() {
  List<ts2__Placement__c> selectedPlacements = new List<ts2__Placement__c>();
  List<Timecards__c> timecards = new List<Timecards__c>();

  Set<Id> placementIds = new Set<Id>();

  Id templateId = [SELECT Id, Name FROM EmailTemplate WHERE DeveloperName = :template LIMIT 1].Id;
  List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();

  for (pPlacement pPla : getPlacements()) {
    if (pPla.selected == true) {
      selectedPlacements.add(pPla.pla);
    }
  }

  validate(selectedPlacements, startDate, endDate);

  if (errorList.isEmpty() && !selectedPlacements.isEmpty()) {
    Date start = Date.valueOf(startDate);
    Date finish = Date.valueOf(endDate);

    for (ts2__Placement__c pla : selectedPlacements) {
      placementIds.add(pla.Id);
    }

    for (Timecards__c t : [SELECT Id, Contractor__c, Contractor_First_Name__c, Week_start_date__c, Week_end_date__c, Candidate_Email__c, Client_Email__c FROM Timecards__c WHERE Placement__c IN :placementIds AND Week_start_date__c = :start AND Week_end_date__c = :finish]) {
      String[] toAddress = new List<String>();
      toAddress.add('t.' + toField);

      Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

      email.setToAddresses(toAddress);
      email.setTemplateId(templateId);
      email.setTargetObjectId(t.Contractor__c);
      email.setWhatId(t.Id);

      mailList.add(email);
    }


    Messaging.SendEmailResult[] r = Messaging.sendEmail(mailList);

  }

  return null;
}

It's pretty simple, alls it does is uses an e-mail tempate to send an e-mail out whenever resendEmails() is called.

What I want to do though is reuse this by dynamically choosing which field I use. Currently I have the following in the Visualforce Page with the relevant variable in the code:

<apex:commandButton value="Resend Email" action="{!resendEmails}" status="loading" reRender="response">
  <apex:param name="Template" value="Timecard_to_Candidate" assignTo="{!template}" />
  <apex:param name="Candidate" value="Candidate_Email__c" assignTo="{!toField}" />
</apex:commandButton>

This works as expected, setting template to the value Timecard_to_Candidate and toField to Candidate_Email__c and I know I can call these fields by using a String query:

String query = 'SELECT Id, Name, ' + toField + ' FROM Object__c';

The problem is within the for loop, specificically this line:

toAddress.add('t.' + toField);

How can I dynamically choose what field is added to the toAddress list? Putting in some kind of string as above doesn't work, the only way that does is toAddress.add(t.Candidate_Email__c);

Is there any way we can dynamically put in what field is choosen in a .add() method? Or would I have to use if statements in this case?

The idea behind this is that someone can choose what template, object, e-mail address etc... when calling this function.

1 Answer 1

4

you can use the SObject get method to get the field value by passing the field name.

you will end up something like this

toAddress.add((String)t.get(toField));

this will get the value of the passed in field name and add the value to the toAddress list.

2
  • Thanks Vamsi, really appreciate that! I currently have a String field like: public String toField { get; set; }, but that's not compatible with the .get() method. How might I pass this in?
    – Dan Jones
    Mar 14, 2016 at 9:16
  • 1
    might be becos u hv to type cast the output to a string before adding it to the list. i hv updated the answer with casting. Mar 14, 2016 at 9:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .