+ Start a Discussion
Sunny_SlpSunny_Slp 

Exposing Apex methods as web services Question

Hi Everyone, I'm trying to understand how to expose Apex methods as webservices using webservice keyword.

 

Here is what I understood, after creating a web service method in an apex class, I generate the WSDL for that class and hand it over to the external application developer.

 

What I don't understand is, will the external developer be required to call login() method ? ( as we do in web API)

 

consider the following class:

 

global class MyWebService {
    webService static Id makeContact(String lastName, Account a) {
        Contact c = new Contact(lastName = 'Weissman', AccountId = a.Id);
        insert c;
        return c.id;
    }
}

 

how do we call MyWebService.makeContact method? do we have to use connection/binding ? ( as we do in web API)

 

Finally , can we use any of the web API calls (like retrieve() ) within the client (when it only has WSDL from this particular class) ?

 

Any help will be greatly appreciated.

 

Thank you,

 

Sunny_Slp

Best Answer chosen by Admin (Salesforce Developers) 
NasipuriNasipuri

Hi,

In force.com, you need to first do a login call to authenticate yourself.

Otherwise system will not be able to locate your ORG.

 

Please use Force.com partner or enterprise wsdl (Setup –Develop --API) in conjunction with the custom wsdl that you have developed from a web services APEX method to establish the login.

Once you establish a session you can directly call your webservices method.

 

But the example of the method created here is a bad example as for doing that you alraday have create method in the Force.com web services API.

Thanks and Regards,

Dinesh Nasipuri

dinesh.nasipuri@gmail.com

 

All Answers

NasipuriNasipuri

Hi,

In force.com, you need to first do a login call to authenticate yourself.

Otherwise system will not be able to locate your ORG.

 

Please use Force.com partner or enterprise wsdl (Setup –Develop --API) in conjunction with the custom wsdl that you have developed from a web services APEX method to establish the login.

Once you establish a session you can directly call your webservices method.

 

But the example of the method created here is a bad example as for doing that you alraday have create method in the Force.com web services API.

Thanks and Regards,

Dinesh Nasipuri

dinesh.nasipuri@gmail.com

 

This was selected as the best answer
Sunny_SlpSunny_Slp

Thanks for the fast reply Nasipuri.   I'll try to use Enterprise WSDL and the custom WSDL together.

 

Yes. That example might be a bad one. My actual requirement is I have a custom object RFD with a field Status. The external application should change the field status on RFD depending on its own events.

 

I was thinking of writing an apex method as webservice, so that all they(external app) needed to do was call that method and pass in a string and my apex method (webservice) will change status field on SFDC. I wanted to do this as I though it might simplify the external application side code.

 

But from your answer, it doesn't look as simple as I thought. Do you think for my requirement, its better to go with web API or a custom webservice method?

NasipuriNasipuri

Yes , For your Case the Custom web servic method will add value .

 

You can design the method good way to take a String/number to identify the unique RFD record and take another paramater , updated ststus.

Sunny_SlpSunny_Slp

I'll try and do that. Can you link to an example( to use webservice method, i.e custom WSDL and Enterprise together)? I tried the documentation but couldn't find client side implementation on it.

 

Thanks a lot for your help.

NasipuriNasipuri

If you are using JAVA then the code will be like below

 

/* ExposeAccount is the APEX CLASS Name

* loginResult is the SFDC Login result , that we get through a successfull login call to SFDC

 */

ExposeAccountBinding = (ExposeAccountBindingStub)new ExposeAccountServiceLocator().getexposeAccount(); com.sforce.soap.schemas._class.exposeAccount.SessionHeader sh1 = new com.sforce.soap.schemas._class.exposeAccount.SessionHeader(); sh1.setSessionId(loginResult.getSessionId());

 

ExposeAccountBinding.setHeader( new ExposeAccountServiceLocator().getServiceName().getNamespaceURI(), "SessionHeader", sh1); //Develop a method to call your web service method through ExposeAccountBinding ExposeAccountBinding.METHOD_NAME(METHOD PARAM/s) ; __________________________________________________________

But need to have some experience of Web service client.

 

Thanks and Regards,

Dinesh Nasipuri

dinesh.nasipuri@gmail.com

 

Sunny_SlpSunny_Slp

Thank you.