You need to sign in to do that
Don't have an account?

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;
}
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;
}
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.
Please do let me know if it helps you.
Regards,
Mahesh
All Answers
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.
Please do let me know if it helps you.
Regards,
Mahesh