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.

ケースの複製のまとめ

Posted at

Apex: Clone a record, then make the new record the Parent of the old cloned Record

public static void cloneTransferCases(Map<Id,Case> oldMap,List<Case> newList){
    System.debug('MM++++ oldMap: ' + oldMap.keySet());
    System.debug('MM++++ oldMap Values: ' + oldMap.values());
    System.debug('MM++++ newList: ' + newList);
    Set<id> processedAbandonCloneRecords = new Set<Id>();
    Map<Id,Case> mapCaseToInsert = new Map<Id,Case>();

    for(Case cs : newList){
        // check to see if this record has been processed already in this transaction
        if(!processedAbandonCloneRecords.contains(cs.Id)){
            if(cs.Status == 'Abandoned'){
                if(cs.Abandoned_Reason__c == 'Transferred' && oldMap.get(cs.Id).Abandoned_Reason__c != 'Transferred'){
                    if(cs.Transfer_To__c != null && cs.ParentId == null){
                        // Create a temporary case using oldMap
                        // to get field values prior to Abandonment
                        Case tempCase = oldMap.get(cs.Id).clone();
                        // set owner of new case to match the Transfer To user
                        tempCase.OwnerId = cs.Transfer_To__c;
                        tempCase.ParentId = null;
                        mapCaseToInsert.put(cs.id,tempCase);

                        // mark this record as processed in this transaction
                        processedAbandonCloneRecords.add(cs.Id);

                        // if we are running tests, insert and update will
                        // occur in same transaction so we should actually
                        // not stop cascade
                        if(Test.isRunningTest()){
                            processedAbandonCloneRecords.remove(cs.Id);
                        }
                    }
                }
            }
        }
    }

    if(!mapCaseToInsert.isEmpty()){
        insert mapCaseToInsert.values();
    }

    for(Case cs : newList){
        if(mapCaseToInsert.containsKey(cs.id)) {
            cs.ParentId = mapCaseToInsert.get(cs.id).id;
        }
    }
}
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?