Adding Emails and EmailLink records using SOAP web services.

2 minute read time.

Adding Companies and Persons with their associated email address is  a pretty easy thing to do in the SOAP web services. I have covered the adding companies with their child records in the article "Adding a Company Entity using the Web Service API".

And if you need to add an extra address to an existing company then this is not too difficulty either as I covered in the article "Add an Additional Address to an Existing Company and Person using SOAP Web Services".

But adding Email are not as easy because the Email Link table is not by default exposed to the SOAP web services and this is a hidden system table.

Before you can access this table it must be exposed so that new records can be added.

Because this example involves directly changing the meta data definitions in the database you will need to backup the database and test this thoroughly before using in a live system.

To expose the EmailLink table I ran the following SQL statement.

[code language="sql"]
update custom_tables
set bord_webservicetable = 'Y'
where bord_name = 'EmailLink';
[/sql]

I then restarted the Sage CRM server to ensure that the EmailLink table was now exposed to web services.

Below is the code that I used to insert a new email address for a company. Please look at the comment within the code as these will explain the actions that I took.

Part 1 - this adds the new Email address.

I used the simple add() method to insert the new record. The Email table is exposed by default to web services so can be expected to be seen by the locally cached version of the WSDL that I used in my C# project.

Part 2 - this is a second transaction that inserts the EmailLink record. I used the addrecord() method because I did want to have to refresh the WSDL within my development environment. The only change I wanted to make was to expose the EmailLink table to web services.

[code language="csharp"]
//Part 1
ewarebase[] CRMBase = new ewarebase[1];
email extraEmail = new email();
extraEmail.emailaddress = "[email protected]";
extraEmail.type = "Sales";
CRMBase[0] = extraEmail;
addresult CRMaddResult = CRMService.add("email", CRMBase);
crmid myID = (crmid)CRMaddResult.records[0];

//Part 2
//Assumes EmailLink has been exposed to web services but uses addrecord because the local wsdl has not been updated.
crmrecord[] myEmailLinkSet = new crmrecord[1];

crmrecord myEmailLinkRecord = new crmrecord();
myEmailLinkRecord.entityname = "emaillink";
myEmailLinkRecord.records = new recordfield[4];

//This identifies the email as belonging to an entity of type 'Company'. By default the value of bord_tableid in the custom_tables meta data is '5' for the company entity.
recordfield aEntityID = new recordfield();
aEntityID.name = "entityid";
aEntityID.value = "5";
aEntityID.type = crmrecordtype.integer;
aEntityID.typeSpecified = true;

//This is the id of the company that the email belongs to. This was obtained from the user interface of my program.
recordfield aRecordID = new recordfield();
aRecordID.name = "recordid";
aRecordID.value = textboxComp_Companyid.Text;
aRecordID.type = crmrecordtype.integer;
aRecordID.typeSpecified = true;

//This is type of email address.
recordfield aType = new recordfield();
aType.name = "type";
aType.value = "Sales";
aType.type = crmrecordtype.@string;
aType.typeSpecified = true;

//This is the ID of the newly created email address added in Part 1 above.
recordfield aEmailID = new recordfield();
aEmailID.name = "emailid";
aEmailID.value = myID.crmid1.ToString();
aEmailID.type = crmrecordtype.integer;
aEmailID.typeSpecified = true;

myEmailLinkRecord.records[0] = aEntityID;
myEmailLinkRecord.records[1] = aRecordID;
myEmailLinkRecord.records[2] = aType;
myEmailLinkRecord.records[3] = aEmailID;

myEmailLinkSet[0] = myEmailLinkRecord;
addresult aResult = CRMService.addrecord("emaillink", myEmailLinkSet);
[/code]