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
satyamsatyam 

Please help to correct the mass clone record with attachment trigger

Hi,

 

I am mass cloning the record and for mass cloning the attachment i have written the trigger but its cloning the attachment of first record only and i want to mass clone the attachment with record also.

 

Can anyone please help me to resolve mu problem .Help will really appriciated.Please find attached my trigger below.What changes needed to do for mass cloning the attachment also.

 

trigger CopyAttachments on New_Support__c(after insert)
{
List<Id> parentIds=new List<Id>();
Attachment[] attList = [select id, name, body from Attachment where ParentId = :Trigger.new[0].Parent_New_Support__c];
Attachment[] insertAttList = new Attachment[]{};

for(Attachment a: attList)
{
Attachment att = new Attachment(name = a.name, body = a.body, parentid = Trigger.new[0].id);
insertAttList.add(att);
}
if(insertAttList.size() > 0)
{
insert insertAttList;
}

}

 

Thanks in advance:-)

AdrianCCAdrianCC

It's only cloning the first New_Supports attachments because of the where condition in your soql:

"where ParentId = :Trigger.new[0].Parent_New_Support__c"

 

That [0] means you're only checking for the 1st element of the list(arrays and lists start at 0 index).

 

trigger CopyAttachments on New_Support__c(after insert) {

	//we need to 1st extract a set for the related records ids(the records from which the filed are cloned)
	Set<Id> parentIdsSet = new Set<Id>();
	//we also need to way to easily access the list of New Supports that have the same Parent(I presume the relation is 1 Parent has many Supports)
	Map<String, List<String>> parentIdTosupportIdMap = new Map<String, List<String>>();
	for (New_Support__c supp: Trigger.new) {
		String parentId = supp.Parent_New_Support__c;
		if (parentId != null) {
			parentIdsSet.add(parentId);
			if (parentIdTosupportIdMap.get(parentId) != null) {
				parentIdTosupportIdMap.get(parentId).add(supp.Id);
			} else {
				parentIdTosupportIdMap.put(parentid, new List<String>());
				parentIdTosupportIdMap.get(parentId).add(supp.Id);
			}
		}
		

	}

	//we extract the list of attachments
	Attachment[] attList = [select id, name, body, ParentId from Attachment where ParentId IN :parentIdsSet];
	Attachment[] insertAttList = new Attachment[]{};

	if ((attList != null) && (attList.size() > 0)) {
		for(Attachment a: attList) {
			List<String> supportIdsList = parentIdTosupportIdMap.get(a.ParentId);
			for(String s: supportIdsList) {
				Attachment att = new Attachment(name = a.name, body = a.body, parentid = s);
				insertAttList.add(att);
			}
		}

		if(insertAttList.size() > 0) {
			try {
				insert insertAttList;
			} catch (Exception ex) {
				System.debug('Crashed!!!' + ex.getMessage());
			}
			
		}
	}
	

}

 

PS: this was written fast and dirty in a generic text-editor. Sorry for typos and such, in advance

 

 Adi