Friday 22 March 2013


SOAP Webservice
Create Apex Webservice
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
global class ApexSoapWebservice {
    webservice static List<Account> getMyAccounts(String strFilter) {
        String SOQL;
         
        SOQL = 'SELECT ID, Name FROM Account ';
        if (strFilter == null) {
            SOQL += 'LIMIT 10';
        } else {
            SOQL += 'WHERE ' + strFilter;
        }
        return Database.query(SOQL);
    }
    webservice static List<Contact> getContacts(Account anAccount) {
        return [SELECT ID, Name FROM Contact WHERE AccountID = :anAccount.ID];
    }
    webservice static void createAccountAndContacts(Account newAccount, List<Contact> newContacts) {
        insert newAccount;
        for (Contact c : newContacts) {
            c.AccountID = newAccount.ID;
        }
        insert newContacts;
    }
}
Get WSDLs
You must now obtain 2 WSDLs.

The first WSDL is for logging into Salesforce and it can be obtained from Setup > Develop > API. You may use the Enterprise WSDL or the Partner WSDL.

Note: If you are writing a webservice client to connect to different ORGs, you want to use the partner WSDL because it uses late binding and it is not aware (at compile time) of different sObjects that may exist on different detination ORGs. If you are creating a webservice client specific for one ORG where you know the exact configuration and which sObjects exist, then use the Enterprise WSDL.

The second WSDL contains the definition of the webservice yo just created in Apex, this one is generated from the class itself.

For this article, I am going to use the Enterprise WSDL.
Build webservice client in Visual Studio
These instructions were tested on Visual Studio 2008 Professional Edition, different versions may change slightly.
  1. Create a new project
  2. On the solution explorer, right click on "References" and select "Add Service Reference"
  3. On the "Add Service Reference" window, click the "Advanced..." button
  4. On the "Service Reference Settings" window, click on the "Add Web Reference..." button
  5. On "Add Web Reference" window, type the full path of the Enterprise WSDL downloaded previouslythe in the "URL:" dropdown, and click the "Go" button.
  6. On the "Web Reference name" textbox, you may set the name of the class that is going to be generated
  7. Click the "Add Reference" button
Your should see a screen that looks like this:
Repeat the same stpes for the Apex WSDL. I created two classes:
  • SFDCEnterpriseAPI: This is the Enterprise WSDL that we'll use for logging into the ORG 
  • ApexSoapWebservice: This is the Apex WSDL that we'll use for invoking the methods we wrote above
Now let's write the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Define the variables that will be used.
SFDCEnterpriseAPI.SforceService sfdc = new SFDCEnterpriseAPI.SforceService();
ApexSoapWebservice.ApexSoapWebserviceService apex = new ApexSoapWebservice.ApexSoapWebserviceService();
SFDCEnterpriseAPI.LoginResult lr = null;
 
// Login
try {
    lr = sfdc.login(tbUN.Text, tbPWD.Text);
} catch (System.Web.Services.Protocols.SoapException ex) {
    MessageBox.Show(ex.ToString());
    return;
}
 
// If it reached here, the connection was succesful, but the password may have expired.
if (!lr.passwordExpired) {
    // Set connection details for Enterprise WS
    sfdc.Url = lr.serverUrl;
    sfdc.SessionHeaderValue = new ApexSample.SFDCEnterpriseAPI.SessionHeader();
    sfdc.SessionHeaderValue.sessionId = lr.sessionId;
 
    // Set connection details for Apex WS
    // DO NOT SET THE apex.url, or you will get this error:
    // SoapHeaderException: No operation available for request. {http://soap.sforce.com/schemas/class/...
    apex.SessionHeaderValue = new ApexSoapWebservice.SessionHeader();
    apex.SessionHeaderValue.sessionId = lr.sessionId;
 
    // Now make the call to your methods
    ApexSample.ApexSoapWebservice.Account[] accounts = apex.getMyAccounts(null);
} else {
    Console.WriteLine("Your password is expired.");
    return;
}

0 comments:

Post a Comment

    Links