Print

Zoho CRM: Upload a Product Photo using Deluge

What?
A quick article to remind me on how to upload a photo using CRM API v2. Yes it's documented and yes it still confuses me now and again. So I'm writing this to remind me and to keep reminding me of how to do this.

Why?
My use-case is that I have a photo in Zoho Creator but the source doesn't really matter as the part I struggled on was uploading it to CRM. I want to update the photo in the CRM record.

How?
So we have to do the usual which is download the photo using invokeUrl then we set the paramname not to attachment but to file. We then use another invokeUrl to upload the photo to CRM:
copyraw
// init
v_AppOwner = "joel_the_awesomest";
v_AppName = "joels_app";
v_ReportName = "My_Report";
// specify your CRM record ID for this product
v_CrmProductID = 1234567890123456789;
// specify your Creator record ID holding the image
v_CreatorQuoteID = 9876543210987654321;
//
// get the full details of the Creator record
r_GetFullRecord = zoho.creator.getRecordById(v_AppOwner,v_AppName,v_ReportName,v_CreatorQuoteID,"joels_creator");
if(!isnull(r_GetFullRecord.get("data")))
{
	r_CreatorRecord = r_GetFullRecord.get("data");
	v_CreatorPhotoUrl = ifnull(r_CreatorRecord.get("Display_Photo"),"");
	if(v_CreatorPhotoUrl!="")
	{
		//
		// download photo from your Creator (change connection and check TLD)
		v_Endpoint1 = "https://creatorapp.zoho.com" + v_CreatorPhotoUrl;
		r_CreatorPhoto = invokeUrl
		[
			url: v_Endpoint1
			type: GET
			connection: "joels_creator"
		];
		info r_CreatorPhoto;
		//
		// set the param name to file (this is important)
		r_CreatorPhoto.setParamName("file");
		//
		// upload the photo to CRM (and prevent workflows from running)
		v_Endpoint2 = "https://www.zohoapis.com/crm/v2/Products/" + v_CrmProductID + "/photo?restrict_triggers=workflow";
		r_CrmPhoto = invokeurl
		[
			url: v_Endpoint2
			type: POST
			files: r_CreatorPhoto
			connection: "joels_crm"
		];
		info r_CrmPhoto;
	}
}
  1.  // init 
  2.  v_AppOwner = "joel_the_awesomest"
  3.  v_AppName = "joels_app"
  4.  v_ReportName = "My_Report"
  5.  // specify your CRM record ID for this product 
  6.  v_CrmProductID = 1234567890123456789
  7.  // specify your Creator record ID holding the image 
  8.  v_CreatorQuoteID = 9876543210987654321
  9.  // 
  10.  // get the full details of the Creator record 
  11.  r_GetFullRecord = zoho.creator.getRecordById(v_AppOwner,v_AppName,v_ReportName,v_CreatorQuoteID,"joels_creator")
  12.  if(!isnull(r_GetFullRecord.get("data"))) 
  13.  { 
  14.      r_CreatorRecord = r_GetFullRecord.get("data")
  15.      v_CreatorPhotoUrl = ifnull(r_CreatorRecord.get("Display_Photo"),"")
  16.      if(v_CreatorPhotoUrl!="") 
  17.      { 
  18.          // 
  19.          // download photo from your Creator (change connection and check TLD) 
  20.          v_Endpoint1 = "https://creatorapp.zoho.com" + v_CreatorPhotoUrl; 
  21.          r_CreatorPhoto = invokeUrl 
  22.          [ 
  23.              url: v_Endpoint1 
  24.              type: GET 
  25.              connection: "joels_creator" 
  26.          ]
  27.          info r_CreatorPhoto; 
  28.          // 
  29.          // set the param name to file (this is important) 
  30.          r_CreatorPhoto.setParamName("file")
  31.          // 
  32.          // upload the photo to CRM (and prevent workflows from running) 
  33.          v_Endpoint2 = "https://www.zohoapis.com/crm/v2/Products/" + v_CrmProductID + "/photo?restrict_triggers=workflow"
  34.          r_CrmPhoto = invokeUrl 
  35.          [ 
  36.              url: v_Endpoint2 
  37.              type: POST 
  38.              files: r_CreatorPhoto 
  39.              connection: "joels_crm" 
  40.          ]
  41.          info r_CrmPhoto; 
  42.      } 
  43.  } 

The above should yield something like:
copyraw
/1234567890123_my_beautiful_face.jpg

{"code":"SUCCESS","details":{},"message":"photo uploaded successfully","status":"success"}
  1.  /1234567890123_my_beautiful_face.jpg 
  2.   
  3.  {"code":"SUCCESS","details":{},"message":"photo uploaded successfully","status":"success"} 

Source(s):
Category: Zoho :: Article: 774