Dear Community User! We have started the migration process.
This community is now in READ ONLY mode.
Read more: Important information on the platform change.

cancel
Showing results for 
Search instead for 
Did you mean: 
SOLVED

How can I create and save a WebIQ recipe using JS?

How can I create and save a WebIQ recipe using JS?

HmiGuide
Community Moderator
Community Moderator

I know the WebIQ recipe widgets, and I know that you can use them to create new recipes. In my project, I need to create a WebIQ recipe using a JS script.

What do I have to do to create a new WebIQ recipe?
I can successfully create a recipe object, but it is not saved as a recipe. I can create a new recipe from an existing one by cloning. But when no recipe exists, I need to create one from scratch.

 

// For testing I just use the data from an existing recipe.

let id = 10 // only constant for testing, id does exists
let templateId = loadedRcp.templateId
let versionId = loadedRcp.versionId
let versionNum = loadedRcp.versionNum
let name = "newRcp" // only fixed for testing, name does not exist
let createdBy = loadedRcp.createdBy
let createdTimestamp = loadedRcp.createdTimestamp
let modifiedBy = loadedRcp.modifiedBy
let modifiedTimestamp = loadedRcp.modifiedTimestamp
let comment = loadedRcp.comment
let metadata = loadedRcp.metadata
let values = loadedRcp.values
let manager = loadedRcp.manager

let newRcp = new shmi.visuals.core.Recipe(id, templateId, versionId, versionNum, name, createdBy, createdTimestamp, modifiedBy, modifiedTimestamp, comment, metadata, values, manager)

 

 

4 REPLIES 4

webiq-sk
Frequent Contributor

 

Here is a simple UI Action that allows you to create a recipe through JavaScript code - you can of course also use the code inside a LocalScript. Please note the parameters defined when creating this UI Action, otherwise it won't work:

/**
 * Custom UI-Action 'create-recipe'.
 *
 * Description:
 * [Add description here]
 */
(function() {
    var actions = shmi.pkg("visuals.session.userActions"); //get reference to userActions object

    /**
     * UI-Action 'create-recipe' implementation
     *
     * @params {any[]} parameters  configured ui-action parameters
     * ---- Initial parameters, needs to be updated manually when changed ----
     * @param {number} parameters[0]  Recipe Template ID
     * @param {string} parameters[1]  Recipe Name Prefix
     *
     */
    actions["create-recipe"] = function(parameters) {
        // Place your code here
        const templateId = parseInt(parameters[0]),
            recipeName = parameters[1] + Math.random(),
            rm = shmi.requires("visuals.session.RecipeManager");

        rm.getTemplate(templateId, function(response, err) {
            if (err) {
                shmi.notify("Could not fetch recipe template: " + err.message, "${V_ERROR}");
            } else {
                console.log('Creating recipe...');
                console.log(response);
                response.createRecipe(recipeName, {}, function(newRecipe, createErr) {
                    if (createErr) {
                        shmi.notify("Could not create recipe: " + createErr.message, "${V_ERROR}");
                    } else {
                        console.log("Created recipe!");
                        console.log(newRecipe);
                        shmi.notify(`Recipe created: '${newRecipe.name}.' with ID #${newRecipe.id}`, "${V_ERROR}");
                    }
                });
            }
        });
    };
}());

 

HmiGuide
Community Moderator
Community Moderator

Thanks @webiq-sk I was blind, I overlooked the function.

David08
Long-established Member

Hello webiq-sk,

I'm trying your Javascript code but I need the name of the recipe to be variable for a string item and I can't get it to work. I am changing the input parameters[1] to an item but when I run the UI-Action it creates the recipe with the name of the variable, not the value. For example if the String variable is called RecipeName and its value is Job1, the recipe is created with the name RecipeName.
Could you tell me how to make the script write the value of the variable and not the name as the recipe name.
Thanks

HmiGuide
Community Moderator
Community Moderator

When you define an item as input of an UI-Action, it aways passes the name of the item, not the value of the item. With the name of the item you can get it's value and properties (like Min./Max. label ....). For more info see:

WebIQ ItemManager and article itemManager-Methods-and-documentation-request 

Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist