3

Currently I'm pulling through a list of records from a VisualForce/Apex Page. What I'm trying to do is iterate through this list and update it.

Below successfully outputs the records in the Debug logs:

public PageReference updateTargets() {
  List<Revenue_Targets__c> targets = new List<Revenue_Targets__c>();

  for (revenueTarget rt : runQuery())
    targets.add(rt.revTar);

  System.debug('These are the selected records...');
  for(Revenue_Targets__c rt : targets)
    system.debug(rt );

  targets = null;

  return null;
}

Good stuff! However, when I modify this to update the records rather than debug it, nothing happens!

public PageReference updateTargets() {
  List<Revenue_Targets__c> targets = new List<Revenue_Targets__c>();

  for (revenueTarget rt : runQuery())
    targets.add(rt.revTar);

  update targets;

  targets = null;

  return null;
}

I've got a Wrapper class which I'll include here if that helps shed any light on anything:

public List<revenueTarget> runQuery() {
  try {
    targets = new List<revenueTarget>();
    for (Revenue_Targets__c revTar : Database.query(query))
      targets.add(new revenueTarget(revTar));
  }
  catch (Exception e) {
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Oops!' + e));
  }
  return targets;
}
public class revenueTarget {
  public Revenue_Targets__c revTar {
    get;
    set;
  }
  public revenueTarget(Revenue_Targets__c rt) {
    revTar = rt;
  }
}

I'm confused as to why this isn't updating, so any help here would be appreciated!

Thanks!

Edit I've tried a few different ways, including this one (although it doesn't seem best practice):

public PageReference updateTargets() {
    List<Revenue_Targets__c> targets = new List<Revenue_Targets__c>();

    for (revenueTarget rt : runQuery())
      targets.add(rt.revTar);

    for (Revenue_Targets__c rt : targets) {
      Revenue_Targets__c currentTargets = Database.query(query);

      currentTargets.Id = rt.Id;
      currentTargets.Client_Interview_Target__c = rt.Client_Interview_Target__c;
      currentTargets.CV_Sell_In_Target__c = rt.CV_Sell_In_Target__c;
      currentTargets.CVs_Submitted_Target__c = rt.CVs_Submitted_Target__c;
      currentTargets.Revenue_Placement_Target__c = rt.Revenue_Placement_Target__c;
      currentTargets.Meeting_Target__c = rt.Meeting_Target__c;
      currentTargets.New_Vacancies_Target__c = rt.New_Vacancies_Target__c;
      currentTargets.Placement_Target__c = rt.Placement_Target__c;
      currentTargets.Reverse_References_Target__c = rt.Reverse_References_Target__c;
      currentTargets.UA_Interview_Target__c = rt.UA_Interview_Target__c;
      currentTargets.Target__c = rt.Target__c;

      update currentTargets;
    }

    targets = null;

    return null;   }

Edit 2 I'll include the runQuery() and runSearch() functions below here, as requested.

public PageReference runSearch() {
  String startDate = getStartDate();
  String endDate = getEndDate();

  query = 'SELECT Id, Consultant__c, Title__c, Manager__c, Client_Interview_Target__c, CV_Sell_In_Target__c, CVs_Submitted_Target__c, Revenue_Placement_Target__c, Meeting_Target__c, New_Vacancies_Target__c, Placement_Target__c, Reverse_References_Target__c, UA_Interview_Target__c, Target__c, Month__c, Year__c';
  query += ' FROM Revenue_Targets__c';
  query += ' WHERE Consultant__c != null';
  query += ' AND Target_Date__c >= ' + startDate;
  query += ' AND Target_Date__c <= ' + endDate;

  if (selectedChannel != 'All')
    query += ' AND Consultant__r.Multi_Channel_Services__c = \'' + selectedChannel + '\'';

  if (selectedDivision != 'All')
    query += ' AND Consultant__r.Multi_Channel_Services__c = \'' + selectedDivision + '\'';

  if (selectedConsultant != 'All')
    query += ' AND Consultant__r.Name = \'' + selectedConsultant + '\'';

  runQuery();

  return null;
}

public List<revenueTarget> runQuery() {
  try {
    targets = new List<revenueTarget>();

    for (Revenue_Targets__c revTar : Database.query(query))
      targets.add(new revenueTarget(revTar));
  }
  catch (Exception e) {
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Oops!' + e));
  }

  return targets;
}
  • 1
    I see your line "update targets;" that will put any changes back into Saleforce, but I don't see anywhere that you change any values of items in list "targets". Have you left that code out? – Jagular Jul 29 '15 at 16:29
  • Everything looks good here. You're not running into a validation rule or other exception, are you? – sfdcfox Jul 29 '15 at 16:37
  • Hey @Jagular, I've updated my answer, but I get the same result here where nothing happens still. The page just seems to rerender without applying any updates fields. – Dan Jones Jul 29 '15 at 16:51
  • 1
    Ack, take the list definition out of the loop. Use a map if the targets are that different. Also create a different list to contain the items you want to update, that way you can do it outside of the loop. – Jason Hardy Jul 29 '15 at 16:52
  • Are you sure the records are getting updated?try wrapping the update statement within a try catch block and look for DML exceptions in the catch block – Rao Jul 29 '15 at 16:53
1

After perspiring much sweat, spilling much blood, shedding many tears and swearing at Salesforce I've finally found the answer. After noticing in the debug logs that new values weren't being passed through, I assumed the problem as I suspected initially lay here:

public PageReference updateTargets() {
  List<Revenue_Targets__c> targets = new List<Revenue_Targets__c>();

  for (revenueTarget rt : runQuery())
    targets.add(rt.revTar);

  update targets;

  targets = null;

  return null;
}

Specifically in this part:

for (revenueTarget rt : runQuery())

As I understand it, rather than iterating over the list I already got and was displaying in the InputFields, I was effectively querying it again thus removing any changes that were made within the VisualForce page. I imagine it was updating, but it was updating with the exact same values.

So, for anyone who might have this same problem, the below code works and updates.

public PageReference updateTargets() {
  List<Revenue_Targets__c> updatedTargets = new List<Revenue_Targets__c>();

  for (revenueTarget rt : targets)
    updatedTargets.add(rt.revTar);

  update updatedTargets;

  updatedTargets = null;

  return null
}

The original list was being stored in a global list called targets, so I just had to iterate over that.

This probably isn't the best code in the world, but I'm glad it works.

I appreciate the help you guys gave. Definately pointed me in the right direction!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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