How to Create a Task and Populate a Custom Date/Time Field with a TAT.

How to Create a Task and Populate a Custom Date/Time Field with a TAT.

Task: We first create a task based on lead status change. We will be then populating a custom date/time field which can be used as a TAT. The TAT will be adjusted according to the users shift hours and working days.

Prerequisites (if applicable): Connection with User specific scopes.

Instructions:

Step 1: Go to Setup, Workflow Rules. Create New. Choose Leads Module.

Step 2: Set the condition for the lead status to be a particular value.

Step 3: Start writing deluge.

Outcome: After this workflow is enabled, whenever someone changes the lead status, a task will be created with a TAT.


Code:
//Get details Create our task
pDetails = zoho.crm.getRecordById("Leads",id);
// info pDetails;
lName = pDetails.get("Full_Name");
info lName;
owner = pDetails.get("Owner").get("id");
info owner;
//Task Variables
subject = "3rd Follow Up " + lName;
dueDate = today.addDay(1);
//Create Task Map
tMap = Map();
tMap.put("$se_module","Leads");
tMap.put("What_Id",id);
tMap.put("Subject",subject);
tMap.put("Due_Date",dueDate);
tMap.put("Owner",owner);
tMap.put("Status","Not Started");
createResp = zoho.crm.createRecord("Tasks",tMap);
task = zoho.crm.getRelatedRecords("Tasks","Leads",id);
for each  rec in task
{
	ab = rec.getJSON("Subject");
	checkstr1 = ab.contains("3rd Follow Up");
	if(checkstr1 == true)
	{
		ID = rec.getJSON("id");
		taskinfo = zoho.crm.getRecordById("Tasks",ID);
		userid = taskinfo.get("Owner").get("id");
		created = taskinfo.get("Created_Time");
		ctime = taskinfo.get("Created_Time").toTime("yyyy-MM-dd'T'HH:mm:ss");
		if(created.contains("+"))
		{
			timezone = "+" + created.getSuffix("+");
		}
		else
		{
			timezone = "-" + created.getSuffix("-");
		}
		chour = ctime.getHour();
		info chour;
		cmin = ctime.getMinutes();
		info cmin;
		cdate = ctime.getDay();
		resp = invokeurl
		[
			url :"https://www.zohoapis.in/crm/v5/users/" + userid
			type :GET
			connection:"crm"
		];
		shift = resp.get("users").get(0).get("$current_shift").get("id");
		//info shift;
		resp1 = invokeurl
		[
			url :"https://www.zohoapis.in/crm/v5/settings/business_hours/shift_hours/" + shift
			type :GET
			connection:"crm"
		];
		time = resp1.get("shift_hours").get(0).get("daily_timing");
		info time;
		start = time.get(0).subString(0,2).toLong();
		end = time.get(1).subString(0,2).toLong();
		duration = end - start;
		abh = 12 - duration;
		if(chour < start)
		{
			due = ctime.addBusinessDay(1).toString("yyyy-MM-dd " + start + ":00:00").toTime("yyyy-MM-dd HH:mm:ss").addHour(abh).toString("yyyy-MM-dd'T'HH:mm:ss") + timezone;
		}
		else if(chour > end || chour == end && cmin > 0)
		{
			due = ctime.addBusinessday(2).toString("yyyy-MM-dd " + start + ":00:00").toTime("yyyy-MM-dd HH:mm:ss").addHour(abh).toString("yyyy-MM-dd'T'HH:mm:ss") + timezone;
		}
		else if(chour >= start && chour <= end && cmin == 0)
		{
			cdiff = end - ctime.getHour();
			val = 12 - cdiff;
			if(val <= duration)
			{
				due = ctime.addBusinessday(1).toString("yyyy-MM-dd " + start + ":00:00").toTime("yyyy-MM-dd HH:mm:ss").addHour(val).toString("yyyy-MM-dd'T'HH:mm:ss") + timezone;
			}
			else
			{
				due = ctime.addBusinessday(2).toString("yyyy-MM-dd " + start + ":00:00").toTime("yyyy-MM-dd HH:mm:ss").addHour(val - duration).toString("yyyy-MM-dd'T'HH:mm:ss") + timezone;
			}
		}
		else if(chour >= start && chour <= end && cmin > 0)
		{
			cdiff = end - ctime.getHour();
			dmin = cmin / 60;
			val = 12 - (cdiff - dmin);
			info val;
			if(val <= duration)
			{
				due = ctime.addBusinessday(1).toString("yyyy-MM-dd " + start + ":00:00").toTime("yyyy-MM-dd HH:mm:ss").addMinutes(val * 60).toString("yyyy-MM-dd'T'HH:mm:ss") + timezone;
			}
			else
			{
				due = ctime.addBusinessday(2).toString("yyyy-MM-dd " + start + ":00:00").toTime("yyyy-MM-dd HH:mm:ss").addMinutes((val - duration) * 60).toString("yyyy-MM-dd'T'HH:mm:ss") + timezone;
			}
		}
		info due;
		update = zoho.crm.updateRecord("Tasks",ID,{"Due_Date_Time":due});
	}
}