Java – Create Organization Account


package com.z2systems.cxfclient.samples.account;

import com.z2systems.schemas.account.*;
import com.z2systems.schemas.common.*;
import com.z2systems.schemas.neonws.AccountService;
import com.z2systems.schemas.neonws.Neonws;
import org.junit.Test;

import javax.xml.namespace.QName;
import java.net.MalformedURLException;
import java.net.URL;

public class CreateOrganizationAccount {

    private static final QName SERVICE_NAME = new QName("http://www.z2systems.com/schemas/neonws/", "Neonws");

    @Test
    public void createOrganizationAccountTest() {

        URL wsdlURL = null;
        try {
            wsdlURL = new URL("https://api.neoncrm.com/neonws/services/AccountService?wsdl");

        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        Neonws ss = new Neonws(wsdlURL, SERVICE_NAME);
        AccountService port = ss.getAccountPort();

        CreateOrganizationAccountRequest request = new CreateOrganizationAccountRequest();
        OrganizationAccount organizationAccount = new OrganizationAccount();

        // set organization name
        organizationAccount.setOrganizationName("ABCDEF Software limited.");

        //construct Primary contact.
        Contact contact = new Contact();
        contact.setFirstName("James");
        contact.setLastName("Jones");
        contact.setPrefix("Mr."); // available prefixes can be retrieved from common/listPrefixes

        CodeNamePair gender = new CodeNamePair(); //available genders can be retrieved from account/listGenders
        gender.setCode("M");
        contact.setGender(gender);

        // add address for "John Doe"
        Address address = new Address();
        address.setIsPrimaryAddress(Boolean.TRUE);
        address.setIsShippingAddress(Boolean.FALSE);

        IdNamePair addressType = new IdNamePair();
        addressType.setName("Home");
        address.setAddressType(addressType);

        address.setAddressLine1("unit 3, 26 Hercules Street");
        address.setCity("Chicago");

        //state code name pair can be retrieved by account/listStates
        CodeNamePair state = new CodeNamePair();
        state.setCode("IL");
        address.setState(state);

        //country id name pair can be retrieved by account/listCountries
        IdNamePair country = new IdNamePair();
        country.setId("1");
        address.setCountry(country);

        address.setZipCode("60601");

        AddressList addressList = new AddressList();
        addressList.getAddress().add(address);
        contact.setAddresses(addressList);
        organizationAccount.setPrimaryContact(contact);

        //construct custom field data. field id & optionIds can be retrieved from common/listCustomFields
        CustomFieldDataList customFieldDataList = new CustomFieldDataList();
        CustomFieldData customFieldData = new CustomFieldData();
        customFieldData.setFieldId("53");
        customFieldData.setFieldOptionId("16");
        customFieldDataList.getCustomFieldData().add(customFieldData);
        organizationAccount.setCustomFieldDataList(customFieldDataList);

        //choose organization types
        OrganizationTypeList organizationTypeList = new OrganizationTypeList();
        IdNamePair type1 = new IdNamePair();
        type1.setId("2");
        IdNamePair type2 = new IdNamePair();
        type2.setId("3");
        organizationTypeList.getOrganizationType().add(type1);
        organizationTypeList.getOrganizationType().add(type2);
        organizationAccount.setOrganizationTypes(organizationTypeList);

        request.setOrganizationAccount(organizationAccount);
        request.setUserSessionId("T1357615747419"); //user session Id retrieved from login
        CreateOrganizationAccountResponse response = port.createOrganizationAccount(request);

        if (response != null) {

            System.out.println(response.getOperationResult().toString());

            if (OperationResult.SUCCESS.equals(response.getOperationResult())) {
                System.out.println(response.getAccountId());
            } else {
                System.out.println(response.getErrors());
            }
        }

    }
}