2

Has anyone tried creating Sharing rule using Spring'15 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) ;
    else
        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);
    handleSaveResults33(resp[0]);
}

When I call above method for a standard object like Contact, I get sucess message from salesforce though no rule 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.

1 Answer 1

0

Unfortunately, owner based sharing rules are gone as of API Version 33.0. Here are the docs.

Quote here:

This component is removed as of API version 33.0 and is available in earlier versions only.

However, it looks like you can use an alternative: SharingOwnerRule.

SharingOwnerRule Defines a ownership-based sharing rule. It extends SharingBaseRule and inherits all its fields. Available in API version 33.0 and later.

Docs here.

1
  • Take a look at this: help.salesforce.com/apex/…... It says that individual sharing rule type deploying is not possible but you can deploy complete sharing rule and that is what I was trying with above code sample.
    – Aditi Jain
    Commented Jun 4, 2015 at 7:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .