Below, I provide a simple programme. The programme is written in Python. With Python, a library “salesforce_api” is used. With this library, one is able to insert records, query records and delete records in Salesforce.
from salesforce_api import Salesforce
client = Salesforce(username='datamigrationuser@pension4you.nl.pensiond2', password='***', security_token='***',is_sandbox=True)
hx = client.sobjects.Contact.insert({'LastName': 'Example', 'Email': 'test@example.com'})
print(hx)
hx = client.sobjects.query("SELECT Id FROM Contact WHERE LastName='Example'")
hx = client.sobjects.Contact.delete(hx[0]['Id'])
print(hx)
Recently, I discovered a nice trick to check the Email addresses. It uses a check on the setup of Email addresses. They must contain an @ symbol. They must contain a dot etc. It runs as follows:
import re
hx = 'jaouadÂÂtaheri@hotmail.com'
if not re.match(r"^[A-Za-z0-9.+-]+@[A-Za-z0-9._-]+.[a-zA-Z]$", hx):
print(hx,' klopt niet Email adres ')
else:
print('Email ok**')
I wrote a Python programme to select rows that contain NULLs. Whenever a null was found, it is written to a separate file.
The programme uses data frames. This allows to treat data as matrix. Once it is interpreted as a matrix, one may inspect each row. If the row contains a NULL, it is written to an external file.
for i in range(ff.shape[0]): if ff.iloc[i,:].isnull().sum() > 0: dfObjNULL = dfObjNULL.append(ff.iloc[i,:]) else: dfObjnonNULL = dfObjnonNULL.append(ff.iloc[i,:])
dfObjNULL.to_csv (r’C:\Users\tmaanen\CloudStation\Pensioenfonds\moeilijk\ffNULL.csv’, index = False, header=True, sep=’;’) dfObjnonNULL.to_csv (r’C:\Users\tmaanen\CloudStation\Pensioenfonds\moeilijk\ffnonNULL.csv’, index = False, header=True, sep=’;’)
The idea is rather straightforward. First a file is written. The content is stored in a data frame. The data frame is then inspected row by row. If a NULL is detected the row is appended to a data frame that is set up to store the rows with NULLs. At the end, the data frame is written to a CSV file.
I recently added a cgi-bin programme to my website (see archief.van-maanen.com/cgi-bin/test.py. It allows me to add functionality to the website that cannot be achieved with standard html.
Most web servers have the capability to add cgi-bin functionality to its website. I have a apache web server and I had to tweak the web server to allow cgi programmes. Tweaking the web server is done by making changes to the httpd.conf.
Let me first list the changes to the httpd.conf file
Change the #AddHandler cgi-script .cgi line to AddHandler cgi-script .cgi .py The additional .py refers to the extension of the programmes that will be added to the website
Change the line Options Indexes FollowSymLinks into Options Indexes FollowSymLinks ExecCGI
Adjust the section that contains the properties of the directory where cgi-bin programmes will be stored: In my case Directory “C:\Users\tomva\CloudStation\htdocs\cgi-bin”. There I added SetHandler cgi-script and Options ExecCGI. This sets the security rules on the directory. If these are not set correctly, it is forbidden to access these files. One then receives the 403 error.
Restart the web server
I use Python to write programmes. This programme starts with these lines:
I worked with Salesforce in recent period. A nice feature of this application is the possibility to send requests via the http protocol. Doing so, one may add data to this system.
How does it work? Generally speaking, one sends a signal to the system, upon which the system reacts. In my case, data are added to the system. The system also sends a signal in return. This signal may reveal whether the data are correctly added to the system.
The idea is a python programme is written that send requests. The signals that are returned are interpreted within this programme. Elements from the signals are stored as variables. This allows to create a sequence of requests and answers. Let us follow the programme logic.
Step1: Send the UID and PSW with this request: req = urllib.request.Request(url, data, headers). This returns a sessionId. This sessionId acts as key for further work.
Step2: Access a particular object with the sessionId that we just acquired. The sessionId is stored in the header, whereas the object is stored in the data. The request looks as: urllib.request.Request(url, data, headers). This returns a jobId that can be used in further work.
Step3: Add the data to the object. The jobId is stored in the header; the actual data that will be added are stored in the data within the request. The request looks as: urllib.request.Request(url, data, headers). This returns a batchId. This batchId can be used later to check if the job went according to the wishes.
Step4: Close the job with urllib.request.Request(url, data, headers) with the sessionId in the header and the command close_job in the data.
Step5: allows to check the outcome of the batch with urllib.request.Request(url,headers= headers) with the batchid in the header.
Each time, a signal is returned. Quite often, an XML is returned. I investigated this XML as a string whereby I checked on the occurrence of a particular tag. I knew between a tag and end tag an element can be found that can be retrieved.
Salesforce is a CRM application that is build upon a set of data. These data may refer to customers, but is is possible to create your own data structures.
These data structures have two types of field that are used to create the relation ships.
The first type can be seen in object (Boek__c) that I created:
Look at Titel__c that has a data type External ID. That is something like a primary key that is used to uniquely identify a record. This then implies that we could load the Titel__c fields with unique data that we may use to find a particular record.
We also have another table (Hoofdstuk__c) that has next fields:
Look at the field Boek__c that acts a foreign key to table Boek__c. The data type is called Master-Detail that indicates that this field is used as a linkage to another table.
If we look at the details of this field, we notice that relationship is called “Hoofdstukken”:
We load these tables by first loading the master tables. In this case, the master table is Boek__c that contains these data:
Data like “Winnetou, Old Shatterhand etc are entered by the user. The ID is generated by the Salesforce system, but is important to know. We must load the detail records with these ID data. As an example, look at the load file for Hoofdstuk__c:
"BOEK__C","INHOUD__C"
"a003X0000162hobQAA","1. Het pistool"
"a003X0000162hobQAA","2. Het geweer"
"a003X0000162hocQAA","1. De zuster"
Boek__c acted as the master-detail data type in Hoofdstuk__c. We must use the Ids from Boek__c.Id to load these. This implies that such Ids must be retrieved and subsequently be used as entry values for Boek__c in the Hoofdstuk__c table.
We can then retrieve full information within the developper console with this query:
select Boek__c.Id, Boek__c.Titel__C,(select Hoofdstuk__c.Inhoud__c from Hoofdstukken__r) from Boek__c
Note that we use the name from the relationship (“Hoofdstukken”) to retrieve the values from the detail table. The result then provides:
The values from the master table are provided in plain text, whereas the values from the detail tables are provided as json texts.
It is possible to trap errors in the loading process. This is handy when we work on a migration project. We simply load a table and trap all possible errors.
Let us use a simple table (emp_c) that must be loaded. However, one field is too narrow which loads to errors. These error will be trapped in table err$_emp_c. When loading the table, an option is added that leads errors to this table.
drop table emp_c;
create table emp_c
( employee_id number,
ename varchar2(17)
);
exec dbms_errlog.create_error_log('emp_c');
desc err$_emp_c;
insert into emp_c
select employee_id , first_name||' '||last_name name
from hr.employees
log errors reject limit unlimited;
select * from err$_emp_c;
truncate table err$_emp_c;
After loading, we may investigate the errors with a simple select statement.
Oracle has a use that may act as an example user with a series of tables. This example can be used to get acquainted with oracle SQL or data modelling in general.
However, as the code is simple, it comes at a price: the Vigenere cipher is rather insecure. If the key length is equal to the cipher text length then the cipher is absolutely secure if the key is chosen completely randomly and is only used once. This will turn the cipher into a one time pad.