function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Casey Conner 20Casey Conner 20 

Solution to clone a record many times automatically

I am looking for suggestions on a solution I am trying to build. Let me try to explain. 

Here are my objects. Notice__c, Notification__c, Notice Group__c, Notice Group Member__c

I want to be able to create a Notice record. Each Notice is related to a Notice Group. Each Notice Group has a set of Notice Group Members__c. Once a Notice it published (status value = published), a notification record should be created for each Notice Group Member__c associated with the selected Notice Group.

The purpose is to create a notice for an audience of users, but allow individual tracking of the timing of notice acknowledgement. Therefore, each user has their own clone of the Notice record to interact with.

I am trying to evaluate the best way to implement this solution using any combination of workflow tools / Apex. Thanks!


 
Sanjay Bhati 95Sanjay Bhati 95
Hi Casey Conner,

Please describe the relation of Notification__c with any object. So we can figure it out.
Dushyant SonwarDushyant Sonwar
Casey,

Could you post the schema diagram of these objects as the above detail is not clear about the object relation ship? You can use the schema builder option of salesforce.
 
Dushyant SonwarDushyant Sonwar
You can also create er diagram online. We need it  just to understand the relationship between these objects
Casey Conner 20Casey Conner 20

Here you go. Sorry, I should have included it in the original. Also, my object names on the schema are a little different than originally labeled.


So to elaborate, if a Notice is created and the associated Recipient Group has 5 Recipient Group Member records, then the code/workflow should create 5 Notification records and assign each to the 5 Recipient Group Member records. 

User-added image

Sanjay Bhati 95Sanjay Bhati 95
Hi Casey Conner 20

you can use After Insert trigger on Notice Object. Here is the below example of code that you can refer for your scenario. 
 
List<Recipient__c > receList = [Select Id, UserField From Recipient Where Notice__c=:Trigger.new];

List<Notification__c> notificationList = new List<Notification__c>();
for(Recipient__c  rec : ){
     Notification__c not = new Notification__c();
     not.UserId = rec.UserField // You can refer you original field here
     not.Notice__c = rec.Notice__c;
    // Other data that you want to attach with new record.
    
     notificationList.add(not);
}

if(notificationList.size() > 0){
   insert notificationList;
}

Let me know if you need any clarification. 
Casey Conner 20Casey Conner 20
Thanks Sanjay. I am going to mess around with that and see if I can get it working. Thx!
 
Dushyant SonwarDushyant Sonwar
Casey,

Apologies from my end for replying on so late on this.

After assuming the api names as i don't have any idea of api names.

I have tried and added some comments to simply what is being done below:
 
trigger NoticeTrigger on Notice__c(After Update){
	
	set<String> setOfNoticeIds = new set<String>();
	list<Notification__c> lstNotification = new list<Notification__c>();
	Map<String , set<String>> mapOFUSerIds = new Map<String , set<String>>();
	for(Notice__c noticeObj : Trigger.New){
		if(trigger.oldMap.get(noticeObj.Id).Status__c != noticeObj.Status__c){ // when status field is changed
			if(noticeObj.Status__c =='Published'){ // and notice is published
				setOfNoticeIds.add(noticeObj.Id);
			}
		}
	}
	if(setOfNoticeIds.size() > 0){
		for(RecipentGroupMember__c recipentGroupMemberObj : [Select user__c,RecipentGroup__r.Notice__c from RecipentGroupMember__c 
																	where RecipentGroup__r.Notice__c in :setOfNoticeIds]){ //Query all the related Group Member
			set<String> setOFUserIds = new set<String>();
			if(mapOFUSerIds.containsKey(recipentGroupMemberObj.RecipentGroup__r.Notice__c)){
				setOFUserIds = mapOFUSerIds.get(recipentGroupMemberObj.RecipentGroup__r.Notice__c);
			}
			setOFUserIds.add(user__c);
			mapOFUSerIds.put(RecipentGroup__r.Notice__c  , setOFUserIds); // create a map of notice and corresponding group member
		}
	}
	
	for(Notice__c noticeObj : Trigger.New){
		if(mapOFUSerIds.containsKey(noticeObj.Id)){
			for(String userId : mapOFUSerIds.get(noticeObj.)){ // create notification records related to 
				Notification__c notificationObj = new Notification__c(User__c = userId);
				notificationObj.Notice__c = noticeObj.Id;
				lstNotification.add(notificationObj);
			}
		}
	}
	if(lstNotification.size() > 0){
		insert lstNotification;
	}
}
  1.  First we checked if status field is changed , if changed does the changed value is a Published
  2. We query all the group memeber as we know the notice.
  3. Create a map of notice and correspong users so that when we loop through the notice records we can find group member user , and create notification records
Hope this help!