This is an article to document how I downloaded an image held in a Zoho Creator form, and sent it to an API wanting the publicly accessible link or URL of the image.
Why?
I've got some other articles on handling images in Zoho Creator (see "Sources" below), the most relevant one being my article Zoho Deluge: Get Image Uploaded in Creator Form which has one method of getting a public link.
My use-case scenario here is that while I could upload the link from Creator to eBay's Picture Services successfully, Shopify would not accept any links I gave it. Why not use the links that eBay returns? Well eBay returns URLs to images that have been resized to 400x400 (even with tweak to return 800x800). I wanted a way to upload my image directly to Shopify in 3024x3024 resolution (or the size that the client wants: iPhone res). I then found that Shopify does accept a Base64Encoded version of the image and the below is how I achieved this.
How?
First-off, let me list the various formats that Zoho Creator says that public link exists. Let's say I have a field called Main_Photo and this is in a form called Portal_Listing with a report/view called Portal_Listing_Report and I have a publish key held in v_ListingPublishKey and lastly, my datacenter is .COM:
URL Syntax #1
copyraw
// works in eBay v_ImageSrc = "https://creator.zoho.com/file" + zoho.appuri + "Portal_Listing_Report/" + input.ID + "/Main_Photo/image-download/" + v_ListingPublishKey + "?filepath=/" + v_Filename;
- // works in eBay
- v_ImageSrc = "https://creator.zoho.com/file" + zoho.appuri + "Portal_Listing_Report/" + input.ID + "/Main_Photo/image-download/" + v_ListingPublishKey + "?filepath=/" + v_Filename;
URL Syntax #2
copyraw
// works in eBay v_ImageSrc = "https://creator.zohopublic.com" + zoho.appuri + "Portal_Listing_Report/" + input.ID + "/Main_Photo/image-download/" + v_ListingPublishKey + "/" + v_Filename;
- // works in eBay
- v_ImageSrc = "https://creator.zohopublic.com" + zoho.appuri + "Portal_Listing_Report/" + input.ID + "/Main_Photo/image-download/" + v_ListingPublishKey + "/" + v_Filename;
URL Syntax #3
copyraw
// Works in eBay v_ImageSrc = "https://creatorexport.zoho.com" + zoho.appuri + "Portal_Listing_Report/" + input.ID + "/Main_Photo/image-download/" + v_ListingPublishKey + "/" + v_Filename;
- // Works in eBay
- v_ImageSrc = "https://creatorexport.zoho.com" + zoho.appuri + "Portal_Listing_Report/" + input.ID + "/Main_Photo/image-download/" + v_ListingPublishKey + "/" + v_Filename;
URL Syntax #4
The following won't work for the example below as it doesn't have the publish/public key, but it can be used internally:
copyraw
// DOESN'T WORK FOR ME!!! v_ImageSrc = "https://creatorexport.zoho.com/DownloadFile.do?filepath=/"+v_Filename+"&sharedBy="+zoho.adminuser+"&appLinkName="+zoho.appname+"&viewLinkName=Portal_Listing_Report";
- // DOESN'T WORK FOR ME!!!
- v_ImageSrc = "https://creatorexport.zoho.com/DownloadFile.do?filepath=/"+v_Filename+"&sharedBy="+zoho.adminuser+"&appLinkName="+zoho.appname+"&viewLinkName=Portal_Listing_Report";
URL Syntax #5 [image]: Update 2023
Again this example doesn't include the publish key but can be used across apps in Creator [note this is just for quick ref]. Where my form is called "My_Form" and the image field is "My_Photo" [also note: instead of zoho.appuri, we are hardcoding the appowner and appname as this is used across Creator apps, ie. images are stored in a separate app]:
copyraw
l_CompatiblePhotoExtensions = {"png","jpg","jpeg","gif","bmp","heic"};
c_Document = My_Form[ID == 1234567890];
//
// for image field type
v_PhotoFull = ifnull(c_Document.My_Photo,"");
v_PhotoWithExt = v_PhotoFull.getSuffix("/image/").getPrefix("\"");
v_PhotoFileExtension = v_PhotoWithExt.subString(v_PhotoWithExt.lastIndexOf(".") + 1).toLowerCase();
//
if(l_CompatiblePhotoExtensions.containsIgnoreCase(v_PhotoFileExtension))
{
v_ThisPhotoSrc = "https://creator.zoho.com/file/appowner/appname/My_Form_Report/" + c_Document.ID + "/My_Photo/image-download?filepath=/" + v_PhotoWithExt;
}
- l_CompatiblePhotoExtensions = {"png","jpg","jpeg","gif","bmp","heic"};
- c_Document = My_Form[ID == 1234567890];
- //
- // for image field type
- v_PhotoFull = ifnull(c_Document.My_Photo,"");
- v_PhotoWithExt = v_PhotoFull.getSuffix("/image/").getPrefix("\"");
- v_PhotoFileExtension = v_PhotoWithExt.subString(v_PhotoWithExt.lastIndexOf(".") + 1).toLowerCase();
- //
- if(l_CompatiblePhotoExtensions.containsIgnoreCase(v_PhotoFileExtension))
- {
- v_ThisPhotoSrc = "https://creator.zoho.com/file/appowner/appname/My_Form_Report/" + c_Document.ID + "/My_Photo/image-download?filepath=/" + v_PhotoWithExt;
- }
URL Syntax #6 [file]: Update 2023
Same again but this is for a field of type document/file rather than image. Where my form is called "My_Form" and the file upload field is "My_File":
copyraw
c_Document = My_Form[ID == 1234567890];
//
// for file upload field type
v_DocFileName = ifnull(c_Document.My_File,"");
v_DocExtension = v_DocFileName.subString(v_DocFileName.lastIndexOf(".") + 1).toLowerCase();
v_ThisDocSrc = "https://creator.zoho.com/file/appowner/appname/My_Form_Report/" + c_Document.ID + "/My_File/download?filepath=/" + v_DocFileName;
//
// with public key
v_PublishKey = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
v_ThisDocSrc = "https://creator.zoho.com/file/appowner/appname/My_Form_Report/" + c_Document.ID + "/My_File/download/" + v_PublishKey + "?filepath=/" + v_DocFileName;
//
// if My_File is in a subform
v_DocFileName = ifnull(row.My_File,"");
v_ThisDocSrc = "https://creator.zoho.com/file/appowner/appname/My_Form_Report/" + c_Document.ID + "/My_Subform.My_File/download?filepath=/" + v_DocFileName;
- c_Document = My_Form[ID == 1234567890];
- //
- // for file upload field type
- v_DocFileName = ifnull(c_Document.My_File,"");
- v_DocExtension = v_DocFileName.subString(v_DocFileName.lastIndexOf(".") + 1).toLowerCase();
- v_ThisDocSrc = "https://creator.zoho.com/file/appowner/appname/My_Form_Report/" + c_Document.ID + "/My_File/download?filepath=/" + v_DocFileName;
- //
- // with public key
- v_PublishKey = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
- v_ThisDocSrc = "https://creator.zoho.com/file/appowner/appname/My_Form_Report/" + c_Document.ID + "/My_File/download/" + v_PublishKey + "?filepath=/" + v_DocFileName;
- //
- // if My_File is in a subform
- v_DocFileName = ifnull(row.My_File,"");
- v_ThisDocSrc = "https://creator.zoho.com/file/appowner/appname/My_Form_Report/" + c_Document.ID + "/My_Subform.My_File/download?filepath=/" + v_DocFileName;
The API Solution
The following solution requires that your target API accepts base64encoded images or that you can send these in a cURL request (same thing really):
copyraw
Sends something like the following request (sent using Zoho Deluge but the cURL breakdown as follows):
string API.fn_ShopifyQuery_UploadPhoto(int p_Position, int p_ProductID)
{
v_Output = "";
m_Header = Map();
m_Header.put("Content-Type","application/json");
//
// your shopify details
v_ClientID = "<YOUR_CLIENT_ID>";
v_ClientSecret = "<YOUR_CLIENT_SECRET>";
v_ShopID = "example.myshopify.com";
v_ShopifyURL = "https://" + v_ClientID + ":" + v_ClientSecret + "@" + v_ShopID;
v_ShopifyApiVersion = "2020-01";
//
// your creator details
v_ListingPublishKey = "AAAAABBBBBCCCCDDDDEEEFFFGGGGHHHIIIJJJKKLLLMMMNNOOOPPPQQWRRRSSTTUUVVWWXXYYZZ122345567890";
v_Position = ifnull(p_Position, 1);
v_ShopifyProductID = ifnull(p_ProductID, 123456789012);
r_PortalListing = Portal_Listing[Shopify_Product_ID == v_ShopifyProductID];
if(r_PortalListing.count()>0)
{
v_Filename = r_PortalListing.Main_Photo.getSuffix("/image/").getPrefix("\"");
v_ImageSrc = "https://creator.zoho.com/file" + zoho.appuri + "Portal_Listing_Report/";
v_ImageSrc = v_ImageSrc + r_PortalListing.ID + "/Main_Photo/image-download/" + v_ListingPublishKey;
v_ImageSrc = v_ImageSrc + "?filepath=/" + v_Filename;
r_DownloadPhoto = invokeUrl
[
url: v_ImageSrc
type: GET
];
r_DownloadPhoto.setParamName("file");
v_EncodedImg = zoho.encryption.base64Encode(r_DownloadPhoto);
m_Params = Map();
m_SubParams = Map();
m_SubParams.put("position", 1);
m_SubParams.put("attachment", v_EncodedImg);
m_SubParams.put("filename", v_Filename);
m_Params.put("image", m_SubParams);
//
v_Endpoint = v_ShopifyURL + "/admin/api/" + v_ShopifyApiVersion.toString() + "/products/" + v_ShopifyProductID + "/images.json";
r_UploadPhoto = invokeurl
[
url :v_Endpoint
type :POST
headers:m_Header
parameters: m_Params.toString()
];
v_Output = r_UploadPhoto;
}
return v_Output;
}
- string API.fn_ShopifyQuery_UploadPhoto(int p_Position, int p_ProductID)
- {
- v_Output = "";
- m_Header = Map();
- m_Header.put("Content-Type","application/json");
- //
- // your shopify details
- v_ClientID = "<YOUR_CLIENT_ID>";
- v_ClientSecret = "<YOUR_CLIENT_SECRET>";
- v_ShopID = "example.myshopify.com";
- v_ShopifyURL = "https://" + v_ClientID + ":" + v_ClientSecret + "@" + v_ShopID;
- v_ShopifyApiVersion = "2020-01";
- //
- // your creator details
- v_ListingPublishKey = "AAAAABBBBBCCCCDDDDEEEFFFGGGGHHHIIIJJJKKLLLMMMNNOOOPPPQQWRRRSSTTUUVVWWXXYYZZ122345567890";
- v_Position = ifnull(p_Position, 1);
- v_ShopifyProductID = ifnull(p_ProductID, 123456789012);
- r_PortalListing = Portal_Listing[Shopify_Product_ID == v_ShopifyProductID];
- if(r_PortalListing.count()>0)
- {
- v_Filename = r_PortalListing.Main_Photo.getSuffix("/image/").getPrefix("\"");
- v_ImageSrc = "https://creator.zoho.com/file" + zoho.appuri + "Portal_Listing_Report/";
- v_ImageSrc = v_ImageSrc + r_PortalListing.ID + "/Main_Photo/image-download/" + v_ListingPublishKey;
- v_ImageSrc = v_ImageSrc + "?filepath=/" + v_Filename;
- r_DownloadPhoto = invokeUrl
- [
- url: v_ImageSrc
- type: GET
- ];
- r_DownloadPhoto.setParamName("file");
- v_EncodedImg = zoho.encryption.base64Encode(r_DownloadPhoto);
- m_Params = Map();
- m_SubParams = Map();
- m_SubParams.put("position", 1);
- m_SubParams.put("attachment", v_EncodedImg);
- m_SubParams.put("filename", v_Filename);
- m_Params.put("image", m_SubParams);
- //
- v_Endpoint = v_ShopifyURL + "/admin/api/" + v_ShopifyApiVersion.toString() + "/products/" + v_ShopifyProductID + "/images.json";
- r_UploadPhoto = invokeUrl
- [
- url :v_Endpoint
- type :POST
- headers:m_Header
- parameters: m_Params.toString()
- ];
- v_Output = r_UploadPhoto;
- }
- return v_Output;
- }
copyraw
Yields something like the following response:
curl -d '{"image":{"position":1,"attachment":"/9/R0lGODlhbgCMAPf ... ... ... Q7PCAAOw==\n","filename":"my_test_photo.jpg"}}'
-X POST "https://your-development-store.myshopify.com/admin/api/2021-10/products/9876543210987/images.json" \
-H "X-Shopify-Access-Token: {access_token}"
- curl -d '{"image":{"position":1,"attachment":"/9/R0lGODlhbgCMAPf ... ... ... Q7PCAAOw==\n","filename":"my_test_photo.jpg"}}'
- -X POST "https://your-development-store.myshopify.com/admin/api/2021-10/products/9876543210987/images.json" \
- -H "X-Shopify-Access-Token: {access_token}"
copyraw
{
"image": {
"position": 1,
"width": 3024,
"height": 3024,
"alt": null,
"id": 12345678901234,
"product_id": 9876543210987,
"created_at": "2021-10-17T21:45:56+01:00",
"updated_at": "2021-10-17T21:45:56+01:00",
"src": "https://cdn.shopify.com/s/files/1/2345/6789/1234/products/aaaabbbbccccddddeeeeffff11112222.jpg?v=1234567890",
"variant_ids": [],
"admin_graphql_api_id": "gid://shopify/ProductImage/23456789012345"
}
}
- {
- "image": {
- "position": 1,
- "width": 3024,
- "height": 3024,
- "alt": null,
- "id": 12345678901234,
- "product_id": 9876543210987,
- "created_at": "2021-10-17T21:45:56+01:00",
- "updated_at": "2021-10-17T21:45:56+01:00",
- "src": "https://cdn.shopify.com/s/files/1/2345/6789/1234/products/aaaabbbbccccddddeeeeffff11112222.jpg?v=1234567890",
- "variant_ids": [],
- "admin_graphql_api_id": "gid://shopify/ProductImage/23456789012345"
- }
- }
This next version is a more generic form of the above function uploading the photo to Shopify:
copyraw
And it's usage:
void API.fn_ShopifyQuery_UploadPhoto(int p_ProductID, int p_Position, string p_ImageSrc)
{
m_Header = Map();
m_Header.put("Content-Type","application/json");
//
// app specific
v_ClientID = "<YOUR_CLIENT_ID>";
v_ClientSecret = "<YOUR_CLIENT_SECRET>";
v_ShopID = "example.myshopify.com";
v_ShopifyURL = "https://" + v_ClientID + ":" + v_ClientSecret + "@" + v_ShopID;
v_ShopifyApiVersion = "2020-01";
//
// your creator details
v_Position = ifnull(p_Position,1);
v_ShopifyProductID = ifnull(p_ProductID,0);
v_ImageSrc = ifnull(p_ImageSrc,"");
if(v_ImageSrc != "" && v_ShopifyProductID != 0)
{
r_DownloadPhoto = invokeurl
[
url :v_ImageSrc
type :GET
];
r_DownloadPhoto.setParamName("file");
v_EncodedImg = zoho.encryption.base64Encode(r_DownloadPhoto);
//
// build shopify request
m_Params = Map();
m_SubParams = Map();
m_SubParams.put("position",v_Position);
m_SubParams.put("attachment",v_EncodedImg);
m_Params.put("image",m_SubParams);
//
v_Endpoint = v_ShopifyURL + "/admin/api/" + v_ShopifyApiVersion.toString() + "/products/" + v_ShopifyProductID + "/images.json";
r_UploadPhoto = invokeurl
[
url :v_Endpoint
type :POST
parameters:m_Params.toString()
headers:m_Header
];
}
}
- void API.fn_ShopifyQuery_UploadPhoto(int p_ProductID, int p_Position, string p_ImageSrc)
- {
- m_Header = Map();
- m_Header.put("Content-Type","application/json");
- //
- // app specific
- v_ClientID = "<YOUR_CLIENT_ID>";
- v_ClientSecret = "<YOUR_CLIENT_SECRET>";
- v_ShopID = "example.myshopify.com";
- v_ShopifyURL = "https://" + v_ClientID + ":" + v_ClientSecret + "@" + v_ShopID;
- v_ShopifyApiVersion = "2020-01";
- //
- // your creator details
- v_Position = ifnull(p_Position,1);
- v_ShopifyProductID = ifnull(p_ProductID,0);
- v_ImageSrc = ifnull(p_ImageSrc,"");
- if(v_ImageSrc != "" && v_ShopifyProductID != 0)
- {
- r_DownloadPhoto = invokeUrl
- [
- url :v_ImageSrc
- type :GET
- ];
- r_DownloadPhoto.setParamName("file");
- v_EncodedImg = zoho.encryption.base64Encode(r_DownloadPhoto);
- //
- // build shopify request
- m_Params = Map();
- m_SubParams = Map();
- m_SubParams.put("position",v_Position);
- m_SubParams.put("attachment",v_EncodedImg);
- m_Params.put("image",m_SubParams);
- //
- v_Endpoint = v_ShopifyURL + "/admin/api/" + v_ShopifyApiVersion.toString() + "/products/" + v_ShopifyProductID + "/images.json";
- r_UploadPhoto = invokeUrl
- [
- url :v_Endpoint
- type :POST
- parameters:m_Params.toString()
- headers:m_Header
- ];
- }
- }
copyraw
// somewhere in the beginning
l_ShopifyPictures = List();
v_ListingPublishKey = "AAAAABBBBBCCCCDDDDEEEFFFGGGGHHHIIIJJJKKLLLMMMNNOOOPPPQQWRRRSSTTUUVVWWXXYYZZ122345567890";
if(!isnull(input.Main_Photo))
{
v_Filename = input.Main_Photo.getSuffix("/image/").getPrefix("\"");
v_ImageSrc = "https://creator.zoho.com/file" + zoho.appuri + "Portal_Listing_Report/";
v_ImageSrc = v_ImageSrc + input.ID + "/Main_Photo/image-download/" + v_ListingPublishKey;
v_ImageSrc = v_ImageSrc + "?filepath=/" + v_Filename;
l_ShopifyPictures.add(v_ImageSrc);
}
// after I've pushed the product to Shopify and it returned to me a Product ID:
if(v_ShopifyProductID != 0)
{
// do photos
if(input.Photos_Changed)
{
v_PositionIndex = 1;
for each v_ShopifyImageSrc in l_ShopifyPictures
{
if(v_ShopifyImageSrc!="")
{
thisapp.API.fn_ShopifyQuery_UploadPhoto(v_ShopifyProductID, v_PositionIndex, v_ShopifyImageSrc);
}
v_PositionIndex = v_PositionIndex + 1;
}
info "Re-uploaded Photos to Shopify";
}
}
- // somewhere in the beginning
- l_ShopifyPictures = List();
- v_ListingPublishKey = "AAAAABBBBBCCCCDDDDEEEFFFGGGGHHHIIIJJJKKLLLMMMNNOOOPPPQQWRRRSSTTUUVVWWXXYYZZ122345567890";
- if(!isnull(input.Main_Photo))
- {
- v_Filename = input.Main_Photo.getSuffix("/image/").getPrefix("\"");
- v_ImageSrc = "https://creator.zoho.com/file" + zoho.appuri + "Portal_Listing_Report/";
- v_ImageSrc = v_ImageSrc + input.ID + "/Main_Photo/image-download/" + v_ListingPublishKey;
- v_ImageSrc = v_ImageSrc + "?filepath=/" + v_Filename;
- l_ShopifyPictures.add(v_ImageSrc);
- }
- // after I've pushed the product to Shopify and it returned to me a Product ID:
- if(v_ShopifyProductID != 0)
- {
- // do photos
- if(input.Photos_Changed)
- {
- v_PositionIndex = 1;
- for each v_ShopifyImageSrc in l_ShopifyPictures
- {
- if(v_ShopifyImageSrc!="")
- {
- thisapp.API.fn_ShopifyQuery_UploadPhoto(v_ShopifyProductID, v_PositionIndex, v_ShopifyImageSrc);
- }
- v_PositionIndex = v_PositionIndex + 1;
- }
- info "Re-uploaded Photos to Shopify";
- }
- }
Correction / Additional
The above solution means that for every photo uploaded, an API call is used up. If you have low limits, even 2000, this means you can only upload 100 products a day if they each have 20 photos (even less products then that as the upload of a product costs 1 API call). But I have found you can submit all photos in one go as well:
copyraw
// build up list of photos from Creator fields
l_CreatorPhotos = List();
v_ListingPublishKey = "AAAAABBBBBCCCCDDDDEEEFFFGGGGHHHIIIJJJKKLLLMMMNNOOOPPPQQWRRRSSTTUUVVWWXXYYZZ122345567890";
if(!isnull(input.Main_Photo))
{
v_Filename = input.Main_Photo.getSuffix("/image/").getPrefix("\"");
v_ImageSrc = "https://creator.zoho.com/file" + zoho.appuri + "Portal_Listing_Report/";
v_ImageSrc = v_ImageSrc + input.ID + "/Main_Photo/image-download/" + v_ListingPublishKey;
v_ImageSrc = v_ImageSrc + "?filepath=/" + v_Filename;
l_CreatorPhotos.add(v_ImageSrc);
}
//
// then upload
l_ShopifyUploadImages = List();
v_PositionIndex = 0;
for each v_CreatorImageSrc in l_CreatorPhotos
{
if(v_CreatorImageSrc != "")
{
v_PositionIndex = v_PositionIndex + 1;
r_DownloadPhoto = invokeurl
[
url : v_CreatorImageSrc
type : GET
];
r_DownloadPhoto.setParamName("file");
v_EncodedImg = zoho.encryption.base64Encode(r_DownloadPhoto);
//
// build shopify request
m_Image = Map();
m_SubParams = Map();
m_SubParams.put("position",v_PositionIndex);
m_SubParams.put("attachment",v_EncodedImg);
m_Image.put("image",m_SubParams);
l_ShopifyUploadImages.add(m_Image);
}
}
//
v_Endpoint = v_ShopifyURL + "/admin/api/" + v_ShopifyAPIVersion + "/products/" + v_ShopifyProductID + "/images.json";
r_UploadPhoto = invokeurl
[
url : v_Endpoint
type : POST
parameters: l_ShopifyUploadImages.toString()
headers: m_Header
];
- // build up list of photos from Creator fields
- l_CreatorPhotos = List();
- v_ListingPublishKey = "AAAAABBBBBCCCCDDDDEEEFFFGGGGHHHIIIJJJKKLLLMMMNNOOOPPPQQWRRRSSTTUUVVWWXXYYZZ122345567890";
- if(!isnull(input.Main_Photo))
- {
- v_Filename = input.Main_Photo.getSuffix("/image/").getPrefix("\"");
- v_ImageSrc = "https://creator.zoho.com/file" + zoho.appuri + "Portal_Listing_Report/";
- v_ImageSrc = v_ImageSrc + input.ID + "/Main_Photo/image-download/" + v_ListingPublishKey;
- v_ImageSrc = v_ImageSrc + "?filepath=/" + v_Filename;
- l_CreatorPhotos.add(v_ImageSrc);
- }
- //
- // then upload
- l_ShopifyUploadImages = List();
- v_PositionIndex = 0;
- for each v_CreatorImageSrc in l_CreatorPhotos
- {
- if(v_CreatorImageSrc != "")
- {
- v_PositionIndex = v_PositionIndex + 1;
- r_DownloadPhoto = invokeUrl
- [
- url : v_CreatorImageSrc
- type : GET
- ];
- r_DownloadPhoto.setParamName("file");
- v_EncodedImg = zoho.encryption.base64Encode(r_DownloadPhoto);
- //
- // build shopify request
- m_Image = Map();
- m_SubParams = Map();
- m_SubParams.put("position",v_PositionIndex);
- m_SubParams.put("attachment",v_EncodedImg);
- m_Image.put("image",m_SubParams);
- l_ShopifyUploadImages.add(m_Image);
- }
- }
- //
- v_Endpoint = v_ShopifyURL + "/admin/api/" + v_ShopifyAPIVersion + "/products/" + v_ShopifyProductID + "/images.json";
- r_UploadPhoto = invokeUrl
- [
- url : v_Endpoint
- type : POST
- parameters: l_ShopifyUploadImages.toString()
- headers: m_Header
- ];
Correction / Additional version 2.0
Playing around with this for a client, I found an even better solution! When creating or updating a product, you can instead of giving it Image URLs, give it the Image as attachment a base64 encoded string. Note the above says that 10 photos produced 10 webhooks but actually I'd overlooked the webhook used to invokeUrl download a Creator file so it was using 20 webhooks; the following reduces that back down to 10 webhooks (1 to download each photo and base64 encode it but no extras to upload it in addition to the product update):
copyraw
// initialize
m_Product = Map();
b_CreateProduct = true;
//
// I have a field called Shopify Product ID which stores the... um... shopify product id...
if(!isNull(input.Shopify_Product_ID))
{
m_Product.put("id",input.Shopify_Product_ID);
b_CreateProduct = false;
}
//
// title and HTML
m_Product.put("title",input.Listing_Title);
if(!isNull(input.Full_Shopify_HTML_Code))
{
m_Product.put("body_html",input.Full_Shopify_HTML_Code);
}
m_Product.put("vendor",input.Brand);
m_Product.put("product_type",input.Item_Type);
//
// listings / channels
m_Product.put("published_scope","global");
m_Product.put("published",true); // set to true when comfortable with it publishing correctly
m_Product.put("status",ifnull(input.Publish_Status.toLowerCase(),"draft"));
//
// build up variant list under this product
l_Variants = List();
m_Variant = Map();
m_Variant.put("weight",0.0);
m_Variant.put("title",input.Listing_Title);
m_Variant.put("sku",input.Product_SKU);
m_Variant.put("price",input.Shopify_Unit_Sell_Price);
m_Variant.put("taxable",input.Taxable);
m_Variant.put("inventory_quantity",input.Quantity.round(0)); // deprecated // use inventory_levels
m_Variant.put("inventory_policy","deny");
m_Variant.put("inventory_management","shopify");
m_Variant.put("inventoryItem",{"cost":ifnull(input.Shopify_Unit_Cost_Price,0).toDecimal().round(2).toString()}); // not working // use inventory_items
l_Variants.add(m_Variant);
m_Product.put("variants",l_Variants);
//
// do photos
if(input.Photos_Changed)
{
l_AllImages = List();
v_PositionIndex = 0;
for each v_ShopifyImageSrc in l_ShopifyPictures
{
if(v_ShopifyImageSrc != "")
{
v_PositionIndex = v_PositionIndex + 1;
r_DownloadPhoto = invokeurl
[
url :v_ShopifyImageSrc
type :GET
];
r_DownloadPhoto.setParamName("file");
v_EncodedImg = zoho.encryption.base64Encode(r_DownloadPhoto);
//
// build photos request
m_Image = Map();
m_Image.put("position",v_PositionIndex);
m_Image.put("attachment",v_EncodedImg);
l_AllImages.add(m_Image);
}
}
m_Product.put("images",l_AllImages);
}
//
// shopify tags
l_Tags = input.Tags;
m_Product.put("tags",l_Tags);
//
// JSON product request
m_CreateRecord = Map();
m_CreateRecord.put("product",m_Product);
//
// send request to Shopify API
r_ShopifyConfig = API_Integration[Connection_Name == "Shopify API"];
if(r_ShopifyConfig.count() > 0)
{
v_ShopifyAPIVersion = r_ShopifyConfig.API_Version.toString();
v_ShopifyURL = "https://" + r_ShopifyConfig.Client_ID + ":" + r_ShopifyConfig.Client_Secret + "@" + r_ShopifyConfig.Shop_ID;
//
// if creating the product
if(b_CreateProduct)
{
v_Endpoint = v_ShopifyURL + "/admin/api/" + r_ShopifyConfig.API_Version + "/products.json";
r_NewProduct = invokeurl
[
url :v_Endpoint
type :POST
parameters:m_CreateRecord.toString()
headers:m_Header
];
r_Response = r_NewProduct.toMap();
if(!isnull(r_Response.get("product").get("id")))
{
v_ShopifyProductID = r_Response.get("product").get("id").toLong();
v_ShopifyHandle = r_Response.get("product").get("handle");
// here I'm just getting the first variant in the list under this product
v_ShopifyInventoryID = r_Response.get("product").get("variants").get(0).get("inventory_item_id").toLong();
v_ShopifyVariantID = r_Response.get("product").get("variants").get(0).get("id").toLong();
}
}
}
- // initialize
- m_Product = Map();
- b_CreateProduct = true;
- //
- // I have a field called Shopify Product ID which stores the... um... shopify product id...
- if(!isNull(input.Shopify_Product_ID))
- {
- m_Product.put("id",input.Shopify_Product_ID);
- b_CreateProduct = false;
- }
- //
- // title and HTML
- m_Product.put("title",input.Listing_Title);
- if(!isNull(input.Full_Shopify_HTML_Code))
- {
- m_Product.put("body_html",input.Full_Shopify_HTML_Code);
- }
- m_Product.put("vendor",input.Brand);
- m_Product.put("product_type",input.Item_Type);
- //
- // listings / channels
- m_Product.put("published_scope","global");
- m_Product.put("published",true);  // set to true when comfortable with it publishing correctly
- m_Product.put("status",ifnull(input.Publish_Status.toLowerCase(),"draft"));
- //
- // build up variant list under this product
- l_Variants = List();
- m_Variant = Map();
- m_Variant.put("weight",0.0);
- m_Variant.put("title",input.Listing_Title);
- m_Variant.put("sku",input.Product_SKU);
- m_Variant.put("price",input.Shopify_Unit_Sell_Price);
- m_Variant.put("taxable",input.Taxable);
- m_Variant.put("inventory_quantity",input.Quantity.round(0));  // deprecated // use inventory_levels
- m_Variant.put("inventory_policy","deny");
- m_Variant.put("inventory_management","shopify");
- m_Variant.put("inventoryItem",{"cost":ifnull(input.Shopify_Unit_Cost_Price,0).toDecimal().round(2).toString()}); // not working // use inventory_items
- l_Variants.add(m_Variant);
- m_Product.put("variants",l_Variants);
- //
- // do photos
- if(input.Photos_Changed)
- {
- l_AllImages = List();
- v_PositionIndex = 0;
- for each v_ShopifyImageSrc in l_ShopifyPictures
- {
- if(v_ShopifyImageSrc != "")
- {
- v_PositionIndex = v_PositionIndex + 1;
- r_DownloadPhoto = invokeUrl
- [
- url :v_ShopifyImageSrc
- type :GET
- ];
- r_DownloadPhoto.setParamName("file");
- v_EncodedImg = zoho.encryption.base64Encode(r_DownloadPhoto);
- //
- // build photos request
- m_Image = Map();
- m_Image.put("position",v_PositionIndex);
- m_Image.put("attachment",v_EncodedImg);
- l_AllImages.add(m_Image);
- }
- }
- m_Product.put("images",l_AllImages);
- }
- //
- // shopify tags
- l_Tags = input.Tags;
- m_Product.put("tags",l_Tags);
- //
- // JSON product request
- m_CreateRecord = Map();
- m_CreateRecord.put("product",m_Product);
- //
- // send request to Shopify API
- r_ShopifyConfig = API_Integration[Connection_Name == "Shopify API"];
- if(r_ShopifyConfig.count() > 0)
- {
- v_ShopifyAPIVersion = r_ShopifyConfig.API_Version.toString();
- v_ShopifyURL = "https://" + r_ShopifyConfig.Client_ID + ":" + r_ShopifyConfig.Client_Secret + "@" + r_ShopifyConfig.Shop_ID;
- //
- // if creating the product
- if(b_CreateProduct)
- {
- v_Endpoint = v_ShopifyURL + "/admin/api/" + r_ShopifyConfig.API_Version + "/products.json";
- r_NewProduct = invokeUrl
- [
- url :v_Endpoint
- type :POST
- parameters:m_CreateRecord.toString()
- headers:m_Header
- ];
- r_Response = r_NewProduct.toMap();
- if(!isnull(r_Response.get("product").get("id")))
- {
- v_ShopifyProductID = r_Response.get("product").get("id").toLong();
- v_ShopifyHandle = r_Response.get("product").get("handle");
- // here I'm just getting the first variant in the list under this product
- v_ShopifyInventoryID = r_Response.get("product").get("variants").get(0).get("inventory_item_id").toLong();
- v_ShopifyVariantID = r_Response.get("product").get("variants").get(0).get("id").toLong();
- }
- }
- }
Source(s)
- Zoho Deluge: Encode a file to base 64 format
- Shopify API Developer: Create a new Product Image
- Joellipman.com: Zoho Creator: Download File from ZohoCRM field type "File Upload" (not attachments)
- Joellipman.com: Zoho Deluge: Get Image Uploaded in Creator Form
- Joellipman.com: Zoho Deluge: Push Item to Shopify API
Category: Zoho :: Article: 779



Add comment