0

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
4
  • 1
    Can you include the complete error message? Is there a GACK ID?
    – Swetha
    Feb 2 at 17:31
  • Please find this error message 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,
    – yash patel
    Feb 3 at 5:41
  • From Salesforce internal logs,I can see that the stacktrace 985879857 corresponds to java.lang.NullPointerException on id null
    – Swetha
    Feb 4 at 17:30
  • Thanks Swetha. I will check regarding that. Also, will you be able to help for finding which id is missing?
    – yash patel
    Feb 6 at 5:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.