1

We have a VF page that allows users to mass update cases based on whatever fields are on the page. The page displays a temp case whose values are then applied to the list of selected cases.

We wanted to update this page to use fieldsets and make it more dynamic and controllable from the UI/Admin. So the page was easy to convert over to fieldsets but then in the controller we need to first detect if the fields in the field set have value, then if they do apply it to the list of cases and then update.

Here is some sample code for what we are trying to do:

private Case genCase = new Case();
private List<Case> allCases = new List<Case>();

public massUpdate() {
  Schema.FieldSet cseFields = Schema.SObjectType.Case.fieldSets.getMap().get('Mass_Update');
  public List<Schema.FieldSetMember> fieldsMembers = cseFields.getFields();
  if (fieldsMembers != null && !fieldsMembers.isEmpty()) {
    this.allCases = Database.query(constructQuereyString(fieldsMembers, this.caseIds));
  }
}

public PageReference newSave(){
  //here is where we are having trouble. Ideally we would like to loop 
  //over the fields in the fieldset and see if there is value in the 
  //this.genCase if there is (meaning someone entered a value in the 
  //page) then we would to apply that field value to the list of cases.. 
  for (Schema.FieldSetMember f : fieldsMembers) {
    //this doesnt work but i think we need something along these lines
    if (this.genCase.f.getFieldPath() != null) {
      for (Case c : this.allCases) {
        //and again this doesnt work but we would then need to dynamically apply it to the list of case fields
        c.f.getFieldPath() = this.genCase.f.getFieldPath();
      }
    }
  }
  // eventually we would then update the list of cases if there is changes made etc
}

private String constructQuereyString(List<Schema.FieldSetMember> pFields, List<Id> pCaseIds) {
  String query = 'SELECT ';
  for (Schema.FieldSetMember f : pFields) {
    query += f.getFieldPath() + ', ';
  }

  for (String s : extraCaseFields) {
    query += s + ', ';
  }
  query += 'Id FROM Case WHERE Id IN ' + fixCaseIdsForQuery(pCaseIds);
  return query;
}

private String fixCaseIdsForQuery(List<Id> pCaseIds) {
  String returnString = ' (';
  for (Id i : pCaseIds) {
    returnString += '\'' + i + '\',';
  }
  returnString = returnString.removeEnd(',');
  returnString += ') ';
  return returnString;
}
2
  • 1
    What's your specific question? – Santanu Boral Jul 19 '17 at 16:42
  • I wrote it in comments in the save method. – SalesForceGirl Jul 19 '17 at 17:03
1

Looping through first the field set (fieldsMembers) and then through the cases selected list (this.allCases). Check if the VF page field member value is null and the use '.put' method to update the entered value into the case.

public PageReference newSave(){

    //loop over fieldset fields for each selected case (this.allCases)
    for (Schema.FieldSetMember f : fieldsMembers) {
        for (Case c : this.allCases) {
            //check if value in genCase field set member is null
            if(genCase.get(f.getFieldPath()) != null && genCase.get(f.getFieldPath()) != BLANK_VALUE) {
                //update the field in the current selected case with the field set value from genCase
                c.put(f.getFieldPath(),genCase.get(f.getFieldPath()));
                }

            }
        } 
1
  • I don't believe this works with FieldSetMembers that have relationships in them, would it? – ca_peterson Jul 20 '17 at 20:56

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.