Generic Mass Edit Page

Hi,

This is really nice piece of code that can be reuse for any object.
The following visual page allow you to mass edit many records quickly with javascript.
Any comment will be appreciate.

1.Usage:
Build the URL as follow:
/apex/MassEditPage?ids= < list of records ds seperated by :> &obj= <object API name> &fSet= <fields set for the object>

Parameters:
ids - is the records ids that you want to edit seperated by ':'
obj - abject API name of the records you updating
fSet - field set of the fields you want to modify.

2.functionality:
The functionality is to display list of records in page block table, and above single "dummy" record, that allowing to use mass edit. each time a field in the "dummy" record is changed we run javascript code to change the corresponding field in the list.




3.Field set:
The use of field Set (the third parameter in the URL) allowing the page to work dynamically, and of course allowing also for update the list of editable fields without changing the code.

How to create Field Set?



public class vf_MassEditPage   {  
 
 public list<sObject> objLst {get; set;}               //list of object to update  
 public list<String> objFieldLst {get; set;}          //list of fields in the object  
 public map<String, String> fieldAPI_Label {get; set;}  
 public list<sObject> dummyObjLst {get; set;}     //list with 1 object- use for mass update in the page  

 public vf_MassEditPage()  {  
  
  objFieldLst=new list<String>();  
  fieldAPI_Label=new map<String, String>();  

  //collect the object id from URL  
  list<String> objidLst=new list<String>();  
  for (String id : ApexPages.currentPage().getParameters().get('ids').split(':'))  {
   objidLst.Add(id);  
  }

  String objName=ApexPages.currentPage().getParameters().get('obj');  
  String fieldSetName=ApexPages.currentPage().getParameters().get('fSet');  

  Schema.SObjectType sObjectTypeObj = Schema.getGlobalDescribe().get(objName);  
  Schema.DescribeSObjectResult describeSObjectResultObj = sObjectTypeObj.getDescribe();  
  Schema.FieldSet fieldSetObj = describeSObjectResultObj.FieldSets.getMap().get(fieldSetName);  

  //build sql to get all the objects  
  String sql='SELECT id';  
  for(Schema.FieldSetMember field : fieldSetObj.getFields())  {  
   sql+=',' + field.getFieldPath();  
   objFieldLst.add(field.getFieldPath());  
   fieldAPI_Label.put(field.getFieldPath(), field.getLabel());  
  }  
  sql+=' FROM ' + objName + ' WHERE id in : objidLst';  

  objLst=Database.query(sql);  

  dummyObjLst=new list<sObject>{sObjectTypeObj.newSobject()};  
 }  

 public PageReference saveData()  {  
  try  {  
   update objLst;  
  }  
  catch(Exception e)  {  
   ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Error while saving ' + e));  
  }  
  return ApexPages.currentPage();  
 }  
}  


page:

<apex:page controller="vf_MassEditPage" sidebar="false" id="pageid">  

 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>  
 <script type="text/javascript">  

  function updateAll(inputComp, fieldTarget)  {  
   var inputElem = document.getElementsByTagName("input");  
   for(var i=0; i < inputElem.length; i++)   {  
    if (inputElem[i].className == 'cls_' + fieldTarget)   {  
     inputElem[i].value = inputComp.value;  
    }       
   }  
  }  
 </script>  

 <apex:form id="frm">  
  <apex:pageMessages id="errors" escape="false" />  
  <apex:pageBlock mode="edit" id="results">  
   <apex:pageBlockButtons location="top" id="buttons">  
    <apex:commandButton id="Save" value="Save" action="{!saveData}" />  
   </apex:pageBlockButtons>  

   <apex:pageBlockTable value="{!dummyObjLst}" var="obj" id="editLst1" >  
    <apex:repeat value="{!objFieldLst}" var="fieldName" id="editLst2">  
     <apex:column headerValue="{!fieldAPI_Label[fieldName]}">  
      <apex:inputField value="{!obj[fieldName]}" onChange="updateAll(this, '{!fieldName}')"/>  
     </apex:column>  
    </apex:repeat>  
   </apex:pageBlockTable>  

   <apex:pageBlockTable value="{!objLst}" var="obj" id="objData1" >  
    <apex:repeat value="{!objFieldLst}" var="fieldName" id="objData2">  
     <apex:column >  
      <apex:inputField styleClass="cls_{!fieldName}" value="{!obj[fieldName]}"/>  
     </apex:column>  
    </apex:repeat>  
   </apex:pageBlockTable>  
  </apex:pageBlock>  
 </apex:form>  
</apex:page>  

No comments:

Post a Comment

Retire of Permission on Profiles

If you are working as a Salesforce admin/developer you've probably heard somewhere that Salesforce is planning to make a significant cha...