How to pass a collection node to set a variable mode?

Documentation says:

setExplicitVariableModeForCollection(collectionId: string, modeId: string): void

How to resolve following error?

“Calling setExplicitVariableModeForCollection with a collection id is deprecated. Please pass the collection node instead.”

How do you pass a collection node?

Instead of the collectionId string, pass a VariableCollection object.

I cannot get getVariableCollectionById to work for a team library collection.

I have tried to get the variable collection, for a team library, using the collectionId as follows:

  // get available library collections
  var collections = await figma.teamLibrary.getAvailableLibraryVariableCollectionsAsync(); 
  console.log(collections);

  // get first library collection
  var collection = collections[0];
  console.log(collection);

  // get collection id
  var collectionId = collection.key;
  console.log(collectionId);

  // get same collection using collectionId
  var collectionById = figma.variables.getVariableCollectionById(collectionId);
  console.log(collectionById);

Why does the console output show that getting the collection using the collection id returns null? Note that the collection was returned successfully, the collectionId obtained successfully via the collection key, but null is returned when trying to use that collectionId to get the variable collection by id with Figma.variables.getVariableCollectionById(collectionId).

So, how to do the following?

  • Get team library variable collection using Figma.variables.getVariableCollectionById or other means
  • Get modes of that team library variable collection
  • use setExplicitVariableModeForCollection

Let’s start in order:

This is intended to be used in conjunction with getVariablesInLibraryCollectionAsync

idkey

The argument to this method must be collection.id, not collection.key.

So, you need to follow the following logic:

  1. Get a LibraryVariableCollection;
  2. Get a LibraryVariable;
  3. Import this LibraryVariable by key;
  4. Get a variableCollectionId in imported Variable;
  5. Get a VariableCollection by id.

After completing the fifth step, you will have a VariableCollection object, which you must use in the setExplicitVariableModeForCollection method.

Collection modes will be available in the following path: VariableCollection.modes.

1 Like

This clarification for the logic is extremely helpful.
Just wanted to say thank you! :slightly_smiling_face: