+ Start a Discussion

Issue with creating Sharing rule using Metadata API version 33.0

Has anyone tried creating Sharing rule using Metadata API version 33.0?

I want to create an owner based sharing rule using metadata and below is the code snippet method which is calling MetadataService33 class created by Metadata WDSL.
public static void createSharingRule(String objectName, String ruleName, String ruleLabel, String groupName) {
		MetadataService33.MetadataPort stub = createService33();
		MetadataService33.SharingRules sharingRule = (MetadataService33.SharingRules) stub.readMetadata('SharingRules', new String[] { objectName }).getRecords()[0];
		//MetadataService33.SharingRules sharingRule = new MetadataService33.SharingRules();
		//sharingRule.fullName = objectName  ;
		MetadataService33.SharingCriteriaRule criteriaRuleOfObject = new MetadataService33.SharingCriteriaRule();
		MetadataService33.FilterItem criteriaItemOfRule = new MetadataService33.FilterItem();
		criteriaItemOfRule.operation = 'equals' ;
		criteriaRuleOfObject.criteriaItems = new List<MetadataService33.FilterItem> {criteriaItemOfRule} ;
		criteriaRuleOfObject.booleanFilter = 'False' ;
		MetadataService33.SharedTo sharedToGrpC = new  MetadataService33.SharedTo() ;
		criteriaRuleOfObject.sharedTo = sharedToGrpC ;
		MetadataService33.SharingTerritoryRule territoryRuleOfObject = new MetadataService33.SharingTerritoryRule();
		MetadataService33.SharedTo sharedFrmGrpT = new  MetadataService33.SharedTo() ;
		MetadataService33.SharedTo sharedToGrpT = new  MetadataService33.SharedTo() ;
		territoryRuleOfObject.sharedFrom = sharedFrmGrpT ;
		territoryRuleOfObject.sharedTo = sharedToGrpT ;
		MetadataService33.SharingOwnerRule ownerRuleOfObject = new MetadataService33.SharingOwnerRule();
		ownerRuleOfObject.fullName = ruleName ;
		ownerRuleOfObject.accessLevel = 'Edit' ;
		ownerRuleOfObject.label = ruleLabel ;
		MetadataService33.SharedTo sharedFrmGrp = new  MetadataService33.SharedTo() ; 
		sharedFrmGrp.group_x = new String[] {groupName} ; 
		MetadataService33.SharedTo sharedToGrp = new  MetadataService33.SharedTo() ;
		sharedToGrp.group_x = new String[] {groupName} ;
		ownerRuleOfObject.sharedFrom = sharedFrmGrp ;
		ownerRuleOfObject.sharedTo = sharedToGrp ;
		if(sharingRule.sharingOwnerRules != null)
			sharingRule.sharingOwnerRules.add(ownerRuleOfObject) ;
			sharingRule.sharingOwnerRules =  new List<MetadataService33.SharingOwnerRule> {ownerRuleOfObject} ;
		if(sharingRule.sharingCriteriaRules == null)
			sharingRule.sharingCriteriaRules =  new List<MetadataService33.SharingCriteriaRule> {criteriaRuleOfObject} ;
		if(sharingRule.sharingTerritoryRules == null)
			sharingRule.sharingTerritoryRules = new List<MetadataService33.SharingTerritoryRule> {territoryRuleOfObject} ;
		MetadataService33.SaveResult[] resp = stub.updateMetadata(new MetadataService33.Metadata[] { sharingRule });
		// MetadataService33.SaveResult[] resp = stub.createMetadata(new MetadataService33.Metadata[] { sharingRule });
		// MetadataService33.UpsertResult[] resp = stub.upsertMetadata(new MetadataService33.Metadata[] { sharingRule });
		system.debug('====> ' + resp);

When I call above method for a standard object like Contact, I get sucess message from salesforce though no rules is created in actual and when I call it for a custom object, I get unknow exception from saleforce. ReadMetadata call works perfectly fine and give proper result but updating sharing rule metadata gives me error.

Here is the code snippet how I call the method:
/* @1st parameter: Object API name
    @2nd parameter: Sharing rule name
    @3rd parameter: Sharing rule Label
    @4th parameter: Existing public group name
MetadataWebserviceCall.createSharingRule('Contact', 'Test_Owner_Sharing_Rule2', 'Test Owner Sharing Rule2', 'AD_Sharing_Group');

Thanks in advance.

ShashankShashank (Salesforce Developers) 
As per this article: https://help.salesforce.com/apex/HTViewSolution?urlname=Spring-15-Metadata-API-changes&language=en_US

"Retrieving and deploying of manual sharing and sharing rules by type (criteria, owner, and territory) is not supported."
Yes you are right, deploying sharing rule by individual type is not possible but what I was more interested in at that time was:
"All sharing rules for an organization can now be retrieved and deployed for all objects with wildcard support."

And with my above code, I was trying to do something like deploying the complete sharing rule with only proper SharingOwnerRule and dummy in other two sharing rule type but seems it is not possible.
Hi, Did you manage to find the solution for this?