function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
hemant ranahemant rana 

query to retrieve count of all validation rule in salesforce org

Any how can we get the count of all validation rules and workflows  in salesforce org.
I tried---
private static MetadataService.MetadataPort createService()
    {
        MetadataService.MetadataPort service = new MetadataService.MetadataPort();
        service.SessionHeader = new MetadataService.SessionHeader_element();
        service.SessionHeader.sessionId = UserInfo.getSessionId();
        return service;    
    }
MetadataService.MetadataPort service = createService();
        List<MetadataService.ListMetadataQuery> queries = new List<MetadataService.ListMetadataQuery>();     
        MetadataService.ListMetadataQuery queryWorkflow = new MetadataService.ListMetadataQuery();
        queryWorkflow.type_x = 'Workflow';
        queriesWorkflow.add(queryWorkflow);

But no luck plz help its urgent
Best Answer chosen by hemant rana
Hargobind_SinghHargobind_Singh
This would give you the list of 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); 
      }
    }

For workflows, you would need to list the files using the above code, and then use retreive call to get list of workflow rules. 

You seem to be using Metadata API Apex Wrapper, see if you have a VF page titled "metadataretrieve" in the wrapper, if you do, try the VF page, select workflow, and click "List", then select any object that has workflow, and click "Retreive". Once you see the result, you can change and use the code in controller class.

Link to wrapper : https://github.com/financialforcedev/apex-mdapi

All Answers

Hargobind_SinghHargobind_Singh
This would give you the list of 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); 
      }
    }

For workflows, you would need to list the files using the above code, and then use retreive call to get list of workflow rules. 

You seem to be using Metadata API Apex Wrapper, see if you have a VF page titled "metadataretrieve" in the wrapper, if you do, try the VF page, select workflow, and click "List", then select any object that has workflow, and click "Retreive". Once you see the result, you can change and use the code in controller class.

Link to wrapper : https://github.com/financialforcedev/apex-mdapi

This was selected as the best answer
hemant ranahemant rana
Thnaks it worked
abhishekdeyabhishekdey
I tried the same to fetch the Dashboards of an org:

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 = 'Ezone_dashboard/TestDashboard';
            //queryLayout.folder = 'dashboards/Ezone_dashboard';
            queryLayout.folder = 'Ezone_dashboard';
            queryLayout.type_x = 'Dashboard';
            queries.add(queryLayout);   
            MetadataService.FileProperties[] fileProperties = service.listMetadata(queries, 31);
         
              if(fileProperties!=null)
                {
                  for(MetadataService.FileProperties fileProperty : fileProperties)
                  {
                      System.debug('***************'+fileProperty.fullName);
                  }
                }

However I am getting null response. I guess I am not able to set the folder attribute properly .. Can any body help ?
Bala SFDCBala SFDC
Hello,

I am trying to execute the same script in developer console but getting error Invalid Type : MetadataService.MetadataPort. 

Could you please suggest. It is bit urgent for me to get list of all validation rules and workflow rule in my org.

Thanks in Advance,
Bala
vaishali sharmavaishali sharma
Hi Hargobind, Can you please give us any reference from where we can understand the basics of how to fetch metadata components like validation rule, page layout etc on apex class. Which can explain in brief? Thanks in advance.
Apttus old 2 NewApttus old 2 New
Can you all plz help how to differentiate active Validation Rules and inactive VRules using the above method? Your help will  be much appreciated.