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
Edward VazquezEdward Vazquez 

Explaining what my Apex Class does

Hello,

Creating an Apex Class is new to me. I would like to share a class and I would really appreciate it if someone  walked me thorugh it so I understand what the class is doing. Here is the code. Thanks for your help in advance.

public with sharing class ResalesAttachment {

  public void CreateResalesAttachment(list<Attachment> aListForResales){
    set<id> resalesIdSet = new set<id>();
    for(Attachment a : aListForResales){
      resalesIdSet.add(a.ParentId);
    }
    map<id,Resales__c> resalesMap = new map<id,Resales__c>();
    resalesMap.putall([select Resident_Name__c
      from Resales__c 
      where id in :resalesIdSet]);
    map<id,Resales_Documents__c> attachmentToResalesDoc = new map<id,Resales_Documents__c>();
    for(Attachment a : aListForResales){
      Resales_Documents__c rd = new Resales_Documents__c();
        rd.name = a.name;
        rd.ID_Of_Original_Attachment__c = a.id;
        rd.Resales__c = resalesMap.get(a.ParentId).id;
        

      attachmentToResalesDoc.put(a.id,rd);
    }
    insert attachmentToResalesDoc.values();
    list<Attachment> newAListForResales = new list<Attachment>();
    for(Attachment a : aListForResales){
      Attachment newA = new Attachment();
        newA.Body = a.Body;
        newA.ContentType = a.ContentType;
        newA.Description = a.Description;
        newA.IsPrivate = a.IsPrivate;
        newA.Name = a.Name;
        newA.OwnerId = a.OwnerId;
        newA.ParentId = attachmentToResalesDoc.get(a.id).id;
      newAListForResales.add(newA);
    }
    insert newAListForResales;

  }
Best Answer chosen by Edward Vazquez
Mahesh DMahesh D
Hi Edward,

Please find your class with comments added to explain you in a easy way.

Also modified the code by considering the null checks and best practices into consideration.

 
//
// This is to iterate the Attachments related to Resales
// and create Resales Document records and clone the Attachments from Resales to Resales Document 
// records which we newly inserted as part of this process.
// 
public with sharing class ResalesAttachment {

	public void CreateResalesAttachment(list<Attachment> aListForResales) {
		Set<Id> resalesIdSet = new Set<Id>();
		// Going through the input List of Attachments and capturing the Parent Ids.
		for(Attachment a : aListForResales){
			resalesIdSet.add(a.ParentId);
		}
    
		// Here we are trying to retrieve records from Resales object and store in a Map.
		Map<Id, Resales__c> resalesMap = new map<Id, Resales__c>([select Resident_Name__c from Resales__c where id in :resalesIdSet]);
		
		Map<id, Resales_Documents__c> attachmentToResalesDoc = new Map<id, Resales_Documents__c>();
		
		// Iterating through the input List of Attachments and creating Resales Document record.
		for(Attachment a : aListForResales) {
			// All parentIds may not be Resales object records.
			if(resalesMap.get(a.ParentId) != null) {
				Resales_Documents__c rd = new Resales_Documents__c();
				rd.name = a.name;
				rd.ID_Of_Original_Attachment__c = a.id;
				rd.Resales__c = resalesMap.get(a.ParentId).id;
				attachmentToResalesDoc.put(a.id,rd);
			}
		}
		
		//Check if there any Resale Documents exists to insert.		
		if(!attachmentToResalesDoc.isEmpty()) {
			insert attachmentToResalesDoc.values();
			
			List<Attachment> newAListForResales = new List<Attachment>();
			// Iterating through the input List of Attachments and clone it with a Resale Document as a parent record.
			for(Attachment a : aListForResales){
				if(attachmentToResalesDoc.get(a.Id) != null) {
					Attachment newA = new Attachment();
					newA.Body = a.Body;
					newA.ContentType = a.ContentType;
					newA.Description = a.Description;
					newA.IsPrivate = a.IsPrivate;
					newA.Name = a.Name;
					newA.OwnerId = a.OwnerId;
					newA.ParentId = attachmentToResalesDoc.get(a.Id).Id;
					newAListForResales.add(newA);
				}
			}
			// Check if there any new Attachment records to insert.
			if(!newAListForResales.isEmpty())
				insert newAListForResales;
		}
	}
}

Please do let me know if it helps you.

Regards,
Mahesh

All Answers

Mahesh DMahesh D
Hi Edward,

Please find your class with comments added to explain you in a easy way.

Also modified the code by considering the null checks and best practices into consideration.

 
//
// This is to iterate the Attachments related to Resales
// and create Resales Document records and clone the Attachments from Resales to Resales Document 
// records which we newly inserted as part of this process.
// 
public with sharing class ResalesAttachment {

	public void CreateResalesAttachment(list<Attachment> aListForResales) {
		Set<Id> resalesIdSet = new Set<Id>();
		// Going through the input List of Attachments and capturing the Parent Ids.
		for(Attachment a : aListForResales){
			resalesIdSet.add(a.ParentId);
		}
    
		// Here we are trying to retrieve records from Resales object and store in a Map.
		Map<Id, Resales__c> resalesMap = new map<Id, Resales__c>([select Resident_Name__c from Resales__c where id in :resalesIdSet]);
		
		Map<id, Resales_Documents__c> attachmentToResalesDoc = new Map<id, Resales_Documents__c>();
		
		// Iterating through the input List of Attachments and creating Resales Document record.
		for(Attachment a : aListForResales) {
			// All parentIds may not be Resales object records.
			if(resalesMap.get(a.ParentId) != null) {
				Resales_Documents__c rd = new Resales_Documents__c();
				rd.name = a.name;
				rd.ID_Of_Original_Attachment__c = a.id;
				rd.Resales__c = resalesMap.get(a.ParentId).id;
				attachmentToResalesDoc.put(a.id,rd);
			}
		}
		
		//Check if there any Resale Documents exists to insert.		
		if(!attachmentToResalesDoc.isEmpty()) {
			insert attachmentToResalesDoc.values();
			
			List<Attachment> newAListForResales = new List<Attachment>();
			// Iterating through the input List of Attachments and clone it with a Resale Document as a parent record.
			for(Attachment a : aListForResales){
				if(attachmentToResalesDoc.get(a.Id) != null) {
					Attachment newA = new Attachment();
					newA.Body = a.Body;
					newA.ContentType = a.ContentType;
					newA.Description = a.Description;
					newA.IsPrivate = a.IsPrivate;
					newA.Name = a.Name;
					newA.OwnerId = a.OwnerId;
					newA.ParentId = attachmentToResalesDoc.get(a.Id).Id;
					newAListForResales.add(newA);
				}
			}
			// Check if there any new Attachment records to insert.
			if(!newAListForResales.isEmpty())
				insert newAListForResales;
		}
	}
}

Please do let me know if it helps you.

Regards,
Mahesh
This was selected as the best answer
Edward VazquezEdward Vazquez
Thank you. That was very helpful.