Simple Outbound SOAP API using Apex in Salesforce

Simple Outbound SOAP API using Apex in Salesforce

1. Go to the below link.

http://ec.europa.eu/taxation_customs/vies/checkVatTestService.wsdl

2. Right click and save the XML.

3. Go to Apex Classes.

4. Click Generate From WSDL button.

5. Select the downloaded file from step 2.

6. Click Parse WSDL and Done.

7. Remove Test from the endpoint in the class which was generated.

Note:
Apex class – ecEuropaEuTaxudViesServicesCheckva

Sample Code:

Visualforce page:

<apex:page controller="TestingVAT">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:PageBlockSectionItem >
                    Enter Country Code<apex:inputText value="{!strCountryCode}"  />
                </apex:PageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    Enter VAT Number<apex:inputText value="{!strVATNum}"/>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Check VAT" action="{!checkVAT}" reRender="infoblock"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
        
    <apex:outputPanel id="infoblock">
        <apex:pageBlock rendered="{!NOT(ISBLANK(vatNumber))}" >
            <apex:pageBlockSection >
                <apex:pageBlockSectionItem >
                    Country Code
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    {!countryCode}
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    VAT Number
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    {!vatNumber}
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    Request Date
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    {!requestDate}
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    Valid
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    {!valid}
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    Name
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    {!name}
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    Address
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    {!address}
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:outputPanel>
</apex:page>

Apex Class:

public class TestingVAT {


    public String strCountryCode {get;set;}
    public String strVATNum {get;set;}
    public String countryCode {get;set;}
    public String vatNumber {get;set;}
    public Date requestDate {get;set;}
    public Boolean valid {get;set;}
    public String name {get;set;}
    public String address {get;set;}


    public TestingVAT() {
        strCountryCode = 'RO';
        strVATNum = '9311280';
    }
    
    public void checkVAT() {
        ecEuropaEuTaxudViesServicesCheckva.checkVatResponse_element res = new ecEuropaEuTaxudViesServicesCheckva.checkVatResponse_element();
        ecEuropaEuTaxudViesServicesCheckva.checkVatPort obj = new ecEuropaEuTaxudViesServicesCheckva.checkVatPort();
        res = obj.checkVat(strCountryCode, strVATNum);
        countryCode = res.countryCode;
        vatNumber = res.vatNumber;
        requestDate = res.requestDate;
        valid = res.valid;
        name = res.name;
        address = res.address;
    }
    
}

Output:

Leave a Reply