0

I'm creating a field dynamically via apex (or tooling API haven't decided yet) and my problem has been this field is created with no access level to anyone.

To overcome this I wanted to add the field to a permission set after it's created.

I found the Financialforce examples and wanted to use something like this:

@future(callout = true)

public static void testPerm()
{
    /* Find the Quick Toggler Permission Set! */
    MetadataService.MetadataPort service = createService();
    String[] permissionSetNames = new String[] {'VA_Quick_Visibility_Toggler_Permissions'};
    List<MetadataService.PermissionSet> perm = 
        (List<MetadataService.PermissionSet>) service.readMetadata('PermissionSet', permissionSetNames).getRecords();
    MetadataService.PermissionSet quickPerm = perm.get(0);
    
    MetadataService.PermissionSetFieldPermissions fieldSec = new MetadataService.PermissionSetFieldPermissions();
    fieldSec.field='User.TestFieldA__c';
    fieldSec.editable=true;
    fieldSec.readable=true;
    quickPerm.fieldPermissions.add(fieldSec);
    List<MetadataService.SaveResult> results =
        service.updateMetadata(
            new MetadataService.Metadata[] { quickPerm });
    handleSaveResults(results[0]);
    
}

And that code needs these two methods.

public static MetadataService.MetadataPort createService()
{
    MetadataService.MetadataPort service = new MetadataService.MetadataPort();
    service.SessionHeader = new MetadataService.SessionHeader_element();
    service.SessionHeader.sessionId = UserInfo.getSessionId();
    return service;
}

/**
 * Example helper method to interpret a SaveResult, throws an exception if errors are found
 **/
public static void handleSaveResults(MetadataService.SaveResult saveResult)
{
    // Nothing to see?
    if(saveResult==null || saveResult.success)
        return;
    // Construct error message and throw an exception
    if(saveResult.errors!=null)
    {
        List<String> messages = new List<String>();
        messages.add(
            (saveResult.errors.size()==1 ? 'Error ' : 'Errors ') +
                'occured processing component ' + saveResult.fullName + '.');
        for(MetadataService.Error error : saveResult.errors)
            messages.add(
                error.message + ' (' + error.statusCode + ').' +
                ( error.fields!=null && error.fields.size()>0 ?
                    ' Fields ' + String.join(error.fields, ',') + '.' : '' ) );
        if(messages.size()>0)
            throw new MetadataServiceExamplesException(String.join(messages, ' '));
    }
    if(!saveResult.success)
        throw new MetadataServiceExamplesException('Request failed with no specified error.');
}

I'm getting unknown errors though. Anyone know where I'm going wrong?

Update: getting the API name made me realize that the permission set already has field permissions and the code was attempting to replace the permissions with only the 1 field permission, so fixed that. But I'm still getting Read Timeouts!

0

You need to pass API name of the permission set here and it work fine. API name can be found in the permission set page itself.

enter image description here

    String[] permissionSetNames = new String[] {'Survey_Force_Guest'};

I have tried this many times and it works without any errors.

1
  • I tried with the API name but I still get a timeout error everytime Dec 11 '20 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.