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
kosukekosuke 

初歩的な質問ですが。。。

あるオブジェクトにデータが挿入されたら、参照関係の親オブジェクトの項目の値を
更新するというトリガを作りたいのですが、
テストクラスを作成してもうまく動作してくれません。
原因が中々掴めずにいます。。。ご教授いただければと思います。

開発環境
 Eclipse  4.2.2
api version 29.0
です。

コードは以下のとおりです。
apexトリガ
trigger SyousaiCreate on AAAA__c (after insert) {
  List<AAAA__c> values = Trigger.new;
  AAAA__c  value = values[0];
  List<BBBB__c> InsertDatas = [SELECT Id, total_money__c  from BBBB__c where Id = :value.Id];
  BBBB__c InsertData = null;
  if(InsertDatas.size() > 0){
    InsertData = InsertDatas[0];
    InsertData.total_money__c = InsertData.total_money__c + value.money;
    Update(InsertDatas);
  }
}

テストクラス
@isTest
private class testinsertSyosai {
  static testmethod void testclass(){
    AAAA__c InsertData_Syosai = new AAAA__c(BBBB_Relation__c = 'a001000000XXXXX' ,money__c = 5000);
    insert(InsertData_Syosai);
  }
}

※ BBBB_Relation__c が参照項目となっており、データは存在することを確認しています。

AshleyMorrisAshleyMorris
I had to use google translate to read your question, so I aplogise if I have your question wrong.

You are assuming that the objects AAAA__c and BBBB__c have the same ID when performing the select.  Instead, you need to reference them as parent/child.  I am not sure which is the parent or child, but you will want the query to look something like this;

assuming AAAA__c is the parent object and is referenced using the same name through the BBBB__c object :

List<BBBB__c> InsertDatas = [SELECT Id, total_money__c  from BBBB__c where AAAA__c = :value.id]