Workflow function

Options

I am trying to create a workflow function and I am having issues. I am not a developer, so I need help. I finally was able to get beyond the errors but now I am getting errors when I save/deploy. Please help!

Here is the code:

const codeengine = require('codeengine');

function lookforunlocked () {

  const api= '/api/social/v1/smart-alerts/disable?resourceId=${cardId}&resourceType=CARD ';

  var newdisable = "true";

  return codeengine.sendRequest("PUT", api, {"disable":newdisable}).catch(console.error);

}

module.exports.myFunction = lookforunlocked;

Best Answers

  • Zoolander
    Zoolander Contributor
    Answer ✓
    Options

    Kathy, it looks like your function is looking for a parameter called cardId (in your second line, ${cardId} is javascript to insert a variable in to a string, but your function isn't asking for any arguments.

    As a non-developer also, I've had to figure this out. Let me try to explain.

    Any values that you want to pass to the function as parameters, variables or arguments (devs use the term arguments) need to be included in the parenthesis after the function. So I think the first thing I notice is that you need to, at a minimum, add cardId in the parenthesis and have an input parameter (of the correct data type) that is a cardId. I would guess that is a NUMBER.

    Second, you have 'single quotes' around your string on the second line (where you create the const API) and it should be surrounded by ` backticks `.

    Your function is called "lookForUnlocked" - in looking at the end point are you trying to enable/disable insights for non-card owners? If so, the way you have the function written is hard coded to DISABLE them, not way to enable them.

    I may propose the following changes to the function:

    1 - add cardId as an input parameter, and make it a number
    2 - change the single quotes to backticks"
    3 - eliminate the newdisable variable as it's already harded coded in the API call
    4 - your put appears to be incorrect, it should be a POST

    If what you are after is disabling Insights on a card, the following code works (I changed the function to be named "disableCardInsights" as opposed to "lookForUnlocked", but you can call it whatever you want:

    ←————————————CODE———————————→
    const codeengine = require('codeengine');

    function disableCardInsights(cardId) {
    const api = /api/social/v1/smart-alerts/disable?resourceId=${cardId}&resourceType=CARD;
    const body = ``;
    return codeengine.sendRequest("POST", api,body).catch(console.error);
    }

    module.exports.disableCardInsights = disableCardInsights;

    ←———————————-END CODE——————————>

    Screenshot:

    The last thing I will say is you should never deploy codeEngine packages/functions until you run them successfully in codeEngine. That is when you know your code is working. You can run it by hitting the large play button next to the function in the drop down, or by hitting the small green play button in "the gutter" (as my engineers call it) which should up directly adjacent to the code line numbers.

    If that's what you're after, hopefully my quick and dirty breakdown helps make sense. I had to write a lot of these before I had any level of proficiency. I will also request our team make this function in a global package. My goal is for nobody to have to write a custom function to talk to an internal Domo service.

    Hope that's helpful.

  • Kathy_Zyn
    Kathy_Zyn Member
    Answer ✓
    Options

    Thanks, that seemed to work.

Answers

  • Zoolander
    Zoolander Contributor
    Answer ✓
    Options

    Kathy, it looks like your function is looking for a parameter called cardId (in your second line, ${cardId} is javascript to insert a variable in to a string, but your function isn't asking for any arguments.

    As a non-developer also, I've had to figure this out. Let me try to explain.

    Any values that you want to pass to the function as parameters, variables or arguments (devs use the term arguments) need to be included in the parenthesis after the function. So I think the first thing I notice is that you need to, at a minimum, add cardId in the parenthesis and have an input parameter (of the correct data type) that is a cardId. I would guess that is a NUMBER.

    Second, you have 'single quotes' around your string on the second line (where you create the const API) and it should be surrounded by ` backticks `.

    Your function is called "lookForUnlocked" - in looking at the end point are you trying to enable/disable insights for non-card owners? If so, the way you have the function written is hard coded to DISABLE them, not way to enable them.

    I may propose the following changes to the function:

    1 - add cardId as an input parameter, and make it a number
    2 - change the single quotes to backticks"
    3 - eliminate the newdisable variable as it's already harded coded in the API call
    4 - your put appears to be incorrect, it should be a POST

    If what you are after is disabling Insights on a card, the following code works (I changed the function to be named "disableCardInsights" as opposed to "lookForUnlocked", but you can call it whatever you want:

    ←————————————CODE———————————→
    const codeengine = require('codeengine');

    function disableCardInsights(cardId) {
    const api = /api/social/v1/smart-alerts/disable?resourceId=${cardId}&resourceType=CARD;
    const body = ``;
    return codeengine.sendRequest("POST", api,body).catch(console.error);
    }

    module.exports.disableCardInsights = disableCardInsights;

    ←———————————-END CODE——————————>

    Screenshot:

    The last thing I will say is you should never deploy codeEngine packages/functions until you run them successfully in codeEngine. That is when you know your code is working. You can run it by hitting the large play button next to the function in the drop down, or by hitting the small green play button in "the gutter" (as my engineers call it) which should up directly adjacent to the code line numbers.

    If that's what you're after, hopefully my quick and dirty breakdown helps make sense. I had to write a lot of these before I had any level of proficiency. I will also request our team make this function in a global package. My goal is for nobody to have to write a custom function to talk to an internal Domo service.

    Hope that's helpful.

  • Kathy_Zyn
    Options

    Thanks Dan. Please advise. I am still having issues:

    When I just copied and pasted the code. Here is the parsing error:

    When I added the backticks, I am getting this:

  • Zoolander
    Zoolander Contributor
    Options

    Kathy, you fixed the backticks, which is awesome. As I mentioned in the email, the community thinks the backticks are a formatting thing and hides them. #annoying

    I think the reason you're having an issue if you don't have a description. Please put in a description. The requirement for a description is something we are going to remove, but I would suspect that is your issue.

  • Kathy_Zyn
    Options

    Dan,

    The description is in the image. That is all I am getting, and I can't save the function. I copied your code verbatim. So please let me know why I am still having an issue.

    First image - your code exactly

    Second image - with backticks

    Both are giving me errors.

  • Zoolander
    Zoolander Contributor
    edited January 18
    Options

    Kathy, I'm sorry about the confusion but in the screenshot it literally says "No Description Provided". Under the function name, there is a description field. Put something in the description field.

  • Kathy_Zyn
    Kathy_Zyn Member
    Answer ✓
    Options

    Thanks, that seemed to work.