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;
}