function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
CodeTalkerCodeTalker 

Help with using APEX package in a Java application

 
Hello,
 
I am having trouble calling a package created in Apex. I downloaded the WSDL and also generated the classes using Axis. I used the example from the  Salesforce Apex Language Reference pdf called "myPackage." (I modified it just a little.)
 
package myPackage {
webService Id makeContact(String lastName) {
Contact c = new Contact(lastName = 'Apex', firstName = 'Test');
insert c;
commit;
return c.id;
}
}
 
I now have 4 classes:
MyPackageBindingStub
MyPackageService
MyPackageServiceLocator
MyPackagePortType
 
Can someone show me how to relate these classes together to be able to call the package method.
I've tried logging in to salesforce using a bean, and then putting its session id into a new session header for the stub. When I go to call the method I get an error "No operation for this request."
 

MyPackageService service = new MyPackageServiceLocator();

MyPackageBindingStub bind = new MyPackageBindingStub();

SessionHeader sh = new SessionHeader(bean.getSessionId());

bind.setHeader(service.getServiceName().getNamespaceURI(), "SessionHeader", sh);

bind._setProperty(MyPackageBindingStub.ENDPOINT_ADDRESS_PROPERTY, bean.getLoginResult().getServerUrl());

String id = bind.makeContact("APEX test");

 
 
Does anyone have an example for calling a package? Thank you!
 
 
 
CodeTalkerCodeTalker
I think I found the answer...
 

SessionHeader sh = new SessionHeader(bean.getSessionId());

URL packageURL = new URL("https://na4-api.salesforce.com/services/Soap/package/myPackage");

MyPackageService service = new MyPackageServiceLocator();

MyPackageBindingStub stub = new MyPackageBindingStub(packageURL, service);

String s = service.getServiceName().getNamespaceURI();

stub.setHeader(s, "SessionHeader", sh);

stub.makeContact("ApexText");

SuperfellSuperfell
You should build the URL from the combination of the serverUrl from login and from the URL from the WSDL (take the host from loginResult serverUrl).
CodeTalkerCodeTalker

Thank you for the tip, you must have known that I didn't want to hardcode that url.

I think I found a way to get it without having to combine them.

The  getMyPackageAddress() method returns it.

 

MyPackageService service = new MyPackageServiceLocator();

String packAddr = service.getmyPackageAddress();

URL packageURL = new URL(packAddr);

MyPackageBindingStub stub = new MyPackageBindingStub(packageURL, service);

...