1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Salesforceからfreee APIを使ってみました2

Last updated at Posted at 2020-07-07

他に使ってみたクラスを以下に紹介します。

freee取引先の一覧を取得する

以下に、クラスの定義。

    public class address_attributes {
        public String zipcode;
        public Integer prefecture_code;
        public String street_name1;
        public String street_name2;
    }
    
    public class partner_bank_account_attributes {
        public String bank_name;
        public String bank_name_kana;
        public String bank_code;
        public String branch_name;
        public String branch_kana;
        public String branch_code;
        public String account_type;
        public String account_number;
        public String account_name;
        public String long_account_name;
    }
    
    public class partner_doc_setting_attributes{
        public String sending_method;
    }
    
    //freee取引先
    public class partner {
        public Integer id;            //freee__freeePartnerId__c
        public String code;           //freee__Code__c 外部キー
        public Integer company_id;    //freee__Company__c 注意
        public String name;           //
        public String shortcut1;      //freee__Shortcut1__c
        public String shortcut2;      //freee__Shortcut2__c     
        public String long_name;      //freee__LongName__c 
        public String name_kana;      //freee__NameKana__c
        public String default_title;  //
        public String phone;          //
        public String contact_name;   //
        public String email;          //
        public address_attributes address_attributes;
        public partner_doc_setting_attributes partner_doc_setting_attributes;
        public partner_bank_account_attributes partner_bank_account_attributes;        
    }

    public class partners {
        public List<partner> partners;
    }
    
    public class partner_get {
        public partner partner;
    }

簡単にJsonをcalass化する方法

https://trailblazers.salesforce.com/answers?id=9064S000000CjRZQA0
で紹介されています。以下のサイトです。
Welcome to JSON2Apex

試しにAPI リファレンスの勘定科目の一覧のJsonを使って変換します。

以下結果です。パーサーまで作ってくれます。


//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class JSON2Apex {

    public List<Account_items> account_items;

    public class Account_items {
        public Integer id;
        public String name;
        public String shortcut;
        public String shortcut_num;
        public Integer default_tax_id;
        public Integer default_tax_code;
        public String account_category;
        public Integer account_category_id;
        public List<String> categories;
        public Boolean available;
        public Integer walletable_id;
        public String group_name;
        public String corresponding_income_name;
        public Integer corresponding_income_id;
        public String corresponding_expense_name;
        public Integer corresponding_expense_id;
    }

    
    public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }
}

test クラスも作成してくれます。

//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//
@IsTest
public class JSON2Apex_Test {
    
    static testMethod void testParse() {
        String json = '{'+
        '  \"account_items\": ['+
        '    {'+
        '      \"id\": 1,'+
        '      \"name\": \"ソフトウェア\",'+
        '      \"shortcut\": \"SOFUTO\",'+
        '      \"shortcut_num\": \"123\",'+
        '      \"default_tax_id\": 34,'+
        '      \"default_tax_code\": 34,'+
        '      \"account_category\": \"現金・預金\",'+
        '      \"account_category_id\": 1,'+
        '      \"categories\": ['+
        '        \"資産\"'+
        '      ],'+
        '      \"available\": true,'+
        '      \"walletable_id\": 1,'+
        '      \"group_name\": \"売掛金\",'+
        '      \"corresponding_income_name\": \"売掛金\",'+
        '      \"corresponding_income_id\": 1,'+
        '      \"corresponding_expense_name\": \"買掛金\",'+
        '      \"corresponding_expense_id\": 1'+
        '    }'+
        '  ]'+
        '}';
        JSON2Apex obj = JSON2Apex.parse(json);
        System.assert(obj != null);
    }
}

以上で事前準備が完了です。
ここからは、本当にfreee APIを使ってやりたいことです。
freee APIを使ってやりたいこと1

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?