function apiAbort(obj) { writeOutput(serializeJSON(obj)); abort; } function readJsonBody() { raw = toString(getHttpRequestData().content); if (isNull(raw) || len(trim(raw)) EQ 0) return {}; try { parsed = deserializeJSON(raw); } catch(any e) { apiAbort({ OK=false, ERROR="bad_json", MESSAGE="Invalid JSON body" }); } if (!isStruct(parsed)) return {}; return parsed; } function normStr(v) { if (isNull(v)) return ""; return trim(toString(v)); } data = readJsonBody(); if (!structKeyExists(request, "BusinessID") || !isNumeric(request.BusinessID) || request.BusinessID LTE 0) { apiAbort({ OK=false, ERROR="no_business_selected" }); } if (!structKeyExists(data, "ServicePointName") || len(normStr(data.ServicePointName)) EQ 0) { apiAbort({ OK=false, ERROR="missing_name", MESSAGE="ServicePointName is required" }); } servicePointId = 0; if (structKeyExists(data, "ServicePointID") && isNumeric(data.ServicePointID) && int(data.ServicePointID) GT 0) { servicePointId = int(data.ServicePointID); } spName = normStr(data.ServicePointName); spCode = structKeyExists(data, "ServicePointCode") ? normStr(data.ServicePointCode) : ""; spTypeID = structKeyExists(data, "ServicePointTypeID") && isNumeric(data.ServicePointTypeID) ? int(data.ServicePointTypeID) : 1; sortOrder = structKeyExists(data, "ServicePointSortOrder") && isNumeric(data.ServicePointSortOrder) ? int(data.ServicePointSortOrder) : 0; isActive = 1; if (structKeyExists(data, "IsActive")) { if (isBoolean(data.IsActive)) isActive = (data.IsActive ? 1 : 0); else if (isNumeric(data.IsActive)) isActive = int(data.IsActive); else if (isSimpleValue(data.IsActive)) isActive = (lcase(trim(toString(data.IsActive))) EQ "true" ? 1 : 0); } UPDATE ServicePoints SET ServicePointName = , ServicePointCode = , ServicePointTypeID = , ServicePointIsActive = , ServicePointSortOrder = WHERE ServicePointID = AND ServicePointBusinessID = SELECT ServicePointID FROM ServicePoints WHERE ServicePointID = AND ServicePointBusinessID = LIMIT 1 #serializeJSON({ OK=false, ERROR="not_found" })# INSERT INTO ServicePoints ( ServicePointBusinessID, ServicePointName, ServicePointCode, ServicePointTypeID, ServicePointIsActive, ServicePointSortOrder ) VALUES ( , , , , , ) SELECT LAST_INSERT_ID() AS ServicePointID SELECT ServicePointID, ServicePointBusinessID, ServicePointName, ServicePointCode, ServicePointTypeID, ServicePointIsActive, ServicePointSortOrder FROM ServicePoints WHERE ServicePointID = AND ServicePointBusinessID = LIMIT 1 #serializeJSON({ OK=true, ERROR="", SERVICEPOINT=servicePoint })# #serializeJSON({ OK=false, ERROR="server_error", MESSAGE=cfcatch.message, DETAIL=cfcatch.detail })#