# How to implement a custom logic using function node

In this tutorial we will writing a custom logic using the function node to iterate and check whether customer is eligible for a voucher card and create a response list accordingly.

  1. TBD Following is the payload used for this use case.

    {
        "CustomerInfo":[
            {
              "customer_name" : "Dan",
              "age" : 35,
              "working_status" : "yes",
              "existing_card" : "no",
              "applied_on" : "29/01/2019"
            },
            {
              "customer_name" : "Dan",
              "age" : 27,
              "working_status" : "yes",
              "existing_card" : "no",
              "applied_on" : "29/01/2019"
            },
            {
              "customer_name" : "Dan",
              "age" : 35,
              "working_status" : "yes",
              "existing_card" : "no",
              "applied_on" : "29/01/2019"
            },
            {
              "customer_name" : "Dan",
              "age" : 35,
              "working_status" : "yes",
              "existing_card" : "no",
              "applied_on" : "29/01/2019"
            }
        ]
    }
    
    
  2. Drag and drop the function node from the transformation section in the pallette to the canvas. Double click the function node to open the editor .

  3. Place the below javascript code on the editor code editor section. We are going to implement the code based on the following rules for finding the customers eligible for the voucher card.

    • Customer working status is yes
    • Customer has no existing card
    • Customer age > 30
     var temp = [];
     console.log(msg.payload.CustomerInfo.length);
     for (i = 0; i < msg.payload.CustomerInfo.length; i++) {
     console.log("age :: "+msg.payload.CustomerInfo[i].age + "card::" + msg.payload.CustomerInfo[i].existing_card + 
                 "status::"+msg.payload.CustomerInfo[i].working_status); 
     if(msg.payload.CustomerInfo[i].age > 30 && msg.payload.CustomerInfo[i].existing_card == 'no' &&
         msg.payload.CustomerInfo[i].working_status == 'yes'){
       temp.push(msg.payload.CustomerInfo[i]);
     }
     }
     msg.payload = temp;
     return msg;
    
    
  4. Wire the TBD to the function node.

  5. Drop the debug node to the end of the flow and name it as Completed.

  6. Now click the TBD to test the flow.

Observation

The following output is printed on the debug panel. The output is the list of customers that are satisfied based on the logic implemented on the function node.

[
  {
   "customer_name":"Dan",
   "age":35,
   "working_status":"yes",
   "existing_card":"no",
   "applied_on":"29/01/2019"
  },
  {
   "customer_name":"Sheldon",
   "age":32,
   "working_status":"yes",
   "existing_card":"no",
   "applied_on":"19/07/2019"
  }
]