ACF Library

ContaCreateProduct

Back to List

Description: Create a product in the Conta Accounting System

FileMaker Prototype:

Set Variable [$res; ACF_Run("ContaCreateProduct"; string_ProdPrimKey)]

Category: CONTA API

Dependencies

NOTE: This function serves as a reference template. To use it effectively, you may need to adjust certain parts of the code, such as field names, database structures, and specific data lists to fit the requirements of your system. This customization allows the function to work seamlessly within your environment.

Function source:

Function ContaCreateProduct(string ProdPrimKey)
   array string prodProductNo, prodProductName, prodVatCode, prodBookKepingAccount, prodIsActive;
   array float prodPrice;  
   array int prodContaProductID;
   
   string sql = "SELECT ProductNo, ProductName, VatCode,Price, BookKepingAccount, IsActive, ContaProductID
      FROM Products
      WHERE PrimaryKey = :ProdPrimKey
         
      INTO :prodProductNo, :prodProductName, :prodVatCode, :prodPrice, :prodBookKepingAccount, :prodIsActive, :prodContaProductID
      ";
   string res = ExecuteSQL ( sql ) ; 
   if ( sizeof ( prodProductNo) == 0) then 
      throw "Product does not exists, Primarykey " + ProdPrimKey; 
   end if
   
   // Create the JSON object
   int i = 1; 
   bool Active = (prodIsActive[i]=="Yes"); 
   array string ValidVATcodes = {"high", "medium", "low", "zero.rate", "exempted", "export"}; 
   bool b1 = AssertInArray(prodVatCode[i], ValidVATcodes, 
         prodProductName[i] + "ha an invalid VAT code '"+prodVatCode[i]+"', should be: " + implode (",", ValidVATcodes)); 
   string vat = "output."+prodVatCode[i]; 
   JSON prod;
   prod = JSON(
      "bookkeepingAccountNo", prodBookKepingAccount[i],
      "isActive", Active,
      "name", prodProductName[i],
      "price", prodPrice[i],
      "productNo", prodProductNo[i],
      "vatCode", vat
   );
   
   // Do the API request
   JSON apidata = getContaApiKeyandNumber(); 
   if ( apidata["APIkey"] == "") then
      alert ("No API key found"); 
      return "ERROR: Missing APIkey"; 
   end if
   
   string APIkey = apidata["APIkey"]; 
   bool production = (apidata["Production"]=="1");
   int compOrg = int(apidata["CompNo"]); 
   
   string hdr = 'apiKey:'+APIkey; 
   string url = getContaBaseURL(production)+"/invoice/organizations/"+compOrg+"/products";
   print "\n"+url; 
   string data = string(prod); 
   res = HTTP_POST ( url, data, hdr);
   print res;
   int contaProdID;
   // Verify the result- 
   JSON apiRes = res; 
   if (ContaVerifyAPIresponce(apiRes)) then
      contaProdID = int(apires["id"]); 
      print "ContaProductID = " + contaProdID; 
      // Update ContaProductID on the Product. 
      sql = "UPDATE Products SET ContaProductID = :contaProdID
      WHERE PrimaryKey = :ProdPrimKey"; 
      res = ExecuteSQL( sql ); 
      if ( res != "") then
         alert ( res ); 
         return "ERROR Update ContaProductID..."+res; 
      end if
   end if
   return "OK";
End

The ContaCreateProduct Function creates a product in the Conta accounting system using its API. The function pulls data from FileMaker's Products table, Craft the JSON used by the API, send it, and check the result of the operation. If success, it updates the products table with the conta product ID we later use in the invoicing.

Example

Set Variable [$res; ACF_Run("ContaCreateProduct"; Products::PrimaryKey)]

After that, the product is available inside Conta. Here is a screenshot from Conta after I sent a few sample products to the sandbox environment.

conta-products

Back to List