Send zoho sign template

PHOTO EMBED

Tue Jul 29 2025 12:06:26 GMT+0000 (Coordinated Universal Time)

Saved by @usman13

void automation.SEND_NEWLEAD_AGREEMENT_ON_RENEWAL(Int leaseID)
{
try 
{
	///////////////////////-get renewal lease id------///////////
	Expiring_leasesDetails = zoho.crm.getRelatedRecords("Related_List_Name_1","Leases",leaseID);
	info Expiring_leasesDetails;
	for each  leaseDetail in Expiring_leasesDetails
	{
		lease_id = leaseDetail.get("id");
	}
	info lease_id;
	//////////////////////------get renewal lease info-----////////////
	lease_detail = zoho.crm.getRecordById("Leases",lease_id);
	// 	info lease_detail;
	opp_id = lease_detail.get("Opportunity").get("id");
	// 	info opp_id;
	oppData = zoho.crm.getRecordById("Deals",opp_id);
	info oppData;
	siteid = oppData.get("Site").get("id");
	info "siteid: " + siteid;
	//////////---------
	siteinfo = zoho.crm.getRecordById("Accounts",siteid);
	initial_lead_inspection_result = siteinfo.get("Initial_Lead_Inspection_Result");
	info initial_lead_inspection_result;
	///////////////////////////////////////////////////////////////////
	oppName = oppData.get("Deal_Name");
	info "opp name: " + oppName;
	siteName = oppData.get("Site").get("name");
	info "siteName: " + siteName;
	if(oppData.get("Property_Manager") != null)
	{
		propertyManager = oppData.get("Property_Manager").get("name");
		pm_id = oppData.get("Property_Manager").get("id");
	}
	info "pm:" + propertyManager;
	///////////////////////////////////////////////////////////////////////////////
	petPolicyCharges = oppData.get("Pet_Policy_Charges_USD");
	waterMonthlyCharges = oppData.get("Monthly_Water_Charges_USD");
	info waterMonthlyCharges;
	info petPolicyCharges;
	if(waterMonthlyCharges == null || waterMonthlyCharges == 0)
	{
		waterMonthlyCharges = "Water utility (Monthly Charges) will be charged on actual consumption.";
	}
	else
	{
		waterMonthlyCharges = "Constant of $ " + waterMonthlyCharges + " will be charged for water utility per month residing in the property.";
	}
	if(petPolicyCharges == null || petPolicyCharges == 0 || petPolicyCharges == 200)
	{
		petPolicyCharges = "200";
	}
	info "Test Only";
	/////////////////////////property manager Email///////////////////////
	NewAccessTokenRequest = invokeurl
	[
		url :"https://accounts.zoho.com/oauth/v2/token?refresh_token=1000.0d787bd93f09618b2dbd194ce87a3ac4.c64ff75b9cb389b49ebf42844e9673f3&client_id=1000.6S03ETCF3SEM8YHEQ9BUGXDK6O7Q1H&client_secret=418fb1c600b10085070ca461e6a5e2b8c4a0632133&redirect_uri=https://clevelandbricks.com/&grant_type=refresh_token"
		type :POST
	];
	NewAccessToken = NewAccessTokenRequest.get("access_token");
	//---------------------------Authtoken---------------------------------------
	Authtoken = Map();
	Authtoken.put("Authorization","Zoho-oauthtoken " + NewAccessToken + "");
	Org_Data = invokeurl
	[
		url :"https://www.zohoapis.com/crm/v2/users/" + pm_id
		type :GET
		headers:Authtoken
	];
	info "pm_id" + pm_id;
	info Org_Data;
	propertyManagerEmail = Org_Data.get("users").get(0).get("email");
	info "Property Manager Email: " + propertyManagerEmail;
	////////	///------------------GET INFO FROM NEW LEASE--------------////////
	leaseTerm_months = lease_detail.get("Least_Rental_Term_Months");
	leaseTerm_days = lease_detail.get("Lease_Term_Number_of_Days");
	monthlyRent = lease_detail.get("Monthly_Rent");
	paymentTerms = lease_detail.get("Payment_Terms");
	leaseStartDate = lease_detail.get("Lease_Start_Date").toString("MM/dd/yyyy");
	leaseEndDate = lease_detail.get("Lease_End_Date").toString("MM/dd/yyyy");
	////////////////////////////////////GET INFO FROM OPP///////////////////////////////////////
	securityDeposit = 0;
	tenantName = ifnull(oppData.get("Contact_Name").get("name"),"");
	tenantId = ifnull(oppData.get("Contact_Name").get("id"),"");
	tenantEmail = zoho.crm.getRecordById("Contacts",tenantId).get("Email");
	info "Primary tenant Email: " + tenantEmail;
	//------------------------------------------------------------------------
	coTenant1Data = oppData.get("Sub_Contact_1");
	coTenant2Data = oppData.get("Sub_Contact_2");
	coTenant3Data = oppData.get("Sub_Contact_3");
	coTenant4Data = oppData.get("Sub_Contact_4");
	totalTenant = 1;
	coTenants = {1,2,3,4};
	for each  data in coTenants
	{
		count = oppData.get("Sub_Contact_" + data);
		if(count != null)
		{
			totalTenant = totalTenant + 1;
		}
	}
	info "Total Tenant/s = " + totalTenant;
	/////////-------------------------------lead+lease-------------------------------------
	if(initial_lead_inspection_result = "Fail")
	{
		info "initial_lead_inspection_result=failed";
		if(totalTenant == 1)
		{
			templateResp = zoho.sign.getTemplateById(53924000002918479);
			info "fail 1 tenant";
			//old was 
		}
		else if(totalTenant == 2)
		{
			templateResp = zoho.sign.getTemplateById(53924000002918253);
			info "fail 2 tenants";
		}
		else if(totalTenant == 3)
		{
			templateResp = zoho.sign.getTemplateById(53924000002918031);
			info "fail 3 tenants";
		}
		else if(totalTenant == 4)
		{
			templateResp = zoho.sign.getTemplateById(53924000002916523);
			info "fail 4 tenants";
		}
	}
	else
	{
		info "pass or blank";
		if(totalTenant == 1)
		{
			templateResp = zoho.sign.getTemplateById(53924000002918659);
			info "pass 1 tenant";
			//old was 
		}
		else if(totalTenant == 2)
		{
			templateResp = zoho.sign.getTemplateById(53924000002918839);
			info "pass 2 tenants";
		}
		else if(totalTenant == 3)
		{
			templateResp = zoho.sign.getTemplateById(53924000002919039);
			info "pass 3 tenants";
		}
		else if(totalTenant == 4)
		{
			templateResp = zoho.sign.getTemplateById(53924000002919261);
			info "pass 4 tenants";
		}
	}
	/////////////////////////////////////////////////////////////
	templateRec = templateResp.get("templates");
	actionRec1 = templateRec.get("actions").get(0);
	actionRole1 = actionRec1.get("role");
	actionId1 = actionRec1.get("action_id");
	actionType1 = actionRec1.get("action_type");
	eachActionMap1 = Map();
	eachActionMap1.put("recipient_name",tenantName);
	eachActionMap1.put("recipient_email",tenantEmail);
	eachActionMap1.put("action_type",actionType1);
	eachActionMap1.put("action_id",actionId1);
	eachActionMap1.put("role",actionRole1);
	eachActionMap1.put("verify_recipient","false");
	//////////////////////////////////////property manager/////////////////////////////////////
	if(totalTenant == 1)
	{
		actionPM = templateRec.get("actions").get(1);
	}
	else if(totalTenant == 2)
	{
		actionPM = templateRec.get("actions").get(2);
	}
	else if(totalTenant == 3)
	{
		actionPM = templateRec.get("actions").get(3);
	}
	else if(totalTenant == 4)
	{
		actionPM = templateRec.get("actions").get(4);
	}
	// 	else if(totalTenant == 5)
	// 	{
	// 		actionPM = templateRec.get("actions").get(5);
	// 	}
	PMRole = actionPM.get("role");
	PMId = actionPM.get("action_id");
	PMType = actionPM.get("action_type");
	PMMap1 = Map();
	PMMap1.put("recipient_name",propertyManager);
	PMMap1.put("recipient_email",propertyManagerEmail);
	PMMap1.put("action_type",PMType);
	PMMap1.put("action_id",PMId);
	PMMap1.put("role",PMRole);
	PMMap1.put("verify_recipient","false");
	//////////////////////////////--------------------------///////////////////
	actionMap0 = Map();
	fieldTextData = Map();
	field_boolean_data = Map();
	//-------------------------------------------------------------------------
	//--------------------------------------------------------------------------
	if(coTenant1Data != null)
	{
		coTenant2 = coTenant1Data.get("name");
		coTenant1Id = coTenant1Data.get("id");
		coTenantEmail2 = zoho.crm.getRecordById("Contacts",coTenant1Id).get("Email");
		actionRec2 = templateRec.get("actions").get(1);
		actionRole2 = actionRec2.get("role");
		actionId2 = actionRec2.get("action_id");
		actionType2 = actionRec2.get("action_type");
		eachActionMap2 = Map();
		eachActionMap2.put("recipient_name",coTenant2);
		eachActionMap2.put("recipient_email",coTenantEmail2);
		eachActionMap2.put("action_type",actionType2);
		eachActionMap2.put("action_id",actionId2);
		eachActionMap2.put("role",actionRole2);
		eachActionMap2.put("verify_recipient","false");
		fieldTextData.put("Tenant-2 2b",coTenant2);
		// 		fieldTextData.put("co tenant 01",coTenant2);
		fieldTextData.put("co tenant 001",coTenant2);
	}
	if(coTenant2Data != null)
	{
		coTenant3 = coTenant2Data.get("name");
		coTenant2Id = coTenant2Data.get("id");
		coTenantEmail3 = zoho.crm.getRecordById("Contacts",coTenant2Id).get("Email");
		actionRec3 = templateRec.get("actions").get(2);
		actionRole3 = actionRec3.get("role");
		actionId3 = actionRec3.get("action_id");
		actionType3 = actionRec3.get("action_type");
		eachActionMap3 = Map();
		eachActionMap3.put("recipient_name",coTenant3);
		eachActionMap3.put("recipient_email",coTenantEmail3);
		eachActionMap3.put("action_type",actionType3);
		eachActionMap3.put("action_id",actionId3);
		eachActionMap3.put("role",actionRole3);
		eachActionMap3.put("verify_recipient","false");
		fieldTextData.put("co tenant 2",coTenant3);
		// 		fieldTextData.put("co tenant 02",coTenant3);
		fieldTextData.put("co tenant 002",coTenant3);
	}
	if(coTenant3Data != null)
	{
		coTenant4 = coTenant3Data.get("name");
		coTenant3Id = coTenant3Data.get("id");
		coTenantEmail4 = zoho.crm.getRecordById("Contacts",coTenant3Id).get("Email");
		actionRec4 = templateRec.get("actions").get(3);
		actionRole4 = actionRec4.get("role");
		actionId4 = actionRec4.get("action_id");
		actionType4 = actionRec4.get("action_type");
		eachActionMap4 = Map();
		eachActionMap4.put("recipient_name",coTenant4);
		eachActionMap4.put("recipient_email",coTenantEmail4);
		eachActionMap4.put("action_type",actionType4);
		eachActionMap4.put("action_id",actionId4);
		eachActionMap4.put("role",actionRole4);
		eachActionMap4.put("verify_recipient","false");
		fieldTextData.put("co tenant 3",coTenant4);
		// 		fieldTextData.put("co tenant 03",coTenant4);
		fieldTextData.put("co tenant 003",coTenant4);
	}
	if(coTenant4Data != null)
	{
		coTenant5 = coTenant4Data.get("name");
		coTenant4Id = coTenant4Data.get("id");
		coTenantEmail5 = zoho.crm.getRecordById("Contacts",coTenant4Id).get("Email");
		actionRec5 = templateRec.get("actions").get(4);
		actionRole5 = actionRec5.get("role");
		actionId5 = actionRec5.get("action_id");
		actionType5 = actionRec5.get("action_type");
		eachActionMap5 = Map();
		eachActionMap5.put("recipient_name",coTenant5);
		eachActionMap5.put("recipient_email",coTenantEmail5);
		eachActionMap5.put("action_type",actionType5);
		eachActionMap5.put("action_id",actionId5);
		eachActionMap5.put("role",actionRole5);
		eachActionMap5.put("verify_recipient","false");
		fieldTextData.put("co tenant 4",coTenant5);
		fieldTextData.put("co tenant 04",coTenant5);
		fieldTextData.put("co tenant 004",coTenant5);
	}
	//---------------------------------------------------------------------------
	fieldTextData.put("site name",siteName);
	fieldTextData.put("opportunity name",oppName);
	fieldTextData.put("lease term",leaseTerm_months + " Months");
	if(leaseTerm_days != null && leaseTerm_days != 0)
	{
		fieldTextData.put("lease term",leaseTerm_months + " Month/s and  " + leaseTerm_days + " Days");
	}
	fieldTextData.put("monthly rent","$ " + monthlyRent);
	fieldTextData.put("security deposite","$ " + securityDeposit);
	fieldTextData.put("lease start date",leaseStartDate);
	fieldTextData.put("lease end date",leaseEndDate);
	fieldTextData.put("tenant name",tenantName);
	// 	fieldTextData.put("tenant name 1",tenantName);
	fieldTextData.put("site name1",siteName);
	fieldTextData.put("lease start date 1",leaseStartDate);
	fieldTextData.put("lease end date 1",leaseEndDate);
	fieldTextData.put("lease start date 2",leaseStartDate);
	fieldTextData.put("mo",leaseTerm_months + " Months");
	/////////////////
	if(leaseTerm_days != null && leaseTerm_days != 0)
	{
		fieldTextData.put("mo",leaseTerm_months + " Month/s and  " + leaseTerm_days + " Days");
	}
	fieldTextData.put("rent per month","$ " + monthlyRent);
	// 	fieldTextData.put("tenant name 1",tenantName);
	fieldTextData.put("tenant name 2",tenantName);
	fieldTextData.put("property manager",propertyManager);
	fieldTextData.put("water_monthly",waterMonthlyCharges);
	fieldTextData.put("pet_charges",petPolicyCharges);
	///////////////////////////////////////
	///////////-------new changes phase 2 lead----/////////////
	fieldTextData.put("By_PMname",propertyManager);
	////////////////////------section 4 ---site info------//////////////
	Site_type = siteinfo.get("Account_Type");
	if(Site_type == "Single Unit")
	{
		field_boolean_data.put("Single_Family",true);
	}
	else if(Site_type == "Multi Unit")
	{
		field_boolean_data.put("Multi_Family",true);
	}
	else if(Site_type == "Apartment")
	{
		field_boolean_data.put("Apartment",true);
	}
	///////////////////////////////////------add new INFO IN SECTION 13 ---------------////////
	// 	siteinfo = zoho.crm.getRecordById("Accounts",siteid);
	u_vendors = siteinfo.get("Utilities_Vendors");
	// info siteinfo;
	otherapps = "";
	all_apps = siteinfo.get("Appliances");
	info all_apps;
	for each  machine in all_apps
	{
		info machine;
		if(machine == "Dishwasher")
		{
			field_boolean_data.put("L-Dishwasher",true);
		}
		if(machine == "Stove/Oven")
		{
			field_boolean_data.put("L-StoveOven",true);
		}
		if(machine == "Washing Machine")
		{
			field_boolean_data.put("L-WashingMachine",true);
		}
		if(machine == "Dryer")
		{
			field_boolean_data.put("L-dryer",true);
		}
		if(machine == "Microwave")
		{
			// 			field_boolean_data.put("L-other",true);
			otherapps = otherapps + machine;
			fieldTextData.put("13a other",otherapps);
		}
		if(machine == "Refrigerator")
		{
			field_boolean_data.put("L-Refrigerator",true);
		}
		if(machine == "Air Conditioner")
		{
			// 			field_boolean_data.put("L-other",true);
			otherapps = otherapps + machine;
			fieldTextData.put("13a other",otherapps);
		}
		// 		info fieldTextData;
	}
	/////////////////////---------------ADD INFO IN SECTION 12 -NEW-------------//////////
	if(u_vendors != null || u_vendors != "")
	{
		for each  utility in u_vendors
		{
			Type_of_Service = utility.get("Type_of_Service");
			Payment_Responsibility = utility.get("Payment_Responsibility");
			if(Type_of_Service == "Electricity")
			{
				fieldTextData.put("12.Electric",Payment_Responsibility);
			}
			if(Type_of_Service == "Gas")
			{
				fieldTextData.put("12.Gas",Payment_Responsibility);
			}
			if(Type_of_Service == "Water")
			{
				fieldTextData.put("12.Water",Payment_Responsibility);
			}
			if(Type_of_Service == "Sewer")
			{
				fieldTextData.put("12.Sewer",Payment_Responsibility);
			}
			if(Type_of_Service == "Trash")
			{
				fieldTextData.put("12.Trash",Payment_Responsibility);
			}
			if(Type_of_Service == "Landscaping")
			{
				fieldTextData.put("12.Landscaping",Payment_Responsibility);
			}
			if(Type_of_Service == "Snow Plow")
			{
				fieldTextData.put("12.Snowplow",Payment_Responsibility);
			}
			if(Type_of_Service == "HOA")
			{
				fieldTextData.put("12.HOA",Payment_Responsibility);
			}
		}
	}
	///////////////////////////////
	info waterMonthlyCharges;
	info petPolicyCharges;
	temp_map = Map();
	temp_map.put("field_text_data",fieldTextData);
	temp_map.put("field_boolean_data",field_boolean_data);
	actionMap0.put("field_data",temp_map);
	// 	actionMap0.put("field_data",{"field_boolean_data":field_boolean_data});
	fieldList = List();
	if(totalTenant == 1)
	{
		fieldList.add(eachActionMap1);
		fieldList.add(PMMap1);
	}
	else if(totalTenant == 2)
	{
		fieldList.add(eachActionMap1);
		fieldList.add(eachActionMap2);
		fieldList.add(PMMap1);
	}
	else if(totalTenant == 3)
	{
		fieldList.add(eachActionMap1);
		fieldList.add(eachActionMap2);
		fieldList.add(eachActionMap3);
		fieldList.add(PMMap1);
	}
	else if(totalTenant == 4)
	{
		fieldList.add(eachActionMap1);
		fieldList.add(eachActionMap2);
		fieldList.add(eachActionMap3);
		fieldList.add(eachActionMap4);
		fieldList.add(PMMap1);
	}
	else if(totalTenant == 5)
	{
		fieldList.add(eachActionMap1);
		fieldList.add(eachActionMap2);
		fieldList.add(eachActionMap3);
		fieldList.add(eachActionMap4);
		fieldList.add(eachActionMap5);
		fieldList.add(PMMap1);
	}
	//////////////////
	// 	fields=map();
	// 	fields.put("field_data",{"field_text_data":fieldTextData});
	// 	fields.put("field_data",{"field_boolean_data":field_boolean_data});
	// // 	////
	// 	actionMap0.put("field_data",fields);
	// 	actionMap0.put("field_data",{"field_boolean_data":field_boolean_data});
	////////////////
	actionMap0.put("actions",fieldList);
	submitMap = Map();
	submitMap.put("templates",actionMap0);
	parameters = Map();
	parameters.put("is_quicksend","true");
	parameters.put("data",submitMap);
	info "Parameters: " + parameters;
	// 	////////////////////-----------------lead+lease-------------//////
	if(initial_lead_inspection_result == "Fail")
	{
		info "fail, create using template";
		if(totalTenant == 1)
		{
			response = zoho.sign.createUsingTemplate(53924000002918479,parameters);
			info "Here: 1 agree";
			info response;
		}
		else if(totalTenant == 2)
		{
			response = zoho.sign.createUsingTemplate(53924000002918253,parameters);
		}
		else if(totalTenant == 3)
		{
			response = zoho.sign.createUsingTemplate(53924000002918031,parameters);
		}
		else if(totalTenant == 4)
		{
			response = zoho.sign.createUsingTemplate(53924000002916523,parameters);
		}
	}
	else
	{
		info "pass or blank create using template";
		if(totalTenant == 1)
		{
			response = zoho.sign.createUsingTemplate(53924000002918659,parameters);
			info "Here: 1 agree";
			info response;
		}
		else if(totalTenant == 2)
		{
			response = zoho.sign.createUsingTemplate(53924000002918839,parameters);
		}
		else if(totalTenant == 3)
		{
			response = zoho.sign.createUsingTemplate(53924000002919039,parameters);
		}
		else if(totalTenant == 4)
		{
			response = zoho.sign.createUsingTemplate(53924000002919261,parameters);
		}
	}
	info "Template: " + response;
	/////////////////
	if(response != null)
	{
		lease_update_map = Map();
		lease_update_map.put("Lease_Status","Sent");
		lease_update_map.put("Lease_Document_Status","In Process");
		update_lease = zoho.crm.updateRecord("Leases",lease_id,lease_update_map);
	}
	docxId = response.get("requests").get("request_id");
	info "Document ID: " + docxId;
	mpdocx = Map();
	mpdocx.put("renewal_document_id",docxId);
	update = zoho.crm.updateRecord("Deals",opp_id,mpdocx);
	info "Deal: " + update;
	///////////////////////////////////////////////////////////////////////////////
}
catch (e)
{
	sendmail
	[
		from :zoho.loginuserid
		to :"erp@clevelandbricks.com","rabia@erphub.biz"
		subject :"Send new agreement on Renewal/ CRM"
		message :"Name: " + oppName + " Leaseid " + lease_id + " Root Cause: " + e
	]
}
}
content_copyCOPY