Fetch SPA document from File upload Field and send it Via Docu Sign

PHOTO EMBED

Tue Sep 02 2025 10:52:58 GMT+0000 (Coordinated Universal Time)

Saved by @usman13

string button.send_doc_via_docu_sign1(String Deal_id)
{
try 
{
	// ================= FETCH DEAL & VALIDATE ==================
	get_Details = invokeurl
	[
		url :"https://www.zohoapis.com/crm/v8/Deals/" + Deal_id
		type :GET
		connection:"newzohocrm"
	];
	data = get_Details.get("data");
	for each  rec in data
	{
		existing_envelope_id = rec.get("Envelope_ID");
		if(existing_envelope_id != null && existing_envelope_id != "")
		{
			return "Document already sent for this reservation";
		}
		unit_map = rec.get("Unit");
		unit_name = "";
		if(unit_map != null)
		{
			unit_name = ifnull(unit_map.get("name"),"");
		}
		//info unit_name;
		customer_id = rec.get("Contact_Name").get("id");
		contact_Details = zoho.crm.getRecordById("Contacts",customer_id);
		single_buyer_email = ifnull(contact_Details.get("Email"),"");
		single_buyer_name = ifnull(contact_Details.get("Full_Name"),"");
		single_buyer_phone = ifnull(contact_Details.get("Mobile"),"");
		// use Mobile for OTP
		Joint_buyer_details = ifnull(rec.get("Joint_buyer_name"),null);
		if(Joint_buyer_details != null)
		{
			Joint_buyer_id = Joint_buyer_details.get("id");
			Joint_Buyer_contact_details = zoho.crm.getRecordById("Contacts",Joint_buyer_id);
			Joint_Buyer_email = ifnull(Joint_Buyer_contact_details.get("Email"),"");
			Joint_Buyer_name = ifnull(Joint_Buyer_contact_details.get("Full_Name"),"");
		}
		Reservation_File_Upload = rec.get("Reservation_File_Upload");
		for each  file_data in Reservation_File_Upload
		{
			file_id = file_data.get("File_Id__s");
			response = invokeurl
			[
				url :"https://www.zohoapis.com/crm/v8/files?id=" + file_id
				type :GET
				connection:"newzohocrm"
			];
		}
	}
	// ================= CONVERT TO BASE64 ==================
	base64_pdf = zoho.encryption.base64Encode(response);
	doc = Map();
	doc.put("documentBase64",base64_pdf);
	doc.put("name","Sale Purchase Agreement");
	doc.put("fileExtension","docx");
	doc.put("documentId","1");
	// ================= SIGNERS ==================
	sign_here_buyer = List();
	sign_here_buyer.add({"anchorString":"Signed by Individual Purchaser","anchorUnits":"pixels","anchorXOffset":"168","anchorYOffset":"32","required":true});
	sign_here_buyer.add({"anchorString":"Signed for and on behalf of the Purchaser","anchorUnits":"pixels","anchorXOffset":"172","anchorYOffset":"4","required":true});
	date_signed_buyer = List();
	date_signed_buyer.add({"anchorString":"IN WITNESS WHEREOF the Parties have executed this Agreement.","anchorUnits":"pixels","anchorXOffset":"175","anchorYOffset":"115","required":false});
	date_signed_buyer.add({"anchorString":"I/We acknowledge that in the event there is a Constitution, I/we agree to comply with the provisions","anchorUnits":"pixels","anchorXOffset":"155","anchorYOffset":"215","required":false});
	date_signed_buyer.add({"anchorString":"Signed for and on behalf of the Purchaser","anchorUnits":"pixels","anchorXOffset":"167","anchorYOffset":"63","required":false});
	initials_buyer = List();
	initials_buyer.add({"anchorString":"Purchaser’s initials","anchorUnits":"pixels","anchorXOffset":"145","anchorYOffset":"16","required":true});
	tabs_buyer = Map();
	tabs_buyer.put("signHereTabs",sign_here_buyer);
	tabs_buyer.put("dateSignedTabs",date_signed_buyer);
	tabs_buyer.put("initialHereTabs",initials_buyer);
	signer1 = Map();
	signer1.put("email",single_buyer_email);
	signer1.put("name",single_buyer_name);
	signer1.put("recipientId","1");
	signer1.put("routingOrder","2");
	signer1.put("tabs",tabs_buyer);
	// ---- Phone OTP Identity Verification ----
	if(single_buyer_phone != "")
	{
		identity_verification = Map();
		identity_verification.put("workflowId","c368e411-1592-4001-a3df-dca94ac539ae");
		input_option = Map();
		input_option.put("name","phone_number_list");
		input_option.put("valueType","PhoneNumberList");
		phone_list = List();
		phone_item = Map();
		phone_item.put("countryCode","971");
		phone_item.put("number",single_buyer_phone);
		phone_list.add(phone_item);
		input_option.put("phoneNumberList",phone_list);
		identity_verification.put("inputOptions",{input_option});
		signer1.put("identityVerification",identity_verification);
	}
	signers_list = List();
	signers_list.add(signer1);
	// ---- Witness for Signer 1 ----
	sign_here_witness = List();
	sign_here_witness.add({"anchorString":"IN WITNESS WHEREOF the Parties have executed this Agreement","anchorUnits":"pixels","anchorXOffset":"168","anchorYOffset":"168","required":true});
	sign_here_witness.add({"anchorString":"Signed for and on behalf of the Purchaser","anchorUnits":"pixels","anchorXOffset":"172","anchorYOffset":"90","required":true});
	text_tabs_witness = List();
	text_tabs_witness.add({"anchorString":"IN WITNESS WHEREOF the Parties have executed this Agreement","anchorUnits":"pixels","anchorXOffset":"160","anchorYOffset":"190","tabLabel":"witness1_name","value":"","locked":false,"required":true});
	text_tabs_witness.add({"anchorString":"IN WITNESS WHEREOF the Parties have executed this Agreement","anchorUnits":"pixels","anchorXOffset":"160","anchorYOffset":"218","tabLabel":"witness1_address","value":"","locked":false,"required":true});
	text_tabs_witness.add({"anchorString":"IN WITNESS WHEREOF the Parties have executed this Agreement","anchorUnits":"pixels","anchorXOffset":"160","anchorYOffset":"243","tabLabel":"witness1_occupation","value":"","locked":false,"required":true});
	text_tabs_witness.add({"anchorString":"Signed for and on behalf of the Purchaser","anchorUnits":"pixels","anchorXOffset":"160","anchorYOffset":"110","tabLabel":"witness2_name","value":"","locked":false,"required":true});
	text_tabs_witness.add({"anchorString":"Signed for and on behalf of the Purchaser","anchorUnits":"pixels","anchorXOffset":"160","anchorYOffset":"135","tabLabel":"witness2_address","value":"","locked":false,"required":true});
	tabs_witness = Map();
	tabs_witness.put("signHereTabs",sign_here_witness);
	tabs_witness.put("textTabs",text_tabs_witness);
	witness1 = Map();
	witness1.put("witnessFor","1");
	witness1.put("recipientId","2");
	witness1.put("routingOrder","3");
	witness1.put("email","");
	witness1.put("name","");
	witness1.put("tabs",tabs_witness);
	// ---- Signer 2 (Joint Buyer, no OTP) ----
	if(Joint_buyer_details != null)
	{
		sign_here_joint = List();
		sign_here_joint.add({"anchorString":"Signed by Joint Individual Purchaser","anchorUnits":"pixels","anchorXOffset":"172","anchorYOffset":"28","required":true});
		sign_here_joint.add({"anchorString":"Signed by joint Purchaser","anchorUnits":"pixels","anchorXOffset":"172","anchorYOffset":"29","required":true});
		sign_here_joint.add({"anchorString":"Signed for and on behalf of the Joint Purchaser","anchorUnits":"pixels","anchorXOffset":"175","anchorYOffset":"5","required":true});
		date_signed_joint = List();
		date_signed_joint.add({"anchorString":"Signed by Joint Individual Purchaser","anchorUnits":"pixels","anchorXOffset":"173","anchorYOffset":"79","required":false});
		date_signed_joint.add({"anchorString":"Signed by joint Purchaser","anchorUnits":"pixels","anchorXOffset":"182","anchorYOffset":"80","required":false});
		date_signed_joint.add({"anchorString":"Signed for and on behalf of the Joint Purchaser","anchorUnits":"pixels","anchorXOffset":"174","anchorYOffset":"65","required":false});
		tabs_joint = Map();
		tabs_joint.put("signHereTabs",sign_here_joint);
		tabs_joint.put("dateSignedTabs",date_signed_joint);
		signer2 = Map();
		signer2.put("email",Joint_Buyer_email);
		signer2.put("name",Joint_Buyer_name);
		signer2.put("recipientId","3");
		signer2.put("routingOrder","4");
		signer2.put("tabs",tabs_joint);
		signers_list.add(signer2);
		// ---- Witness for Signer 2 (Joint Buyer) ----
		sign_here_witness2 = List();
		sign_here_witness2.add({"anchorString":"Signed by Joint Individual Purchaser","anchorUnits":"pixels","anchorXOffset":"173","anchorYOffset":"140","required":true});
		//////Changed
		sign_here_witness2.add({"anchorString":"Signed for and on behalf of the Joint Purchaser","anchorUnits":"pixels","anchorXOffset":"174","anchorYOffset":"103","required":true});
		//////Changed
		//////////////////////
		/////////////////////////////
		text_tabs_witness2 = List();
		text_tabs_witness2.add({"anchorString":"Signed by Joint Individual Purchaser","anchorUnits":"pixels","anchorXOffset":"173","anchorYOffset":"160","tabLabel":"witness3_name","value":"","locked":false,"required":true});
		//////Changed
		text_tabs_witness2.add({"anchorString":"Signed by Joint Individual Purchaser","anchorUnits":"pixels","anchorXOffset":"173","anchorYOffset":"185","tabLabel":"witness3_address","value":"","locked":false,"required":true});
		///changed
		text_tabs_witness2.add({"anchorString":"Signed by Joint Individual Purchaser","anchorUnits":"pixels","anchorXOffset":"173","anchorYOffset":"210","tabLabel":"witness3_occupation","value":"","locked":false,"required":true});
		///changed
		////////////
		///////////////////////////
		text_tabs_witness2.add({"anchorString":"Signed for and on behalf of the Joint Purchaser","anchorUnits":"pixels","anchorXOffset":"174","anchorYOffset":"118","tabLabel":"witness4_name","value":"","locked":false,"required":true});
		///chnages
		text_tabs_witness2.add({"anchorString":"Signed for and on behalf of the Joint Purchaser","anchorUnits":"pixels","anchorXOffset":"170","anchorYOffset":"145","tabLabel":"witness4_address","value":"","locked":false,"required":true});
		///changed
		witness2_tabs = Map();
		witness2_tabs.put("signHereTabs",sign_here_witness2);
		witness2_tabs.put("textTabs",text_tabs_witness2);
		witness2 = Map();
		witness2.put("witnessFor","3");
		witness2.put("recipientId","4");
		witness2.put("routingOrder","5");
		witness2.put("email","");
		witness2.put("name","");
		witness2.put("tabs",witness2_tabs);
	}
	// ---- Manager Final Signer (Lani Alipio) ----
	sign_here_manager = List();
	sign_here_manager.add({"documentId":"1","pageNumber":"2","xPosition":"100","yPosition":"600"});
	tabs_manager = Map();
	tabs_manager.put("signHereTabs",sign_here_manager);
	manager_signer = Map();
	manager_signer.put("email","Lani.Alipio@leosuk.com");
	///
	manager_signer.put("name","Lani Alipio");
	manager_signer.put("recipientId","11");
	if(Joint_buyer_details != null)
	{
		manager_signer.put("routingOrder","6");
	}
	else
	{
		manager_signer.put("routingOrder","5");
	}
	manager_signer.put("tabs",tabs_manager);
	signers_list.add(manager_signer);
	// ---- CC LIST (Mark + Lani) ----
	cc_list = List();
	cc_mark = Map();
	cc_mark.put("email","Manthony@leosuk.com");
	//
	cc_mark.put("name","Mark");
	cc_mark.put("recipientId","12");
	cc_mark.put("routingOrder","1");
	cc_list.add(cc_mark);
	cc_noor = Map();
	cc_noor.put("email","Lani.Alipio@leosuk.com");
	///
	cc_noor.put("name","Lani Alipio");
	cc_noor.put("recipientId","13");
	cc_noor.put("routingOrder","1");
	cc_list.add(cc_noor);
	////
	////
	cc_ana = Map();
	cc_ana.put("email","anamay@leosuk.com");
	//////
	cc_ana.put("name","Ana May S. Zamora");
	cc_ana.put("recipientId","14");
	cc_ana.put("routingOrder","1");
	cc_list.add(cc_ana);
	// ================= RECIPIENTS MAP ==================
	recipients = Map();
	recipients.put("signers",signers_list);
	if(Joint_buyer_details != null)
	{
		recipients.put("witnesses",{witness1,witness2});
	}
	else
	{
		recipients.put("witnesses",{witness1});
	}
	recipients.put("carbonCopies",cc_list);
	envelope = Map();
	envelope.put("documents",{doc});
	envelope.put("emailSubject","SPA for Signature - " + unit_name + " Final");
	envelope.put("status","sent");
	envelope.put("recipients",recipients);
	// ================= FETCH DOCUSIGN ACCESS TOKEN ==================
	access_token_response = invokeurl
	[
		url :"https://www.zohoapis.com/crm/v6/settings/variables/5971686000102746225"
		type :GET
		connection:"newzohocrm"
	];
	access_token = access_token_response.get("variables").get(0).get("value");
	headers = Map();
	headers.put("Authorization","Bearer " + access_token);
	headers.put("Content-Type","application/json");
	response = invokeurl
	[
		url :"https://eu.docusign.net/restapi/v2.1/accounts/2a0daa7d-a770-4979-8208-9543d21f12e5/envelopes"
		type :POST
		parameters:envelope.toString()
		headers:headers
	];
	info response;
	envelopeId = response.get("envelopeId");
	update_map = Map();
	update_map.put("Envelope_ID",envelopeId);
	update_map.put("Next_Status_Sync_Time",zoho.currenttime.toString("yyyy-MM-dd'T'HH:mm:ss","Asia/Dubai"));
	update_map.put("Docu_Sign_Status","Sent");
	Update_Rec = zoho.crm.updateRecord("Deals",Deal_id,update_map);
	return "Document has been successfully sent to all recipients.";
}
catch (e)
{
	sendmail
	[
		from :zoho.loginuserid
		to :"zoho.failure@leosinternational.zohodesk.com"
		subject :"[DocuSign Error | Deal " + Deal_id + "]"
		message :"An error occurred while sending the document via DocuSign.\n\nDeal ID: " + Deal_id + "\nError Details: " + e.toString()
	]
	return "Error occurred: " + e.toString();
}
return "";
}
content_copyCOPY