Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WorkflowRuleBatch finds all workflow rule names, then uses Andrew Fawcett's MetadataService to query for additional details
global class WorkflowRuleBatchable implements Database.Batchable<String>, Database.AllowsCallouts, Database.Stateful
{
global final List<MetadataService.WorkflowRule> wfrs;
private final String sessionId;
global WorkflowRuleBatchable(String sessionId)
{
this.sessionId = sessionId;
wfrs = new List<MetadataService.WorkflowRule>();
}
global List<String> start(Database.BatchableContext bc)
{
String salesforceHost = System.Url.getSalesforceBaseURL().toExternalForm();
String urlHost = salesforceHost + '/services/data/v30.0/tooling/query/?q=Select+Id+,Name,+TableEnumOrId+from+WorkflowRule';
HTTPResponse res = metadataQuery(urlHost);
//system.debug('workflow res = ' + res);
//system.debug('workflow res.getBody() = ' + res.getBody());
List<String> workflowRuleNames = new List<String>();
WorkflowRule wfrs = (WorkflowRule) System.JSON.deserialize(res.getBody(), WorkflowRule.class);
for(Integer j=0; j < wfrs.records.size(); j++ )
{
String fullName = wfrs.records[j].TableEnumOrId + '.' + wfrs.records[j].Name;
workflowRuleNames.add(fullName);
}
return workflowRuleNames;
}
global void execute(Database.BatchableContext bc, List<String> scope)
{
MetadataService.MetadataPort service = new MetadataService.MetadataPort();
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = this.sessionId;
// Read Workflow Alert
MetadataService.WorkflowRule wfr = (MetadataService.WorkflowRule) service.readMetadata('WorkflowRule', scope).getRecords()[0];
wfrs.add(wfr);
}
global void finish(Database.BatchableContext bc)
{
system.debug('wfrs = ' + wfrs);
}
HTTPResponse metadataQuery(String urlHostVariable)
{
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint(urlHostVariable);
req.setHeader('Content-type', 'application/json');
req.setHeader('Authorization', 'Bearer ' + this.sessionId);
Http http = new Http();
return http.send(req);
}
class WorkflowRule
{
public Integer size;
public Integer totalSize;
public Boolean done;
public Object queryLocator;
public String entityTypeName;
public List<Records> records;
public WorkflowRule(Integer size, Integer totalSize, Boolean done, Object queryLocator, String entityTypeName, List<Records> records)
{
this.size = size;
this.totalSize = totalSize;
this.done = done;
this.queryLocator = queryLocator;
this.entityTypeName = entityTypeName;
this.records = records;
}
}
class Attributes
{
public String type;
public String url;
}
class Records
{
public Attributes attributes;
public String Id;
public String Name;
public String TableEnumOrId;
public Records(Attributes attributes, String Id, String Name, String TableEnumOrId)
{
this.attributes = attributes;
this.Id = Id;
This.Name = Name;
this.TableEnumOrId = TableEnumOrId;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.