How to use CatalogItemVariable and how create field in catalog item

Danil
Tera Contributor

Docs hasn't keep useful information, need to create fields using CatalogItemVariable API. 

 

 

var catItemVar = new sn_sc.CatalogItemVariable();
var variableDetails = {
    catalog_item: 'catalog item's sys_id', 
    type: '1', // Variable type (e.g., '1' for Single Line Text)
    name: 'testfield',
    question_text: 'What is your new variable?',
    order: '100', // Order of the variable
};

var newVariableSysId = catItemVar.create(variableDetails);

 

 

Mentioned code - doesn't work 

1 ACCEPTED SOLUTION

Nicholas_Gann
Mega Guru

I don't know where you got the above example from as it's not showing when I look at the Washington DC version. You appear to be using the API incorrectly. create() only accepts a boolean parameter as per the documentation. You need to use the setAttributes() function to set the structure of the new Variable and then use create() to create it.

 

I've not used this API before but see below:

 

 

 

var catItemVar = new sn_sc.CatalogItemVariable();
var variableDetails = {
    cat_item: '7b5f699283ab0610e51a1530ceaad33e', 
    type: '1', // Variable type (e.g., '1' for Single Line Text)
    name: 'testfield',
    question_text: 'What is your new variable?',
    order: '100', // Order of the variable
};

catItemVar.setAttributes(variableDetails);
var newVariableSysId = catItemVar.create(true);

 

 

 

I agree the documentation isn't the best as I wasn't sure that doing setAttributes() followed by create() would work but it seems to. I assume update() might be able to update existing variables but it's not clear how it matches unless it just requires the sys_id attribute to be added. It could do with examples.

 

View solution in original post

1 REPLY 1

Nicholas_Gann
Mega Guru

I don't know where you got the above example from as it's not showing when I look at the Washington DC version. You appear to be using the API incorrectly. create() only accepts a boolean parameter as per the documentation. You need to use the setAttributes() function to set the structure of the new Variable and then use create() to create it.

 

I've not used this API before but see below:

 

 

 

var catItemVar = new sn_sc.CatalogItemVariable();
var variableDetails = {
    cat_item: '7b5f699283ab0610e51a1530ceaad33e', 
    type: '1', // Variable type (e.g., '1' for Single Line Text)
    name: 'testfield',
    question_text: 'What is your new variable?',
    order: '100', // Order of the variable
};

catItemVar.setAttributes(variableDetails);
var newVariableSysId = catItemVar.create(true);

 

 

 

I agree the documentation isn't the best as I wasn't sure that doing setAttributes() followed by create() would work but it seems to. I assume update() might be able to update existing variables but it's not clear how it matches unless it just requires the sys_id attribute to be added. It could do with examples.