{"id":3357,"date":"2020-07-08T22:36:49","date_gmt":"2020-07-08T20:36:49","guid":{"rendered":"http:\/\/van-maanen.com\/?p=3357"},"modified":"2020-07-08T22:36:49","modified_gmt":"2020-07-08T20:36:49","slug":"adding-data-to-salesforce","status":"publish","type":"post","link":"http:\/\/archief.van-maanen.com\/?p=3357","title":{"rendered":"Adding data to Salesforce"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Below, I provide a simple Python programme which allows to add data from an external table to Salesforce. The programme uses simple_salesforce  as a library. In the first few lines, the necessary libraries are imported.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from simple_salesforce import Salesforce<br>import pyodbc<br>import pandas as pd<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then the connection to Salesforce is created.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sf = Salesforce(username='datamigrationuser@pension4you.nl.pensiond2', password='password', security_token='security',domain='test')<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Whatever is found in Salesforce is subsequently removed:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">queryOutput = sf.query_all(\"Select Id, code__c, beschrijving__c from SALESFORCEBAK__C\")<br>queryOutputDF = pd.DataFrame(queryOutput['records'])<br>print('Number of rows in salesforce NW',queryOutputDF.shape)<br>for i in range(queryOutputDF.shape[0]):<br>rec = sf.SALESFORCEBAK__C.delete(str(queryOutputDF.iloc[i,1]))<br>if rec != 204:<br>print(rec)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Records from an external table are read.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">conn = pyodbc.connect('DSN=AzureSQL;UID=tomvanmaanen;PWD=password')\ncursor = conn.cursor()\nsql = 'SELECT top(100) [code__c],[beschrijving__c] FROM [KONTROLE].[SALESFORCEBAK__C]'\ndf = pd.read_sql(sql, conn)\ncursor.close()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The data are then added to Salesforce.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for i in range(df.shape[0]):<br>hx1 = str(df.iloc[i,0])<br>hx2 = df.iloc[i,1]<br>rec = sf.SALESFORCEBAK__C.create({'code__c' : hx1, 'beschrijving__c' : hx2 })<br>if rec['success'] != True:<br>print(rec)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The programme terminates with a check on the number of records that are found in the table.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">queryOutput = sf.query_all(\"Select count(Id) from SALESFORCEBAK__C\")<br>queryOutputDF = pd.DataFrame(queryOutput['records'])<br>print('Number of rows in salesforce NW ',queryOutputDF.iloc[0,1],' &lt;&lt;&lt;&lt;')<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Below, I provide a simple Python programme which allows to add data from an external table to Salesforce. The programme uses simple_salesforce as a library. In the first few lines, the necessary libraries are imported. from simple_salesforce import Salesforceimport pyodbcimport pandas as pd Then the connection to Salesforce is created. sf = Salesforce(username=&#8217;datamigrationuser@pension4you.nl.pensiond2&#8242;, password=&#8217;password&#8217;, security_token=&#8217;security&#8217;,domain=&#8217;test&#8217;) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-3357","post","type-post","status-publish","format-standard","hentry","category-allgemein"],"_links":{"self":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/3357","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3357"}],"version-history":[{"count":0,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/3357\/revisions"}],"wp:attachment":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3357"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}