0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Apex_一番最上位の取引先名を取得

Last updated at Posted at 2025-01-01

Apexで取引先階層の一番上の取引先名を、再帰的に取得するコードだよ!


まずは、完成形サンプルコードだよ!

getTopParentAccountName.method
public String getTopParentAccountName(Id accountId) {
    Account acc = [SELECT Id, Name, ParentId FROM Account WHERE Id = :accountId];
    while(acc.ParentId != null) {
        acc = [SELECT Id, Name, ParentId FROM Account WHERE Id = :acc.ParentId];
    }
    return acc.Name;
}

コード解説

引数の取引先ID(基準)から親Accountを辿り、その親AccountのIdを持って、更に上の親Accountを取得しに行くよ! 最終的に最上位のAccountのNameを返すよ! 説明なんか不要な気がするけど。。。



なにか力になれれば嬉しいな!

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?