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});
	}
}


    • Related Articles

    • How to Populate City and State Based on Zip Code

      How to Populate City and State Based on Zip Code Task: We will be populating fields in Leads Module in Zoho CRM. The fields in this case are “City” and “State” based on the value of we put in Zip Code field. Prerequisites (if applicable): Client ...
    • How to set a field as read-only using client script

      Task: Writing a client script for a different inbuild function based on different seniors/events. Prerequisites (if applicable): You should have a basic understanding of how to create and manage client scripts in your application. Instructions: Step ...
    • How to Create an RFQ process in Zoho CRM.

      Task: A process to send RFQ (Request for Quote) to the vendor which is then filled by him to generate a RFQ record in Zoho CRM. (e.g., “By following the instructions, you’ll be able to achieve below steps smoothly” “This article will help you set up ...
    • How to auto capture date in any module of Zoho CRM (T+15)

      User wants the date should capture as T+15 in any module of Zoho CRM. Deluge Code: Cdate = zoho.currentdate.addDay(15).toString("YYYY-MM-dd"); Zoho Link: https://www.zoho.com/deluge/help/functions/date-time.html
    • How to Create, Update, Read and Delete records in Zoho CRM.

      If you want to Create, Update, Read and Delete records in Zoho CRM in any module, please use the following code for the same: Code: var commonname = value.id; var acc = ZDK.Apps.CRM.Accounts.fetchById(commonname); var phone = acc.MobileNo; var email ...