0

Below are logs which i got the error while creating contact in xero as i want to create multiple contact in xero i writing the api but i got the below error if i send the same request in postman it is working properly.

<pre>com.xero.api.XeroApiException: Bad Request com.xero.api.XeroApiExceptionHandler.execute(XeroApiExceptionHandler.java:27) com.xero.api.client.AccountingApi.createContact(AccountingApi.java:912) in.techchefs.anyspace.common.service.impl.XeroServiceImpl.addContactToXero(XeroServiceImpl.java:274) Below are code for calling the Contact API

public Contact addContactToXero(User user, String currencyCode) throws IOException {
        logger.info("inside xeroservise User addContactToXero---->>"+currencyCode);
        ApiClient client = new ApiClient();
        accountingApi = AccountingApi.getInstance(client);

        Contact contact = new Contact();
        contact.setIsCustomer(true);
        contact.setIsSupplier(true);
        String businessName = user.getFirstName() + " " + user.getLastName() + " " + user.getId();

        if (user.getBusinessInfo() != null && user.getBusinessInfo().getName() != null) {
            businessName = user.getBusinessInfo().getName();
        }
        contact.setName(businessName);
        contact.setFirstName(user.getFirstName());
        contact.setLastName(user.getLastName());
        contact.setContactNumber(user.getPhone());
        contact.setEmailAddress(user.getEmail());


        List<Address> arrayOfAddress = new ArrayList<Address>();

        Set<UserAddress> userAddresses = user.getAddress();
        for (Iterator iterator = userAddresses.iterator(); iterator.hasNext();) {
            UserAddress userAddress = (UserAddress) iterator.next();

            Address address = new Address();

            address.setAddressLine1(userAddress.getStreet());
            address.setAddressLine2(userAddress.getSuburb());
            address.setAddressType(userAddress.getAddressType() == 1 ? AddressTypeEnum.DELIVERY : AddressTypeEnum.POBOX);
            address.setRegion(userAddress.getState());
            address.setPostalCode(userAddress.getPostalCode());
            address.setCountry(userAddress.getCountry() == null ? "" : userAddress.getCountry().getTitle());

            arrayOfAddress.add(address);
        }

        contact.setAddresses(arrayOfAddress);
        contact.setContactStatus(ContactStatusEnum.ACTIVE);
        logger.info("addContactToXero by USer contact--->>"+contact);
        String accessToken=getAccessToken(currencyCode);
        logger.info("addContactToXero by USer accessToken--->>"+accessToken);
        XeroTokenStorage token=tokenStorageService.getByCurrencode(currencyCode);
        logger.info("addContactToXero by USer token.getTenantId()--->>"+token.getTenantId());
        Contacts contatcsAdded = accountingApi.createContact(accessToken,token.getTenantId(),contact);
        List<Contact> contatcsAddedList = contatcsAdded.getContacts();

        return contatcsAddedList.get(0);
        //return contatcsAdded;
    }

I give the logger for contact

class Contact {
    contactID: null
    contactNumber: 6666
    accountNumber: null
    contactStatus: ACTIVE
    name: Marc Verano 1
    firstName: Marc
    lastName: Verano
    emailAddress: info@anyspaces.com
    skypeUserName: null
    contactPersons: []
    bankAccountDetails: null
    taxNumber: null
    accountsReceivableTaxType: null
    accountsPayableTaxType: null
    addresses: []
    phones: []
    isSupplier: true
    isCustomer: true
    defaultCurrency: null
    xeroNetworkKey: null
    salesDefaultAccountCode: null
    purchasesDefaultAccountCode: null
    salesTrackingCategories: []
    purchasesTrackingCategories: []
    trackingCategoryName: null
    trackingCategoryOption: null
    paymentTerms: null
    updatedDateUTC: null    
    contactGroups: []
    website: null
    brandingTheme: null
    batchPayments: null
    discount: null
    balances: null
    attachments: []
    hasAttachments: null
    validationErrors: []
    hasValidationErrors: null
}

i got the error from java is bad request and same request if i send through postman it is working. Pls tell me some solution on that

1 Answers1

0

There is a clue in your question - "if I send the same request through postman it is working" - contact name and number need to be unique, so check that you are providing unique values for these fields.

However as droopsnoot mentioned in a comment, any validation errors will be returned from the Xero API, so interrogate the response and you will probably find the reason it is occurring.

Dan
  • 1,489
  • 12
  • 16