[SalesForce] SOAP API: Applying filter to a retrieve data in Apex

I generated Apex code from the ExactTarget SOAP Api:

Exacttarget Wsdl URL

I am trying to make webservicecallouts using apex to retrieve "SentEvent" for an specific subscriber. Using SOAPUi it works absolutely fine, however, in Apex, I don't know how to set the filter (without the filter it works fine). Any ideas how to set the filter in Apex?

Here is the SOAP envelop I am trying to reproduce in Apex:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Header>
      <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
         <wsse:UsernameToken wsu:Id="UsernameToken-24440876" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <wsse:Username>myusername</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">MyPassword</wsse:Password>
         </wsse:UsernameToken>
      </wsse:Security>
   </soapenv:Header>
   <soapenv:Body>
      <RetrieveRequestMsg xmlns="http://exacttarget.com/wsdl/partnerAPI">
         <RetrieveRequest>
            <ObjectType>SentEvent</ObjectType>
            <Properties>Client.ID</Properties>
            <Properties>TriggeredSendDefinitionObjectID</Properties>
            <Properties>SendID</Properties>
            <Properties>SubscriberKey</Properties>
            <Properties>EventDate</Properties>
            <Properties>EventType</Properties>
            <Properties>BatchID</Properties>        
            <Filter xsi:type="ns1:SimpleFilterPart" xmlns:ns1="http://exacttarget.com/wsdl/partnerAPI">
               <Property>SubscriberKey</Property>
               <SimpleOperator>equals</SimpleOperator>
               <Value>003c000000bxyVeAAI</Value>
            </Filter>
         </RetrieveRequest>
      </RetrieveRequestMsg>
   </soapenv:Body>
</soapenv:Envelope>

Here is my code I implemented so far to generate the request in Apex (without the filter):

bemExacttargetComWsdlPartnerapi.Soap stub = new bemExacttargetComWsdlPartnerapi.Soap(username,password);
bemExacttargetComWsdlPartnerapi.RetrieveRequest request = = new bemExacttargetComWsdlPartnerapi.RetrieveRequest();
request.ObjectType = 'SentEvent';
List<String> properties = new List<String>{'SendID','SubscriberKey','EventDate','EventType'};
request.ObjectType = 'SentEvent';
request.Properties = properties;

You can use this link to generate Apex code from the Wsdl, the class generated is too big to paste it here:

FuseIT – Wsdl2Apex

Best Answer

The generated RetrieveRequest class has:

public exacttargetComWsdlPartnerapi.FilterPart Filter;

It also has:

public class SimpleFilterPart {
    public String Property;
    public String SimpleOperator;
    public String[] Value;
    public DateTime[] DateValue;
    // ...
}

The trick here is the WebserviceCallout.Invoke() can't handle inheritance in Apex classes.

Try changing the FilterPart member of RetrieveRequest to be a SimplePartFilter.

Then you should be able to call it with:

bemExacttargetComWsdlPartnerapi.Soap stub = new bemExacttargetComWsdlPartnerapi.Soap();
// You will need to construct the UsernameToken Security Header.
bemExacttargetComWsdlPartnerapi.RetrieveRequest request = = new bemExacttargetComWsdlPartnerapi.RetrieveRequest();
request.ObjectType = 'SentEvent';
request.Properties = new List<String>{'SendID','SubscriberKey','EventDate','EventType'};
request.Filter = new bemExacttargetComWsdlPartnerapi.SimpleFilterPart();
request.Filter.Property = 'SubscriberKey';
request.Filter.SimpleOperator = 'equals';
request.Filter.Value = new String[] {'003c000000bxyVeAAI'};

exacttargetComWsdlPartnerapi.RetrieveResponseMsg_element response = stub.Retrieve_x(request);