function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
pontaponta 

主従関係のテーブルで従のデータ取得方法

 

SForceOfficeToolkitLib3を使用してQueryでデータの取得をしています。
主のテーブルがtbA、従のテーブルがtbbとして
主のIdを選択し、従のデータを取得するにはどうすればいいでしょうか?
sql = "SELECT (select Name ,Id  From tbb ) FROM tba WHERE Id ='" & aid & "'"
上記のSQLですと実行時エラー '-2147467259(80004005)'が表示されます。
SOQLが使えるみたいですが上記の書き方は間違っていますか?

 

ikouikou

従オブジェクトを主オブジェクトと一緒に(または主の条件で絞って)取得する場合は、

従のオブジェクト名にRelationship nameを指定するようです。

 

tbbがカスタムオブジェクトなら__rが付いて、たぶん以下のようになると思います。

 

sql = "SELECT (select Name ,Id  From tbb__r ) FROM tba WHERE Id ='" & aid & "'"

 

標準オブジェクトの場合はオブジェクトごとにRelationship nameがあるみたいです。

取引先(Account)と取引先責任者(Contact)なら、Contactsという感じで。

 

詳しくはこちらのリファレンスを見てくださいませ。

 

 

pontaponta

回答ありがとうございます。

 

主従共にカスタムオブジェクトを使用しています。

 

従のテーブルtbbに子リレーション名としてRelation1と設定してあります

 

 

sql = "SELECT (select Name ,Id  From Relation1__r ) FROM tba WHERE Id ='" & aid & "'"

 上記のようにコード修正しましたが エラーが表示されます。

エラーの内容は下記のメッセージが表示されます

 

実行時エラー '-2147467259(80004005)'
sObject type 'Relation1__r)' is not supported. 
If you are attempting to use a custom object. be sure to append the '__c' after the entity name.
Please reference your WSDL or describe call for the appropriate names.

 

 

 

 

ikouikou

あれ・・・?

 

ちょっとWSDLで確認してみてください。

 

設定>開発>API>Enterprise WSDLの作成

 

 <complexType name="obj1__c">
- <complexContent>
- <extension base="ens:sObject">
- <sequence>
  <element name="ActivityHistories" nillable="true" minOccurs="0" type="tns:QueryResult" /> 
  <element name="Attachments" nillable="true" minOccurs="0" type="tns:QueryResult" /> 
  <element name="ConnectionReceived" nillable="true" minOccurs="0" type="ens:PartnerNetworkConnection" /> 
  <element name="ConnectionReceivedId" nillable="true" minOccurs="0" type="tns:ID" /> 
  <element name="ConnectionSent" nillable="true" minOccurs="0" type="ens:PartnerNetworkConnection" /> 
  <element name="ConnectionSentId" nillable="true" minOccurs="0" type="tns:ID" /> 
  <element name="CreatedBy" nillable="true" minOccurs="0" type="ens:User" /> 
  <element name="CreatedById" nillable="true" minOccurs="0" type="tns:ID" /> 
  <element name="CreatedDate" nillable="true" minOccurs="0" type="xsd:dateTime" /> 
  <element name="Events" nillable="true" minOccurs="0" type="tns:QueryResult" /> 
  <element name="FeedSubscriptionsForEntity" nillable="true" minOccurs="0" type="tns:QueryResult" /> 
  <element name="IsDeleted" nillable="true" minOccurs="0" type="xsd:boolean" /> 
  <element name="LastActivityDate" nillable="true" minOccurs="0" type="xsd:date" /> 
  <element name="LastModifiedBy" nillable="true" minOccurs="0" type="ens:User" /> 
  <element name="LastModifiedById" nillable="true" minOccurs="0" type="tns:ID" /> 
  <element name="LastModifiedDate" nillable="true" minOccurs="0" type="xsd:dateTime" /> 
  <element name="Name" nillable="true" minOccurs="0" type="xsd:string" /> 
  <element name="Notes" nillable="true" minOccurs="0" type="tns:QueryResult" /> 
  <element name="NotesAndAttachments" nillable="true" minOccurs="0" type="tns:QueryResult" /> 
  <element name="OpenActivities" nillable="true" minOccurs="0" type="tns:QueryResult" /> 
  <element name="Owner" nillable="true" minOccurs="0" type="ens:Name" /> 
  <element name="OwnerId" nillable="true" minOccurs="0" type="tns:ID" /> 
  <element name="ProcessInstances" nillable="true" minOccurs="0" type="tns:QueryResult" /> 
  <element name="ProcessSteps" nillable="true" minOccurs="0" type="tns:QueryResult" /> 
  <element name="Shares" nillable="true" minOccurs="0" type="tns:QueryResult" /> 
  <element name="SystemModstamp" nillable="true" minOccurs="0" type="xsd:dateTime" /> 
  <element name="Tags" nillable="true" minOccurs="0" type="tns:QueryResult" /> 
  <element name="Tasks" nillable="true" minOccurs="0" type="tns:QueryResult" /> 
  <element name="X1_Email__c" nillable="true" minOccurs="0" type="xsd:string" /> 
  <element name="obj3__r" nillable="true" minOccurs="0" type="tns:QueryResult" /> 
  <element name="val1_1__c" nillable="true" minOccurs="0" type="xsd:string" /> 
  </sequence>
  </extension>
  </complexContent>
  </complexType>

 

主オブジェクト(obj1__c)の要素としてxxxxx__rがいると思います。

pontaponta

設定>開発>API>Enterprise WSDLの作成

で確認してみました。

 

主オブジェクトに要素としてRelation1__rが存在しました。

 

 

<complexType name="tba__c">
−<complexContent>
−<extension base="ens:sObject">
−<sequence>
<element name="Relation1__r" nillable="true" minOccurs="0" 
type="tns:QueryResult"/>

 

 

 

ikouikou

ん~・・・ちょっと原因が分からないです。

 

もしかしたらSForceOfficeToolkitLib3ではこういう書き方がサポートされていないのかもしれません。

サポートするapiのバージョンの問題かな?

 

お役に立てず申し訳ないです。

pontaponta

ありがとうございます

 

SForceOfficeToolkitLib3ではサポートされていないのかな

別の方法を考えます