Skip to main content
Martyn_Bowis
Active Member
February 20, 2024
Solved

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

  • February 20, 2024
  • 4 replies
  • 872 views

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?

This topic has been closed for replies.
Best answer by tank666

Let’s start in order:

figma.com

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.

4 replies

tank666
February 20, 2024

Instead of the collectionId string, pass a VariableCollection object.

figma.com
UX/UI designer. Figma expert. Creator and developer of plugins and widgets for Figma and FigJam. Check out my resources in Figma Community: figma.com/@tank
Martyn_Bowis
Active Member
February 20, 2024

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

tank666
tank666Answer
February 21, 2024

Let’s start in order:

figma.com

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.

UX/UI designer. Figma expert. Creator and developer of plugins and widgets for Figma and FigJam. Check out my resources in Figma Community: figma.com/@tank
Ienaka_Atsushi
New Member
March 15, 2024

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