For keeping a control over module stage or status, you can use the below deluge script. The below deluge script defines validation control over deals module stages:
// Function to validate allowed stage transitions in the Deals module
map validation_rule.LeadStatustValidation1(String crmAPIRequest)
{
// Convert the incoming API request to a map and extract the 'record' map
entityMap = crmAPIRequest.toMap().get("record");
// Get the Deal record ID from the request (if available)
id = ifnull(entityMap.get('id'),"");
// Initialize the response map
response = Map();
if(id != "")
{
// Fetch the existing Deal record using the ID
getdetails = zoho.crm.getRecordById("Deals",id);
// Extract current and new stage values
currentStage = getdetails.get("Stage");
newStage = entityMap.get("Stage");
// Log for debugging purposes
info "Current Stage: " + currentStage;
info "New Stage: " + newStage;
// Define the valid stage order (used to determine forward/backward movement)
stageOrder = list();
stageOrder.add("Qualified Deal");
stageOrder.add("Quotation Created");
stageOrder.add("Negotiation");
stageOrder.add("Quotation Accepted");
stageOrder.add("Sales Order Created");
stageOrder.add("Deal Won");
stageOrder.add("Deal Lost");
// Get the index of the current and new stage from the stage order list
currentIndex = stageOrder.indexOf(currentStage);
newIndex = stageOrder.indexOf(newStage);
// Define allowed backward movement only from Negotiation to Quotation Created
allowBackwardFromNegotiation = list();
allowBackwardFromNegotiation.add("Quotation Created");
if(currentIndex > -1 && newIndex > -1)
{
if(newStage == currentStage)
{
// No change in stage
response.put("status","success");
response.put("message","No change in stage.");
}
else if(currentIndex < newIndex)
{
// Forward movement is allowed
response.put("status","success");
response.put("message","Valid forward movement.");
}
else
{
// Backward movement logic
if(currentStage == "Negotiation" && allowBackwardFromNegotiation.contains(newStage))
{
response.put("status","success");
response.put("message","Backward movement allowed from Negotiation to Quotation Created.");
}
else
{
response.put("status","error");
response.put("message","Backward movement is not allowed from " + currentStage + " to " + newStage + ".");
}
}
}
else
{
// Invalid stage(s) in the request
response.put("status","error");
response.put("message","Invalid stage(s) detected.");
}
}
else
{
// No ID provided in the request
response.put("status","error");
response.put("message","Record ID is missing.");
}
// Return the final response
return response;
}