7

I am trying to alter Organization-Wide Defaults (OWD) via apex using https://github.com/financialforcedev/apex-mdapi (Apex Wrapper Salesforce Metadata API) tool.

I can create custom object with attribute 'sharingModel' set to 'Read' and it works fine. But when I try to update 'sharingModel' set to 'ReadWrite', it does not update to 'ReadWrite'. However, there is no error thrown and all other fields are updated except 'sharingModel'. Any help would be helpful.

Thanks

Adding code: Code used from MetadataServiceExamples class.

This is when creating an object with sharingModel as "ReadWrite"

public static void createObject() { 
    MetadataService.MetadataPort service = createService();
    MetadataService.CustomObject customObject = new MetadataService.CustomObject();
    customObject.fullName = 'Test__c';
    customObject.label = 'Test'; 
    customObject.pluralLabel = 'Tests';
    customObject.nameField = new MetadataService.CustomField(); 
    customObject.nameField.type_x = 'Text'; 
    customObject.nameField.label = 'Test Record'; 
    customObject.deploymentStatus = 'Deployed'; 
    customObject.sharingModel = 'ReadWrite'; 
    List<MetadataService.SaveResult> results = service.createMetadata( new MetadataService.Metadata[] { customObject });
    handleSaveResults(results[0]); 
}

This is when updating the same object with sharingModel as "Private"

public static void upsertObject() { 
    MetadataService.MetadataPort service = createService(); 
    MetadataService.CustomObject customObject = new MetadataService.CustomObject(); 
    customObject.fullName = 'Test__c'; 
    customObject.label = 'Test'; 
    customObject.pluralLabel = 'Tests Upsert'; 
    customObject.nameField = new MetadataService.CustomField(); 
    customObject.nameField.type_x = 'Text'; 
    customObject.nameField.label = 'Test Record Upsert'; 
    customObject.deploymentStatus = 'Deployed'; 
    customObject.sharingModel = 'Private'; 
    List<MetadataService.UpsertResult> results = service.upsertMetadata( new MetadataService.Metadata[] { customObject }); 
    handleUpsertResults(results[0]);
}

1 Answer 1

5
+25

Update

See Known Issue: sharingModel does not update through Metadata API CRUD-based calls (W-4865521 )

Summary
sharingModel does not update through Metadata API CRUD-based calls

API call finishes with success = true but the sharingModel for the object is not updated

One workaround that has so far come out of support case 18656360 is that you can change the sharingModel using a deploy() API call with a package zip file.


One possibility, check the API version of the Metadata API you are using.

From the documentation for CustomObject.sharingModel: (my emphasis)

Using API version 29.0 and earlier, this field is read-only and you can't set this field through Metadata API; you must use the Salesforce user interface. Using API version 30.0 and later, you can set this field for internal users using the API and the Salesforce user interface.


I suspect there may be more going on here than the API allows for. Despite the documentations claims that it will work. I tried to manually change the sharing model for a custom object under the Sharing Settings UI from Public Read/Write to Public Read Only under Default Internal Access.

After attempting that I got the message:

One or more sharing operations has been initiated. See below for additional details. Certain operations may not be available.

And under Test Sharing Rules:

An organization-wide default update on Tests Update RW has been initiated by Daniel Ballinger on 3/19/2018 4:33 PM. You can't submit any changes for Tests Update RW prior to the completion of the operation. Daniel Ballinger will receive an email when the operation finishes.

An immediate API call to readMetadata still showed the old sharingModel. Yet after a few minutes (presumably for the batch job to run) it had changed to Read.

Set Your Organization-Wide Sharing Defaults covers why the change is immediate when increasing the default access but queued when decreasing. Despite that, no change seems to occur based on the Metadata API calls.


Sample direct SOAP Metadata API POST to /services/Soap/m/42.0/00D280000000001

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <env:Header>
        <SessionHeader xmlns="http://soap.sforce.com/2006/04/metadata">
            <sessionId>00D2800000000001!ARwAQAs_Not_A_Real_Session_ID_EddSwwi2qBSNS.NHZ_wWOrf46RRoNemG2d1eovwiSH8uPp</sessionId>
        </SessionHeader>
    </env:Header>
    <env:Body>
        <updateMetadata xmlns="http://soap.sforce.com/2006/04/metadata">
            <metadata xsi:type="CustomObject">
                <fullName>Test__c</fullName>
                <deploymentStatus>Deployed</deploymentStatus>
                <label>Test</label>
                <nameField xsi:type="CustomField">
                    <label>Test Record</label>
                    <type>Text</type>
                </nameField>
                <pluralLabel>Tests Update RW</pluralLabel>
                <!-- Changing this has no affect -->
                <sharingModel>ReadWrite</sharingModel>
            </metadata>
        </updateMetadata>
    </env:Body>
</env:Envelope>
13
  • I am using API version 39.0.
    – redparrot
    Commented Mar 15, 2018 at 10:46
  • 4
    @rakeshml OK. Can you share the apex code as part of the question? It will make reproducing it easier. Commented Mar 15, 2018 at 19:42
  • Yes! I tried the same and it does not change. Seems that Salesforce is running a background job when we set it to private. I will check after few minutes to verify the same!
    – Ayub
    Commented Mar 20, 2018 at 9:14
  • 1
    Plan B after twitter didn't work. Implication was that it doesn't work async. I'm not sure that was the underlying issue. Now trying with support case 18656360 Commented Mar 25, 2018 at 23:02
  • 1
    @RogerioHonorio The known issue is at success.salesforce.com/…. It is still showing as In Review. Commented Oct 16, 2019 at 23:19

You must log in to answer this question.

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