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?

More than 1 year has passed since last update.

更新/追加されたレコードを含めないで親子関係のレコードを処理する

Last updated at Posted at 2021-05-07

Answersの質問に対する回答(ちゃんと動くApexコードの提供)

今回は私にとってはかなりの力作(2つのトリガ)。おまけにTest Classまでつけちゃう大サービス(というよりロジックの検証用ですがね)

ただし、回答者さんは面白くなかったのか回答に対しては無反応?いやいや無視なんですな。
きっと、質問したけど無理と言われましたが期待した答えだったのかもしれませんね。

まぁ。慣れっ子です。質問しておいて回答されると怒る人や、回答を得たら速攻で質問ごと消してなかったことにする人、
一番厄介なのは、気に入った回答を得られるまでは、何度も同じ投稿を繰り返す人など世界は日本と違うなぁって思わされることが多いです。

そうそう、みんながそういうわけでないですけどね。大半方々はちゃんとお礼の返信をくれますよ。
(しかし密かにブラックリストは作っています、2度とそういう質問者に捕まらないようにね)

と、前置きは長くなりましたが、日の目を見なかったのでここに記録しておきます。

Account trigger

trigger insertAccount on Account (after insert) {
	
    Set<Id> AccountIdSet = new Set<Id>();
    List<Account> updateAccountList = new List<Account>();
    List<Contact> insertContactList = new List<Contact>();
    if (Trigger.isInsert && Trigger.isAfter) {
        for (Account ac: Trigger.new) {
            Contact c = new Contact();
            c.AccountId = ac.Id;
            c.FirstName = 'Info';
            c.LastName = 'Default';
            c.email ='info@websitedomain.xxx ';
            c.Only_Default_contact__c = true;
            c.isPrimary__c = true;
            insertContactList.add(c);
            AccountIdSet.add(ac.Id);
        }//end of for        
        if (insertContactList.size() >0) insert insertContactList;
        
        List<Contact> conList = [SELECT Id,AccountId FROM Contact WHERE AccountId =: AccountIdSet];
        for (Contact con : conList){
            Account ac = new Account();
            ac.Id = con.AccountId;
            ac.primary_contact__c = con.Id;
            updateAccountList.add(ac);
        }//end of for
        if (updateAccountList.size() >0) update updateAccountList;
    }
}

Contact trigger

trigger updateContact on Contact (after insert,after update) {

    Map<Id, Contact> conOldMap = null;
    Map<Id, Contact> conNewMap = new Map<Id, Contact>(Trigger.new);
    if (Trigger.old != null) {
        conOldMap = new Map<Id, Contact>(Trigger.old);        
    } else {
        conOldMap = new Map<Id, Contact>();
    }
    
    
    Set<Id> AccountIdSet = new Set<Id>();
    if (Trigger.isInsert && Trigger.isAfter) {
        for (Contact c: Trigger.new) {
			AccountIdSet.add(c.AccountId);            
        }//end of for
        List<Contact> conList = [SELECT Id FROM Contact WHERE AccountId =: AccountIdSet];
        
        List<Contact> updateContactList =new List<Contact>();
        for (Contact con : conList){
            if (conNewMap.get(con.Id) == null){
                Contact c = new Contact(); 
                c.Id = con.Id;
                c.Only_Default_contact__c = false;
                updateContactList.add(c);
            }      
        }//end of for
        if (updateContactList.size() >0) update updateContactList;
    }//end of if
    
    if (Trigger.isUpdate && Trigger.isAfter){
        Set<Id> AccountIdSet2 = new Set<Id>();
        Map<Id,Id> ContactAccountMap = new Map<Id,Id>();        
        for (Contact c: Trigger.new) {
            Contact oldCon = (Contact)conOldMap.get(c.Id);
            if (oldCon != null){
                if (c.isPrimary__c ==true && oldCon.isPrimary__c == false){
                    AccountIdSet2.add(c.AccountId);
                    ContactAccountMap.put(c.Id,c.AccountId);
                }
            }
        }//end of for
        List<Contact> conList2 = [SELECT Id FROM Contact WHERE AccountId =: AccountIdSet2];
        system.debug(Logginglevel.INFO,'-- conList2 !!------------------>>  '+ conList2 ); 
        
        List<Contact> updateContactList2 =new List<Contact>();
        for (Contact con : conList2){
            if (conNewMap.get(con.Id) == null){
                Contact c = new Contact(); 
                c.Id = con.Id;
                c.isPrimary__c = false;
                updateContactList2.add(c);
            }      
        }//end of for
        List<Account> updateAccountList2 =new List<Account>();
        for (Contact con : conList2){
            Id acId = (Id)ContactAccountMap.get(con.Id);
            if (acId != null){
                Account ac = new Account();
                ac.Id = acId;
                ac.primary_contact__c = con.Id;
                updateAccountList2.add(ac);
            }
        }//end of for
        
        if (updateContactList2.size() >0) update updateContactList2;
        if (updateAccountList2.size() >0) update updateAccountList2;
        
    }//end of if
    
}

test class

@isTest
public class AccountContactTest {
    
    static testMethod void  main01Test() {
        
        Test.startTest();
        
        //Step 1
        Account acc = new Account();
        acc.Name = 'test';
        insert acc;
        
        List<Account> accList = [SELECT Id,primary_contact__c FROM Account WHERE Id =: acc.Id];
        List<Contact> conList = [SELECT Id,FirstName,LastName,email,Only_Default_contact__c,isPrimary__c,AccountId FROM Contact WHERE AccountId =: acc.Id];
        
        system.debug(Logginglevel.INFO,'-- accList ------------------>>  '+ accList );   
        system.debug(Logginglevel.INFO,'-- conList ------------------>>  '+ conList );   
        
        System.assertEquals(accList.size(), 1);
        System.assertEquals(accList[0].primary_contact__c, conList[0].Id);
        
        System.assertEquals(conList.size(), 1);
        System.assertEquals(conList[0].FirstName,'Info');
        System.assertEquals(conList[0].LastName,'Default');
        System.assertEquals(conList[0].email,'info@websitedomain.xxx');
        System.assertEquals(conList[0].Only_Default_contact__c,True);
        System.assertEquals(conList[0].isPrimary__c,True);
        System.assertEquals(conList[0].AccountId,acc.Id);
        
        
        //Step 2
        Contact con = new Contact();
        con.FirstName = 'test';
        con.LastName = '01';
        con.AccountId = acc.Id;
        insert con;
        
        List<Contact> conList2 = [SELECT Id,FirstName,LastName,email,Only_Default_contact__c,isPrimary__c,AccountId FROM Contact WHERE Id =: conList[0].Id];
        system.debug(Logginglevel.INFO,'-- conList2 ------------------>>  '+ conList2 ); 
        System.assertEquals(conList2[0].Only_Default_contact__c,false);
        
        //Step 3
        con.isPrimary__c = true;
        update con;
        
        List<Account> accList3 = [SELECT Id,primary_contact__c FROM Account WHERE Id =: acc.Id];
        
        system.debug(Logginglevel.INFO,'-- accList3 ------------------>>  '+ accList3 );  
        System.assertEquals(accList3[0].primary_contact__c, con.Id);
        
        List<Contact> conList3 = [SELECT Id,FirstName,LastName,email,Only_Default_contact__c,isPrimary__c,AccountId FROM Contact WHERE Id =: conList[0].Id];
        system.debug(Logginglevel.INFO,'-- conList3 ------------------>>  '+ conList3 );   
        System.assertEquals(conList3[0].isPrimary__c,false);
        
        Test.stopTest();
    }

}

関連する資料

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?