0

I'm trying to clone an attachment on an opportunity to a custom case object via a trigger. My code is creating the case but not cloning the attachment. Is there anything I can do to get this to work?

trigger RDAssessmentCreation on Opportunity (after insert, after update) {
list<PlatformCase__c> lstPC = new List<PlatformCase__c>();
list<map<Date,PlatformCase__c>> lstmapPC = new list<map<Date,PlatformCase__c>>();


    list<Opportunity> lstOpp = [Select id, Accountid,Filing_Deadline__c, Projected_Delivery_Date__c, CloseDate, StageName, Generate_Projects__c, RecordTypeId,(Select Id, ProductID__c,Jurisdiction__c,Quantity,ServiceDate From Opportunitylineitems) From Opportunity
                               ];


    for(opportunity opp : trigger.new){
        if(opp.StageName =='3 - Assessment Request' && opp.RecordTypeId == '012M00000004qsD') {
            lstPC.add(new PlatformCase__c(Opportunity__c = opp.Id,
                                          Subject__c = 'Test R&D Case',
                                          Status__c = 'New',
                                          Category__c = 'Data Support',
                                          Description__c = 'Test Add Case Attachment'));
            list<Attachment> lstAttachment = [SELECT id FROM Attachment WHERE parentID IN :lstOpp];
            Attachment newAtt = new Attachment(ParentId = opp.id);
        }
    }
    insert lstPC;
}
2
  • where's the insert of newAtt?
    – cropredy
    Feb 18, 2015 at 0:17
  • I added that prior to insert Case and I'm getting the error variable does not exist. Feb 18, 2015 at 0:32

1 Answer 1

1

I am going to agree with @crop1645 on this you are definitely missing your insert of your new attachments. To expand on that it looks like you want to include multiple attachments as well so you will want to loop through your attachments and most likely clone them, the reason for that is if attachments are deleted from the new object you most likely wouldn't want them to be removed from your original opportunity. Hope this helps.

List<Attachment> attachments = new List<Attachment>();

for(Attachment file : Trigger.new) {
   Attachment newFile = file.clone();
   newFile.ParentId = opp.Id;
   attachments.add(newFile);
   }
// finally, insert the cloned attachments
insert attachments;

Updated Code Section

>     for(opportunity opp : trigger.new){
>         if(opp.StageName =='3 - Assessment Request' && opp.RecordTypeId == '012M00000004qsD') {
>             lstPC.add(new PlatformCase__c(Opportunity__c = opp.Id,
>                                           Subject__c = 'Test R&D Case',
>                                           Status__c = 'New',
>                                           Category__c = 'Data Support',
>                                           Description__c = 'Test Add Case Attachment'));
>         }
>         List<Attachment> attachments = new List<Attachment>();
> 
> for(Attachment file : Trigger.new) {    Attachment newFile =
> file.clone();    newFile.ParentId = opp.Id;   
> attachments.add(newFile);    }
> 
> insert attachments;
>         insert lstPC; } }
3
  • I'm having issues placing this statement in the trigger. What else do I need for this to work? I'm getting the error that opp.Id variable does not exist. Feb 18, 2015 at 15:52
  • Where did you place this code in your file, it will need to be within the scope of opp.Id
    – TC Sutton
    Feb 19, 2015 at 19:49
  • See updated code section. Feb 20, 2015 at 17:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .