Retrive List of All WorkFlow Rule And Validation Rule Using Metadata

Print List of Workflow or Validation Rule

Create a Class For MetaData Service

public class MetadataService {
    public class ListMetadataQuery {
        public String folder;
        public String type_x;
        private String[] folder_type_info = new String[]{'folder','http://soap.sforce.com/2006/04/metadata',null,'0','1','false'};
        private String[] type_x_type_info = new String[]{'type','http://soap.sforce.com/2006/04/metadata',null,'1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/2006/04/metadata','true','false'};
        private String[] field_order_type_info = new String[]{'folder','type_x'};
    }
    public class FileProperties {
        public String createdById;
        public String createdByName;
        public DateTime createdDate;
        public String fileName;
        public String fullName;
        public String id;
        public String lastModifiedById;
        public String lastModifiedByName;
        public DateTime lastModifiedDate;
        public String manageableState;
        public String namespacePrefix;
        public String type_x;
        private String[] createdById_type_info = new String[]{'createdById','http://soap.sforce.com/2006/04/metadata',null,'1','1','false'};
        private String[] createdByName_type_info = new String[]{'createdByName','http://soap.sforce.com/2006/04/metadata',null,'1','1','false'};
        private String[] createdDate_type_info = new String[]{'createdDate','http://soap.sforce.com/2006/04/metadata',null,'1','1','false'};
        private String[] fileName_type_info = new String[]{'fileName','http://soap.sforce.com/2006/04/metadata',null,'1','1','false'};
        private String[] fullName_type_info = new String[]{'fullName','http://soap.sforce.com/2006/04/metadata',null,'1','1','false'};
        private String[] id_type_info = new String[]{'id','http://soap.sforce.com/2006/04/metadata',null,'1','1','false'};
        private String[] lastModifiedById_type_info = new String[]{'lastModifiedById','http://soap.sforce.com/2006/04/metadata',null,'1','1','false'};
        private String[] lastModifiedByName_type_info = new String[]{'lastModifiedByName','http://soap.sforce.com/2006/04/metadata',null,'1','1','false'};
        private String[] lastModifiedDate_type_info = new String[]{'lastModifiedDate','http://soap.sforce.com/2006/04/metadata',null,'1','1','false'};
        private String[] manageableState_type_info = new String[]{'manageableState','http://soap.sforce.com/2006/04/metadata',null,'0','1','false'};
        private String[] namespacePrefix_type_info = new String[]{'namespacePrefix','http://soap.sforce.com/2006/04/metadata',null,'0','1','false'};
        private String[] type_x_type_info = new String[]{'type','http://soap.sforce.com/2006/04/metadata',null,'1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/2006/04/metadata','true','false'};
        private String[] field_order_type_info = new String[]{'createdById','createdByName','createdDate','fileName','fullName','id','lastModifiedById','lastModifiedByName','lastModifiedDate','manageableState','namespacePrefix','type_x'};
    }
    public class SessionHeader_element {
        public String sessionId;
        private String[] sessionId_type_info = new String[]{'sessionId','http://soap.sforce.com/2006/04/metadata',null,'1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/2006/04/metadata','true','false'};
        private String[] field_order_type_info = new String[]{'sessionId'};
    }
    public class DebuggingInfo_element {
        public String debugLog;
        private String[] debugLog_type_info = new String[]{'debugLog','http://soap.sforce.com/2006/04/metadata',null,'1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/2006/04/metadata','true','false'};
        private String[] field_order_type_info = new String[]{'debugLog'};
    }
    public class CallOptions_element {
        public String client;
        private String[] client_type_info = new String[]{'client','http://soap.sforce.com/2006/04/metadata',null,'1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/2006/04/metadata','true','false'};
        private String[] field_order_type_info = new String[]{'client'};
    }
    public class AllOrNoneHeader_element {
        public Boolean allOrNone;
        private String[] allOrNone_type_info = new String[]{'allOrNone','http://soap.sforce.com/2006/04/metadata',null,'1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/2006/04/metadata','true','false'};
        private String[] field_order_type_info = new String[]{'allOrNone'};
    }
    public class DebuggingHeader_element {
        public MetadataService.LogInfo[] categories;
        public String debugLevel;
        private String[] categories_type_info = new String[]{'categories','http://soap.sforce.com/2006/04/metadata',null,'0','-1','false'};
        private String[] debugLevel_type_info = new String[]{'debugLevel','http://soap.sforce.com/2006/04/metadata',null,'1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/2006/04/metadata','true','false'};
        private String[] field_order_type_info = new String[]{'categories','debugLevel'};
    }
    public class LogInfo {
        public String category;
        public String level;
        private String[] category_type_info = new String[]{'category','http://soap.sforce.com/2006/04/metadata',null,'1','1','false'};
        private String[] level_type_info = new String[]{'level','http://soap.sforce.com/2006/04/metadata',null,'1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/2006/04/metadata','true','false'};
        private String[] field_order_type_info = new String[]{'category','level'};
    }
    public class listMetadata_element {
        public MetadataService.ListMetadataQuery[] queries;
        public Double asOfVersion;
        private String[] queries_type_info = new String[]{'queries','http://soap.sforce.com/2006/04/metadata',null,'0','-1','false'};
        private String[] asOfVersion_type_info = new String[]{'asOfVersion','http://soap.sforce.com/2006/04/metadata',null,'1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/2006/04/metadata','true','false'};
        private String[] field_order_type_info = new String[]{'queries','asOfVersion'};
    }
    public class listMetadataResponse_element {
        public MetadataService.FileProperties[] result;
        private String[] result_type_info = new String[]{'result','http://soap.sforce.com/2006/04/metadata',null,'0','-1','false'};
        private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/2006/04/metadata','true','false'};
        private String[] field_order_type_info = new String[]{'result'};
    }
    public class MetadataPort {
        public String endpoint_x = URL.getSalesforceBaseUrl().toExternalForm() + '/services/Soap/m/37.0';
        public Map<String,String> inputHttpHeaders_x;
        public Map<String,String> outputHttpHeaders_x;
        public String clientCertName_x;
        public String clientCert_x;
        public String clientCertPasswd_x;
        public Integer timeout_x;
        public MetadataService.SessionHeader_element SessionHeader;
        public MetadataService.DebuggingInfo_element DebuggingInfo;
        public MetadataService.DebuggingHeader_element DebuggingHeader;
        public MetadataService.CallOptions_element CallOptions;
        public MetadataService.AllOrNoneHeader_element AllOrNoneHeader;
        private String SessionHeader_hns = 'SessionHeader=http://soap.sforce.com/2006/04/metadata';
        private String DebuggingInfo_hns = 'DebuggingInfo=http://soap.sforce.com/2006/04/metadata';
        private String DebuggingHeader_hns = 'DebuggingHeader=http://soap.sforce.com/2006/04/metadata';
        private String CallOptions_hns = 'CallOptions=http://soap.sforce.com/2006/04/metadata';
        private String AllOrNoneHeader_hns = 'AllOrNoneHeader=http://soap.sforce.com/2006/04/metadata';
        private String[] ns_map_type_info = new String[]{'http://soap.sforce.com/2006/04/metadata', 'MetadataService'};
        
        public MetadataService.FileProperties[] listMetadata(MetadataService.ListMetadataQuery[] queries,Double asOfVersion) {
            MetadataService.listMetadata_element request_x = new MetadataService.listMetadata_element();
            request_x.queries = queries;
            request_x.asOfVersion = asOfVersion;
            MetadataService.listMetadataResponse_element response_x;
            Map<String, MetadataService.listMetadataResponse_element> response_map_x = new Map<String, MetadataService.listMetadataResponse_element>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              '',
              'http://soap.sforce.com/2006/04/metadata',
              'listMetadata',
              'http://soap.sforce.com/2006/04/metadata',
              'listMetadataResponse',
              'MetadataService.listMetadataResponse_element'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.result;
        }
    }
}



Now Just call following Code in Class or Controller

This one for Validation Rules


MetadataService.MetadataPort service = new MetadataService.MetadataPort();
        service.SessionHeader = new MetadataService.SessionHeader_element();
        service.SessionHeader.sessionId = UserInfo.getSessionId();
        
        List<MetadataService.ListMetadataQuery> queries = new List<MetadataService.ListMetadataQuery>();    
        MetadataService.ListMetadataQuery queryLayout = new MetadataService.ListMetadataQuery();
        queryLayout.folder = '';
        queryLayout.type_x = 'ValidationRule';
        queries.add(queryLayout);    
        MetadataService.FileProperties[] fileProperties = service.listMetadata(queries, 25);
          if(fileProperties!=null)
            {
              for(MetadataService.FileProperties fileProperty : fileProperties)
              {
                  System.Debug(fileProperty.fullName); 
              }
            }

Similarly for WorkFlow Rules

queryLayout1.type_x = 'WorkflowRule';


Note : Don't Forget to add remote setting for your org url.


Master Check Box To select all records in Table VisualForce Page.


Provide a master checkbox before all record rows means if user selects that all records will be selected.

- Create VisualForce Component for master check Box.

Component >>


<apex:component >

<script>
function cvCheckAllOrNone(allOrNoneCheckbox) {

    // Find parent table
    var container = allOrNoneCheckbox;
    while (container.tagName != "TABLE") {
        container = container.parentNode;
    }

    // Switch all checkboxes
    var inputs = container.getElementsByTagName("input");
    var checked = allOrNoneCheckbox.checked;
    for (var i = 0; i < inputs.length; i++) { 
        var input = inputs.item(i);
        if (input.type == "checkbox") {
            if (input != allOrNoneCheckbox) {
                input.checked = checked;
            }
        }
    }
}
</script>

<apex:inputCheckbox onclick="cvCheckAllOrNone(this)" title="Toggle All Rows"/>

</apex:component>


You can add a checkbox to the header row of your checkbox column that uses it to check all or none of the checkboxes:

In visualforce page add the component.

<apex:column >
    <apex:facet name="header">
        <c:CheckAllOrNone />
    </apex:facet>
    <apex:inputCheckbox value="YOUrCheckBoxValue"/>
</apex:column>


As this runs at the client side it is nice and quick.

Salesforce auto-create Folder for New Account.



Trigger for Creating Folder On Account Insert.

trigger BoxFolderCreationTrigger on Account (after insert) {
    
    public Set<ID> accids = new Set<ID>();

 for(Account acc : Trigger.New) {
  accIds.add(acc.Id);
 }
 
 if(accIds.size() > 0) {
  BoxFutureClass.createFolder(accIds);
 }
    
}


As Call-out is not allowed in Trigger.Here i have call Future class method for call-out.





Snippet OF Future Class.



global class BoxFutureClass
{
    @future(callout=true)
    public static void createFolder(set<ID> AccIds) {   
        Map<String,String> boxFRUPMap = new Map<String,String>();

        for(box__FRUP__c bF : [SELECT box__Folder_ID__c,box__Record_ID__c FROM box__FRUP__c where box__Object_Name__c = 'Account' limit 9999]) {
            boxFRUPMap.put(bF.box__Record_ID__c,bF.box__Folder_ID__c);
        }
        
        for(Account acc : [Select id,name from account where id IN :AccIds]) 
        {
         
            box.Toolkit boxToolkit ;
       
            if(!boxFRUPMap.containsKey(acc.Id))
            {
                boxToolkit = new box.Toolkit();
               
                String accountFolderId = boxToolkit.createFolderForRecordId(acc.Id,null, true);
             
                boxToolkit.commitChanges();
            }
               
                
          }
    }
 }


box__FRUP__c is custom object for box folder to keep track of folder created by recordId.







Embedding Box Custom Object Records

  1. Log into Salesforce as a Salesforce Admin
  2. For an existing custom object (for example, Custom_Object), create a new custom field in Custom_Object. 
    1. Select Text.
    2. In the Field Label box, type FolderID. In the Length box, type 18 as the maximum length for the text box.
    3. Select the required access level for this object and click Next.
    4. Click Save.
  3. Create a new Visual Force page for Custom_Object, for example, Custom_Object_Box_Section 
  4. Copy the following Visual Force page code and paste it in this new page.
<apex:page standardController="Custom_Object__c" extensions="box.DisplayWidget" showheader="false" sidebar="false" action="{!GetLoginToken}">
 <apex:stylesheet value="{!URLFOR($Resource.box__BoxEmbed, 'BoxEmbed/style.css')}"/> 
<apex:iframe src="/apex/box__BOXSection?id={!Custom_Object__c.Id}&param=Custom_Object_Box_Section" rendered="{!DisplayBoxSection}"/>
<apex:iframe src="{!folderShareLink}" rendered="{!DisplayBoxWidget}" width="100%" height="100%" frameborder="0" scrolling="NO"/>
</apex:page





  • Note: Parameters marked in bold are variables. Change these parameters to match your enterprise setup. For example:
    • Custom_Object__c: Change this variable to the API name of your custom object.
    • Custom_Object__c.Id: Change this variable to the API name of your custom object ID field.
    • Custom_Object_Box_Section: Change this variable to the API name of your Visual Force page.
    1. Add this newly created section to the Custom Object Page Layout. See the Embedding Box in Standard Object Records section for more details.
  • Box For Salesforce

    Installation and Initial Setup


    1. Log into Salesforce as a Salesforce Admin.
    2. Navigate to the package installation link.
    3. At separate points during the installation, you will be asked to:
      • Accept and Approve Third-Party Access for remote settings.
      • Grant access to all users.
      • Click Next or Continue in other screens to complete the installation.
    4. When you complete the installation, the screen will return to the Salesforce Box Connector App (Managed) page.

    5. Click the + tab to view the All Tabs page.
    6. Click Box Settings to view the Connect Salesforce to Box page.
    7. Click the Box API Admin Login button to view the Box API login page.
    8. Enter the Box Service Account credentials.

    9. Specify the name for your Root Folder, e.g. Salesforce-<Org Identifier>.
    10. Specify folder settings for Lead conversions (optional)
    11. Specify your company's subdomain, e.g. if you log into Box at cloud.app.box.com, your subdomain is "cloud".
    12. At this point, you can enable (suggested) or disable Seamless Login and Auto-Collab on the same page, and click Save Settings.
    13. Add the Box functionality to Salesforce objects (e.g. Opportunities, Accounts, etc.)

    Embedding Box in Standard Object Records


    Use the following procedure for all Salesforce standard objects (e.g. Account, Lead, Contact, Opportunity, etc.) requiring a Box Embed addition as part of the page layout.
    1. Access the Salesforce Menu and click Setup (top right corner).
    2. From the left menu, navigate to Build > Customize to find the appropriate object (e.g., Lead, Opportunities, Accounts, or any Custom_Object).
    3. Expand the object's menu and select Page Layouts.
    4. Edit the Page Layout where Box Embed must be added.
    5. From the [Object] Layout > Field menu, drag a new Section into the Page Layout in the desired position. 
    6. Change the Section parameters:
      1. Assign a name for the section (for example, Box Files).
      2. Make the section 1-Column
      3. Click OK
    7. Add the newly-created Visual Force Page to the Page Layout:
      1. From the Lead Layout menu, click Visualforce Pages.
      2. Drag [object_name]BoxSection into your newly created Section.
      3. Click the wrench on the [object_name]BoxSection Visualforce page.
      4. Set the height to 600 pixels (minimum) for best viewing.
      5. Save the page layout.

    How To Create chatter feeds (FeedItem) Or SF File From Attachment?

    Snippet Code for Creating FeedItem.


    - Most of time user easily create feedItem but it seems that file created is not Previewable.

           The only solution to it is add extension to ContentFileName.




    for(Attachment att : [SELECT id,body,parentId FROM ]
    {
     Blob body = att.body;

                String name = (att.name).substringBeforeLast('.');
                String extension = (att.name).substringAfterLast('.');
                
                FeedItem file = new FeedItem();
                file.ParentId =  String.valueof(att.parentid) ;   // ID of parent object of file (Record Id)
                file.ContentData = body;
                file.ContentFileName = name + '.' + extension;
                file.Title = name;
                System.debug('file::' + file);
                
                feedItemList .add(file);
    }

    If(feedItemList.size() > 0)
    {
    insert feedItemList;
    }


    File will get Show up in File related list of Record(Parent id).