I am trying to update the access of SharingRule using the Metadata API from APEX. I have created a MetadataService class using https://github.com/financialforcedev/apex-mdapi/blob/master/apex-mdapi/src/classes/MetadataService.cls. I also created a SharingRuleUpdater class with method to update the SharingRule.
public without sharing class SharingRuleUpdater {
public void updateSharingRule(String sharingRuleName, String accessLevel) {
// Create the metadata API client
MetadataService.MetadataPort service = new MetadataService.MetadataPort();
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();
MetadataService.SharingCriteriaRule scr = new MetadataService.SharingCriteriaRule();
scr.fullName = sharingRuleName;
scr.accessLevel = accessLevel;
// Update the sharing rule
List<MetadataService.SaveResult> result = service.updateMetadata(new MetadataService.Metadata[] {scr});
System.debug('result-->'+result);
}
}
After that I have created Shift_Sharing_Rule SharingCriteriaRule on Shift__c custom object. I am executing below code to update the access of this SharingRule
SharingRuleUpdater sru = new SharingRuleUpdater();
sru.updateSharingRule('Shift__c.Shift_Sharing_Rule','Edit');
Executing above code is giving below error
message=An unexpected error occurred. Please include this ErrorId if you contact support:...., message_type_info=(message, http://soap.sforce.com/2006/04/metadata, null, 1, 1, false), statusCode=UNKNOWN_EXCEPTION
message=An unexpected error occurred. Please include this ErrorId if you contact support: 1488460070-151344 (985879857), message_type_info=(message, http://soap.sforce.com/2006/04/metadata, null, 1, 1, false), statusCode=UNKNOWN_EXCEPTION, statusCode_type_info=(statusCode, http://soap.sforce.com/2006/04/metadata, null, 1, 1, false)]), errors_type_info=(errors, http://soap.sforce.com/2006/04/metadata, null, 0, -1, false), field_order_type_info=(errors, fullName, success), fullName=Shift.Shift_Sharing_Rule,
985879857
corresponds tojava.lang.NullPointerException on id null