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
mxalix258mxalix258 

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;
        }
}

 

~Onkar~Onkar

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;
}
}

mxalix258mxalix258

Hmm...no luck there either. I'm not sure what else it could be....

OhadiosOhadios

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.

 

 

trigger Update_Attachment_Parent on Attachment (after insert, after update) {
//Create a List to store the Ids of Attachments to be deleted
List<Id> forDeletionIds = new List<Id>();
for (Attachment a : trigger.new){
//Find the prefix for the Order_Email_Storage object
Schema.DescribeSObjectResult dsr = Order_Email_Storage__c.SObjectType.getDescribe();
String Order_Email_Prefix = dsr.getKeyPrefix();
system.debug('Order_Email_Storage__C Prefix is: '+Order_Email_Prefix);
//Check to see if the Attachment has an Order_Email_Storage as the Parent Record
String parentIdString = String.valueof(a.parentId);
if (parentIdString.substring(0,3) == Order_Email_Prefix) {
//Order_Email_Prefix){
//Select the Primary Opportunity from the Account
Order_Email_Storage__c parent = [SELECT Master_ID__c FROM Order_Email_Storage__c WHERE ID = :a.parentId];
//Check to see if a Primary Opportunity exists
if (parent.Master_ID__c != null){
//Select the Attachment body (it isn't in memory for an update)
Attachment body = [SELECT Body FROM Attachment WHERE Id = :a.Id];
//Create a new Attachment, preserving as much as is possible
Attachment newA = New Attachment(
Name = a.Name,
Body = body.Body,
IsPrivate = a.IsPrivate,
ContentType = a.ContentType,
Description = a.Description,
OwnerId = a.OwnerId,
//Connect the Attachment to the Primary Opportunity
parentId = parent.Master_ID__c);
//Insert the new Attachment
insert newA;
//Add the now duplicate Attachment ID to a list
forDeletionIds.add(a.Id);
}
}
}
//List and then delete all duplicate Attachments
List<Attachment> forDeletion = [SELECT Id FROM Attachment WHERE Id IN :forDeletionIds];
delete forDeletion;
}