Skip to main content

Could you please provide guidance on how to use the API (Update metadata instance on folder:PUT) using Apex?



For Create metadata instance on folder(POST), I was able to achieve it as described above. However, I’m unsure how to set the Body for Update metadata instance on folder.



Sample Code for Use Create metadata instance on folder(POST)


box.Toolkit toolkit = new box.Toolkit();


String folderId = project.ProjectFolderId__c;



Map<String, String> metadata = new Map<String, String>{


‘masterdefkeyword1’ => project.Keyword1__c,


‘masterdefkeyword2’ => project.Keyword2__c,


‘masterdefkeyword3’ => project.Keyword3__c,


‘projectsupplementkeyword1’ => project.SupplementKeyword1__c,


‘projectsupplementkeyword2’ => project.SupplementKeyword2__c,


‘projectsupplementkeyword3’ => project.SupplementKeyword3__c


};



String scope = ‘enterprise’;


String tempName = ‘projectmetadatatemplate’;


// Box endpoint(Create metadata instance on folder)


String endpoint = ‘https://api.box.com/2.0/folders/’ + folderId + ‘/metadata/’ + scope + ‘/’ + temp;



HttpRequest request = new HttpRequest();


request.setMethod(‘POST’);


request.setEndpoint(endpoint);


request.setBody(JSON.serialize(metadata));


request.setHeader(‘content-type’, ‘application/json’);



HttpResponse response = toolkit.sendRequest(request);

Hi @peifengyuan!



There are actually some brand new Dev Toolkit apex methods and corresponding Flow invocable actions that were released recently, but our documentation has not been updated yet.



New Dev Toolkit Metadata CRUD Methods:





  • box.FolderMetadata createBoxMetadataByFolderId(String folderId, String scope, String template_key, List<box.KeyValuePair> keyValuePairs)


  • box.FolderMetadata getBoxMetadataByFolderId(String folderId, String scope, String template_key)


  • box.FolderMetadata updateBoxMetadataByFolderId(String folderId, String scope, String template_key, List<box.FolderMetadataUpdate> mdUpdates)


  • Boolean deleteBoxMetadataByFolderId(String folderId, String scope, String template_key)




box.KeyValuePair Class



global class KeyValuePair {

@AuraEnabled

global String key;

@AuraEnabled

global String value;

global KeyValuePair() {



}

}



box.FolderMetadataUpdate Class



global class FolderMetadataUpdate {

@AuraEnabled

global String op;

@AuraEnabled

global String path;

@AuraEnabled

global String value;

global FolderMetadataUpdate() {



}



Here is an example of creating a metadata instance on a folder:



box.Toolkit toolkit = new box.Toolkit();

String folderId = '216484150697';

String accountId = '001Dm000005NJVXIA4';

List<box.KeyValuePair> mdKeyValues = new List<box.KeyValuePair>();



Account acct = [SELECT Id, Name FROM Account WHERE Id = :accountId LIMIT 1];

if(acct != null) {

box.KeyValuePair accountId = new box.KeyValuePair();

accountId.key = 'accountId';

accountId.value = acct.Id;

mdKeyValues.add(accountId);



box.KeyValuePair accountName = new box.KeyValuePair();

accountName.key = 'accountName';

accountName.value = acct.Name;

mdKeyValues.add(accountName);



box.FolderMetadata folderMeta = toolkit.createBoxMetadataByFolderId(folderId, 'enterprise', 'account', mdKeyValues);

System.debug('Added folder metadata: ' + folderMeta);

}



And here is an example of updating the metadata instance on a folder:



box.Toolkit toolkit = new box.Toolkit();

String folderId = '216484150697';

String accountId = '001Dm000005NJVXIA4';

List<box.FolderMetadataUpdate> mdUpdates = new List<box.FolderMetadataUpdate>();





Account acct = [SELECT Id, Name FROM Account WHERE Id = :accountId LIMIT 1];

if(acct != null) {

box.FolderMetadataUpdate mdUpdate = new box.FolderMetadataUpdate();

mdUpdate.op = 'replace';

mdUpdate.path = '/accountName';

mdUpdate.value = 'This is my new Account Name';



mdUpdates.add(mdUpdate);

box.FolderMetadata folderMeta = toolkit.updateBoxMetadataByFolderId(folderId, 'enterprise', 'account', mdKeyValues);

System.debug('Updated folder metadata: ' + folderMeta);

}



I hope this helps! Let us know if you have any additional questions!


Hi @kadams !



Thank you for providing the sample code.


I have reviewed the release notes and implemented it using the API since it was not mentioned in the documentation.


I appreciate your assistance and will refer to the code as a reference.



Thank you again and Hope you have a greate day!!



Thank you & Best Regards,


peifeng yuan


Reply