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 2022-06-27

ああ、バルクでなかった。

元の質問 : レコードコレクションを別のレコードコレクションに渡すor変更する場合について
2022-06-28の朝確認したら消して逃亡してましたな。かなり色々と質問に答えたのにこれだ...

How to clone records using Flows? One flow to clone them all! #Spring20Delight

単発の場合はこれが使えそう。

public class CloneRecordFlowActionClass {
    
    @InvocableMethod(label='Clone Record')
    public static FlowOutputParameters[] cloneRecord(FlowInputParameters[] input){
        FlowOutputParameters[] result = new FlowOutputParameters[]{};
        
        sObject s = input[0].recordToClone; // 入力レコードをsObject型変数にコピーする
        s.put('Id',NULL); //レコードの「Id」フィールドをNULLとして設定する
        insert s; //Inserting the sObject record
        
        FlowOutputParameters obj = new FlowOutputParameters();
        obj.clonedRecordId = (string)s.get('Id'); //挿入されたsObjectレコードIDをコピーして、フロー出力として返します
        result.add(obj);
        
        return result;
    }
    
    /* Input parameters for the Apex action */
    public class FlowInputParameters{
        @InvocableVariable(label='Record to Clone' required = true )
        public sObject recordToClone;
    }
    
    /* Output parameters of the Apex action */
    public class FlowOutputParameters{
        @InvocableVariable(label='Cloned Record ID')
        public string clonedRecordId;
    }

}

複数レコードように改造

public class CloneRecordFlowActionClass {
    
    @InvocableMethod(label='Clone Record')
    public static FlowOutputParameters[] cloneRecord(FlowInputParameters[] input){
        FlowOutputParameters[] result = new FlowOutputParameters[]{};
        
        Integer param_count = FlowInputParameters.size();
        List<Object> insetObjList = new List<Object>();
        for (Integer i = 0, i < param_count i++) {
           sObject s = input[i].recordToClone; // 入力レコードをsObject型変数にコピーする
           s.put('Id',NULL); //レコードの「Id」フィールドをNULLとして設定する
           insetObjList.add(s);
       }    
       Database.SaveResult[] srList = Database.insert(insetObjList, false);//Inserting the sObject record
        
       
       for (Database.SaveResult sr : srList) {
          FlowOutputParameters obj = new FlowOutputParameters();
          obj.clonedRecordId = sr.getId();//挿入されたsObjectレコードIDをコピーして、フロー出力として返します
          result.add(obj);
       }        
        
       return result;
    }
    
    /* Input parameters for the Apex action */
    public class FlowInputParameters{
        @InvocableVariable(label='Record to Clone' required = true )
        public sObject recordToClone;
    }
    
    /* Output parameters of the Apex action */
    public class FlowOutputParameters{
        @InvocableVariable(label='Cloned Record ID')
        public string clonedRecordId;
    }

}
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?