-2

I have a scenario.

Workstream(child OBject) is in MD relationship with Case.So a case can have multiple related workstream records.

Use case is when a case status is changed to closed, I need to create a new case with status NEW and create clone of these workstream records and assign to the newly created Case record.

All i need to do it in a Case trigger after update.

I did search, not able to find any relevant logic. Any directions or sample links would be great.. Thanks,

2 Answers 2

2

You can create a record of parent and child object records in one go by using Reference and External Id.

  1. Create a String field(External) on case object for example:
  2. Query Case Object record with Workstream Child object records
  3. Loop on query
  4. Create a new instance reference on case object and assign caseId+'abc'
  5. A loop on child object records(Workstream)
  6. Clone the child object record by using a clone method
  7. Set the parent field equal to the reference of case instance
  8. Add into the list
  9. Once the child object list is prepared, create clone instance of case and set same external id field and value = caseId+'abc'
  10. Add into list
  11. Add both lists into one list
  12. insert lists and delete the above-selected case list records

Here is a reference: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_foreign_keys.htm

Here is an example of code, I have done for other objects

String recordId = 'somerecord'; // if we want for individual record
    Datetime yesterdayDateTime = Datetime.now().addDays(-1);
String childObjectName = 'LiveChatTranscriptEvent';
        Set<String> childFieldApiList = Schema.getGlobalDescribe().get(childObjectName).getDescribe().fields.getMap().keySet();
        String chileQuery = 'SELECT ' + String.join(new List<String>(childFieldApiList), ',');
        chileQuery += ' FROM LiveChatTranscriptEvents';

        String objectName = 'LiveChatTranscript';
        Set<String> fieldApiList = Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap().keySet();
        fieldApiList.remove('chatkey');
        String query = 'SELECT ' + String.join(new List<String>(fieldApiList), ',');
        query += ', (' + chileQuery + ')';
        query += ' FROM ' + objectName;
        if (String.isNotEmpty(recordId)) {
            query += ' Where Id = :recordId AND Status = \'InProgress\' AND';
        } else {
            query += ' Where Status = \'InProgress\' AND';
        }
        query += ' CreatedDate < :yesterdayDateTime';

        List<LiveChatTranscript> chatTranscriptList = Database.query(query);  
List<SObject> cloneChatTranscriptList = new List<SObject>();
            List<SObject> cloneChatTranscriptEventList = new List<SObject>();

            LiveChatTranscript liveChatTranscript = new LiveChatTranscript();
            LiveChatTranscriptEvent liveChatTranscriptEvent = new LiveChatTranscriptEvent();

            for (LiveChatTranscript liveChatTranscriptObj : chatTranscriptList) {

                String externalId = liveChatTranscriptObj.Id + '_yg';

                LiveChatTranscript liveChatTranscriptExternalIdRef = new LiveChatTranscript();
                liveChatTranscriptExternalIdRef.External_Id__c = externalId;

                for (LiveChatTranscriptEvent liveChatTranscriptEventObj : liveChatTranscriptObj.LiveChatTranscriptEvents) {

                    Map<String, Object> liveChatTranscriptEventMap = (Map<String, Object>) JSON.deserializeUntyped(JSON.serialize(liveChatTranscriptEventObj));
                    liveChatTranscriptEventMap.remove('LiveChatTranscriptId');
                    liveChatTranscriptEventObj = (LiveChatTranscriptEvent) JSON.deserialize(JSON.serialize(liveChatTranscriptEventMap), LiveChatTranscriptEvent.class);
                    liveChatTranscriptEvent = liveChatTranscriptEventObj.clone(false, false, false, false);
                    liveChatTranscriptEvent.LiveChatTranscript = liveChatTranscriptExternalIdRef;
                    cloneChatTranscriptEventList.add(liveChatTranscriptEvent);

                }

                liveChatTranscript = liveChatTranscriptObj.clone(false, false, false, false);
                liveChatTranscript.ReportCreateDate__c = liveChatTranscriptObj.CreatedDate;
                liveChatTranscript.External_Id__c = externalId;
                liveChatTranscript.Status = 'Completed';
                cloneChatTranscriptList.add(liveChatTranscript);
            }

            cloneChatTranscriptList.addAll(cloneChatTranscriptEventList);
            if (cloneChatTranscriptList.size() > 0) {
                insert cloneChatTranscriptList;
                delete chatTranscriptList;
                return cloneChatTranscriptList[0].Id;
            }





1
  • Thanks a lot Nigam..it did work..
    – user50121
    Commented Jan 24, 2020 at 3:17
1

I hope you do know how triggers work and what are they used for. The way I would do it is like this:

  1. In your after update trigger, I would loop through all the items
  2. Find the ones that were closed and store them in a list
  3. Then, query all their children
  4. Create a map(m1) with Case as a parent and a list of workstreams as children
  5. Loop over m1 to create new Cases and clone the workstreams(in the new map m2)
  6. Insert the m2's keySet(cases)
  7. Loop over once again to assign case id to the cloned workstreams
  8. Insert the workstreams

If all that sounds gibberish, then I would recommend you taking a look at Apex Triggers Module on Trailhead.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .