You need to sign in to do that
Don't have an account?
mxalix258
Unable to read attachment parentid?
I'm trying to create a trigger on attachments, so that when an attachment is added to a Task, and the Task is related to an opportunity, the attachment will be cloned onto the Opportunity.
Below is the code I have so far, but I can only get it to work if I remove the if(a.parentid == t.id) before creating the new attachment. Any thoughts?
trigger CloneOppTask on Attachment (after insert, after update) { Attachment[] insertAttList = new Attachment[]{}; Set<ID> taskset = new Set<ID>(); List<Attachment> attlist = [Select Id, parentId, name, body from Attachment where Parent.Type='Task' AND Id in :Trigger.new]; for(Attachment a : attlist){ taskset.add(a.parentId); } for(Task t: [Select ID, WhatId From Task Where WhatID in :taskset]){ for(Attachment a: attlist){ if(a.parentid == t.id){ Attachment att = new Attachment(name = a.name, body = a.body, parentid = t.WhatId); insertAttList.add(att); } } } if(insertAttList.size() > 0){ insert insertAttList; } }
May be you are doing mistake to write the Query.
I corrected it with please check. Hope it wil resolve your problem.
trigger CloneOppTask on Attachment (after insert, after update) {
Attachment[] insertAttList = new Attachment[]{};
Set<ID> taskset = new Set<ID>();
List<Attachment> attlist = [Select Id, parentId, name, body from Attachment where Parent.Type='Task' AND Id in :Trigger.new];
for(Attachment a : attlist){
taskset.add(a.parentId);// Holding the Task is here.
}
//for(Task t: [Select ID, WhatId From Task Where WhatID in :taskset]){// Incorrect-----use what.type ="opportunity" and id in : taskset
for(Task t: [Select ID, WhatId From Task Where ID in :taskset]){
for(Attachment a: attlist){
if(a.parentid == t.id){
Attachment att = new Attachment(name = a.name, body = a.body, parentid = t.WhatId);
insertAttList.add(att);
}
}
}
if(insertAttList.size() > 0){
insert insertAttList;
}
}
Hmm...no luck there either. I'm not sure what else it could be....
I've created a similar trigger for a custom object called "Orders__c" and a second object "Order_Email_Storage__c".
Beyond just cloning it to the parent, I am also deleting it from the original object.
See if you can adapt this code to your needs.
The code is noted so that it should be a breeze to modify.