Dynamic CSV Import into SugarCRM via Azure Function

Thomas Dillinger Azure, Azure Function, Integration, News, Python, SugarCRM

Microsoft Azure is easy going – with only a few lines of code you can create a CSV importer for SugarCRM. Free of charge up to 1 Million requests per month!

First of all, you create an Azure Function.

python3 -m venv fellowdemo
source fellowdemo/bin/activate
func init sugarfunc --worker-runtime python

Choose Azure Blob Storage Trigger, then add the Blob service in the Azure Portal.

After creating the blob, you need to enter the key in the function.

Define the file in the function

The import script takes only a few lines:

import logging
import azure.functions as func
import os, io, crm, json
import pandas as pd
from sugarcrm.client import Client
def main(myblob: func.InputStream):
    logging.info(f"Python blob trigger function processed blob \n"
                 f"Name: {myblob.name}\n"
                 f"Blob Size: {myblob.length} bytes")
    file = myblob.read()
    csvf = io.BytesIO(file)    
    csvf = csvf.getvalue().decode('UTF-8')
    sniffer = csv.Sniffer()
    cnt = sniffer.sniff(csvf) 
    logging.info (cnt.delimiter) 
    df = pd.read_csv(io.BytesIO(file), sep=cnt.delimiter, dtype=str)
    client = Client('https://sg-python.demo.sugarcrm.eu', 'admin', '***')
    results = []
    results = json.loads(df.to_json(orient='records'))
    for item in results:
        #item['id'] = item['CONTRACT_ID']
        client.set_entry('Leads', item )

Create a simple CSV file to test the script.

Upload the file into the blob storage.

A second later, you see the file being processed:

An the records are created in the SUGARCRM!

Wow, having done a CSV import interface in a few lines of code!

More technical blogs: