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:
// 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; } }
- // 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;
- }
- }
The above should yield something like:
/1234567890123_my_beautiful_face.jpg {"code":"SUCCESS","details":{},"message":"photo uploaded successfully","status":"success"}
- /1234567890123_my_beautiful_face.jpg
- {"code":"SUCCESS","details":{},"message":"photo uploaded successfully","status":"success"}
Source(s):