トリガー

①商談、フェーズが「06.書類のやり取り」で保存されたときに、商談の取引先に紐づけて開店準備を作成
②商談に紐づく活動の日付と内容を、開店準備の「活動履歴」（ロングテキストエリア）に入れる※ワークフローとトリガを同時に使用する際トリガを２回起動されるので、「プロセスビルダー - 商談フェーズ変更日時」が商談トリガと統合する
 
OpportunityTriggerrHandler：
public class OpportunityTriggerrHandler {
  /**
   *
   */  
public static void onBeforeUpsert(Opportunity[]　newOpportunities,Opportunity[] oldOpportunities) {
     
    List<Opening__c> openList = new List<Opening__c>();
     
    for (Integer i = 0; i < newOpportunities.size(); i++) {
      //新規登録の場合
      if (oldOpportunities == null){
        newOpportunities[i].StageChangedate__c = System.now();
        //商談フェーズが「06.書類のやり取り」で保存されたときに、商談の取引先に紐づけて開店準備を作成
        if (newOpportunities[i].StageName == '06.書類のやり取り'){
          Opening__c open = new Opening__c();
          open.Opening_Account__c = newOpportunities[i].AccountId;
          List<Account> accountList = DaoAccount.selectAccountNameFromAcc(newOpportunities[i].AccountId);
          for (Account account : accountList){
            open.Name = account.name + '_新店舗';
            system.debug('account.name：' + account.name);
          }
          //商談に紐づく活動の日付と内容を、開店準備の「活動履歴」に入れる
          List<Task> taskList = DaoTask.selectTaskByOpportunityId(newOpportunities[i].Id);
          List<Event> eventList = DaoEvent.selectEventByOpportunityId(newOpportunities[i].Id);
          String taskText = '';
          String eventText = '';
          String taskActivityDate = '';
          for (Task task : taskList){
            //活動の日付が空白の場合は、「不明日付」を設定
            if (task.ActivityDate != null){
              taskActivityDate = task.ActivityDate.format();
            } else{
              taskActivityDate = '不明日付';
            }
            String taskTextTemp = taskActivityDate + '  ' + task.Subject + '\n' + task.Field2__c + '\n\n';
            taskText += taskTextTemp;
          }
           
          for (Event event : eventList){
            String eventActivityDate = event.ActivityDate.format();
            String eventTextTemp = eventActivityDate + ' ' + event.Subject + '\n' + event.Field2__c + '\n\n';
            eventText += eventTextTemp;
          }
          open.Activityhistory__c = taskText + eventText;
          openList.add(open);
        }
      } else{
        //更新登録の場合
        if (newOpportunities[i].StageName != oldOpportunities[i].StageName){
          newOpportunities[i].StageChangedate__c = System.now();                     
        }
        //商談フェーズが「06.書類のやり取り」で保存されたときに、商談の取引先に紐づけて開店準備を作成
        if (newOpportunities[i].StageName == '06.書類のやり取り' && oldOpportunities[i].StageName != '06.書類のやり取り'){
          Opening__c open = new Opening__c();
          open.Opening_Account__c = newOpportunities[i].AccountId;
          List<Account> accountList = DaoAccount.selectAccountNameFromAcc(newOpportunities[i].AccountId);
          for (Account account : accountList){
            open.Name = account.name + '_新店舗';
            system.debug('account.name：' + account.name);
          }
          //商談に紐づく活動の日付と内容を、開店準備の「活動履歴」に入れる
          List<Task> taskList = DaoTask.selectTaskByOpportunityId(newOpportunities[i].Id);
          List<Event> eventList = DaoEvent.selectEventByOpportunityId(newOpportunities[i].Id);
          String taskText = '';
          String eventText = '';
          String taskActivityDate = '';
          for (Task task : taskList){
            //活動の日付が空白の場合は、「不明日付」を設定
            if (task.ActivityDate != null){
              taskActivityDate = task.ActivityDate.format();
            } else{
              taskActivityDate = '不明日付';
            }
            String taskTextTemp = taskActivityDate + '  ' + task.Subject + '\n' + task.Field2__c + '\n\n';
            taskText += taskTextTemp;
          }
           
          for (Event event : eventList){
            String eventActivityDate = event.ActivityDate.format();
            String eventTextTemp = eventActivityDate + ' ' + event.Subject + '\n' + event.Field2__c + '\n\n';
            eventText += eventTextTemp;
          }
          open.Activityhistory__c = taskText + eventText;
          openList.add(open);
        }  
      }
    }
    if (openList.isEmpty()) {
      return;
    }
    try {
      Database.insert(openList, false);
    } catch (Exception e) {
      System.debug('DB Insert Error: ' + e.getMessage());
    }
  }
}


OpportunityTrigger：
trigger OpportunityTrigger on Opportunity (before insert, before update) {
  if (Trigger.isInsert && Trigger.isBefore) {
    OpportunityTriggerrHandler.onBeforeUpsert(Trigger.new,null);
  }
  if (Trigger.isUpdate && Trigger.isBefore) {
    OpportunityTriggerrHandler.onBeforeUpsert(Trigger.new,Trigger.old);
  }
}