You need to sign in to do that
Don't have an account?
George Adams
Copy case email attachments to custom object
Hi all,
I'm writing an apex class that is called by a Detail Page Button to do several things. It creates a new record in a custom object called "Hearing Result". I'd like to copy the current case's Case Email attachments to this new record in the custom object.
How can I do this?
Here is my apex class so far:
global class logHearingResult{ WebService static id createHearingResult(Id currentCase) { //new set to hold current case Ids Set<Id> caseIds = new Set<Id>(); //add current case Id to above set (currentCase is passed from javascript button) caseIds.add(currentCase); //new list to hold cases with the current case's Id List<Case> results = [ select Id, Priority, ContactId from Case where Id in :caseIds ]; List<Hearing_Result__c> resultsToInsert = new List<Hearing_Result__c>(); id newHearingResultLocation; //work based on current case for (Case c : results){ Hearing_Result__c ins = new Hearing_Result__c (ContactID__c = c.ContactId); resultsToInsert.add(ins); insert ins; newHearingResultLocation = ins.id; } update results; return newHearingResultLocation; } }Thanks!
Please find the below code:
Here:
(1) i changed the class to normal class just for me to test in Developer Console.
(2) We can improve it in multiple ways which you can able to do it.
Tested the program by using the below code in Developer Console:
Also please find the screenshots of created records.
New Record created under Hearing Result object:
Copied Attachment is here:
Please do let me know if it helps you.
Regards,
Mahesh
All Answers
Please find the below code:
Here:
(1) i changed the class to normal class just for me to test in Developer Console.
(2) We can improve it in multiple ways which you can able to do it.
Tested the program by using the below code in Developer Console:
Also please find the screenshots of created records.
New Record created under Hearing Result object:
Copied Attachment is here:
Please do let me know if it helps you.
Regards,
Mahesh
I think the issue I'm having with your solution is that the attachment is attached to the Case Email, and not the case itself. So in debug, attList is empty.
Can your code be modified to grab the attachment from the Case Email?
Here's what the case looks like:
And if I go to the email, this is what it looks like:
I am not sure how you tested the solution,
Just for testing, can you copy my code as is and go to Developer Console and execute the below 2 lines of code by chaning the Case number:
Step 1: Check whether the new record created under "Hearing Result".
Step 2: Attached got copied or not.
Step 3: Anything new happened on the Input Case object record.
Regards,
Mahesh
The issue is that the attachment is on a sub-object of the actual case, so the code needs to look "one level deeper".
I decided to use a workaround to move the attachment from the Case Email up to the Case itself. I found this trigger online that accomplishes this:
That trigger combined with your code works perfectly, so thank you for the help!