Below is some simple C# code that shows how to add a new Address to an existing company. The address is inserted and linked to the company and the default contact for that company.
Notes:
- The webservice object is called CRMbinding.
- The Company ID is assumed to be passed from a textBox control (textBoxSearch.Text) in a windows form.
- The Company entity is retrieved to obtain to primary person ID.
- The add() method for the Address will only create a single entry in the Address_Link table, therefore if the address is required to be associated with both the company and the person two records must be added.
private void buttonAddExtraAddress_Click(object sender, EventArgs e)
{
try
{
int intCompanyID = int.Parse(textBoxSearch.Text);
string strSQLWhereClause = "comp_companyid = '" + intCompanyID + "'";
queryentityresult CRMEntityResult = CRMbinding.queryentity(intCompanyID, "Company");
ewarebase CRMBase = CRMEntityResult.records;
//CRMCompany defined at Form level;
CRMCompany = (company)CRMBase;
address extraCompanyAddress = new address();
extraCompanyAddress.address1 = "AddressData1";
extraCompanyAddress.address2 = "AddressData2";
extraCompanyAddress.address3 = "AddressData3";
extraCompanyAddress.address4 = "AddressData4";
extraCompanyAddress.address5 = "AddressData5";
extraCompanyAddress.city = "London";
extraCompanyAddress.country = "UK";
extraCompanyAddress.postcode = "SW1 4LL";
extraCompanyAddress.companyid = intCompanyID;
extraCompanyAddress.companyidSpecified = true;
//Create a Copy of the Address for use with the Person
//A second copy needs to be entered as only one AddressLink record is created at a time.
address extraPersonAddress = new address();
extraPersonAddress.address1 = extraCompanyAddress.address1;
extraPersonAddress.address2 = extraCompanyAddress.address2;
extraPersonAddress.address3 = extraCompanyAddress.address3;
extraPersonAddress.address4 = extraCompanyAddress.address4;
extraPersonAddress.address5 = extraCompanyAddress.address5;
extraPersonAddress.city = extraCompanyAddress.city;
extraPersonAddress.country = extraCompanyAddress.country;
extraPersonAddress.postcode = extraCompanyAddress.postcode;
extraPersonAddress.companyid = extraCompanyAddress.companyid;
extraPersonAddress.companyidSpecified = extraCompanyAddress.companyidSpecified;
//These lines have the Address added to the person
extraPersonAddress.personid = CRMCompany.primarypersonid;
extraPersonAddress.personidSpecified = true;
ewarebase[] CRMAddressBase = new ewarebase[2];
CRMAddressBase[0] = extraCompanyAddress;
CRMAddressBase[1] = extraPersonAddress;
addresult CRMAddResult = CRMbinding.add("address", CRMAddressBase);
MessageBox.Show(CRMAddResult.records.Length.ToString()+" Addresses Added");
}
catch (Exception MyError)
{
MessageBox.Show(MyError.Message);
}
}

-
Robert Hurson
-
Cancel
-
Vote Up
0
Vote Down
-
-
Sign in to reply
-
More
-
Cancel
Comment-
Robert Hurson
-
Cancel
-
Vote Up
0
Vote Down
-
-
Sign in to reply
-
More
-
Cancel
Children