// Capture the state provided by the client
var state = payload.state.toUpperCase(); // Convert to uppercase for standardization

// Calculate delivery time based on the US state
if (['NY', 'NJ', 'CT', 'PA'].includes(state)) {
  payload.deadline = 'Delivery within up to 3 business days'; // Northeast Region
} else if (['CA', 'OR', 'WA', 'NV'].includes(state)) {
  payload.deadline = 'Delivery within up to 4 business days'; // West Coast
} else if (['TX', 'OK', 'LA', 'AR'].includes(state)) {
  payload.deadline = 'Delivery within up to 5 business days'; // South Central
} else if (['IL', 'IN', 'OH', 'MI'].includes(state)) {
  payload.deadline = 'Delivery within up to 4 business days'; // Midwest
} else if (['FL', 'GA', 'SC', 'NC'].includes(state)) {
  payload.deadline = 'Delivery within up to 5 business days'; // Southeast
} else {
  payload.deadline = 'State not recognized. Please check the abbreviation and try again.'; // Error handling
}

In this example, we use the Execute JavaScript block to calculate the delivery time based on the customer’s region.

  1. First, within the automation structure, we ask the customer to send the abbreviation corresponding to the state they live in. For example: Send “SP” for those who live in São Paulo.
  1. Next, we save the customer’s response in the variable payload.state.
  1. In Execute JavaScript, we use the variable payload.state to run the code that checks the delivery time based on the region provided by the client.
  1. Finally, the result of the code will be saved in the variable payload.deadline, which is sent to the client in the last message indicating the delivery time according to the informed region.

Note: In the example, we also implement error handling in the code, so if the abbreviation provided by the client does not correspond to any Brazilian state, an error message will be displayed requesting them to try again.

// Capture the state provided by the client
var state = payload.state.toUpperCase(); // Convert to uppercase for standardization

// Calculate delivery time based on the US state
if (['NY', 'NJ', 'CT', 'PA'].includes(state)) {
  payload.deadline = 'Delivery within up to 3 business days'; // Northeast Region
} else if (['CA', 'OR', 'WA', 'NV'].includes(state)) {
  payload.deadline = 'Delivery within up to 4 business days'; // West Coast
} else if (['TX', 'OK', 'LA', 'AR'].includes(state)) {
  payload.deadline = 'Delivery within up to 5 business days'; // South Central
} else if (['IL', 'IN', 'OH', 'MI'].includes(state)) {
  payload.deadline = 'Delivery within up to 4 business days'; // Midwest
} else if (['FL', 'GA', 'SC', 'NC'].includes(state)) {
  payload.deadline = 'Delivery within up to 5 business days'; // Southeast
} else {
  payload.deadline = 'State not recognized. Please check the abbreviation and try again.'; // Error handling
}