Apex Codes
public class ListViewUtility {
public static void CreateListViewForPicklistValue(String picklistValue, String sObjectName, List<String> columns) {
MetadataService.MetadataPort service = new MetadataService.MetadataPort();
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();
String sObjectNamewithSuffix = sObjectName + '__c';
MetadataService.ListView listView = new MetadataService.ListView();
listView.fullName = sObjectNamewithSuffix+'.' +picklistValue +sObjectName+'s' ;
listView.label = picklistValue + ' ' + sObjectName;
listView.filterScope = 'Everything';
listView.columns = columns;
MetadataService.ListViewFilter filter = new MetadataService.ListViewFilter();
filter.field = 'Name';
filter.operation = 'equals';
filter.value = picklistValue;
listView.filters = new List<MetadataService.ListViewFilter>{filter};
MetadataService.SharedTo sharingSet =New MetadataService.SharedTo();
sharingSet.role = new List<String> {'CEO'};
listView.sharedTo = sharingSet;
List<MetadataService.SaveResult> results =
service.createMetadata(
new MetadataService.Metadata[] { listView });
}
}