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
ANITHA BEEMU 2ANITHA BEEMU 2 

I need to have attachments attached to opportunity to the related account.i tired following code,no errors but attachments are not getting on account object..pls help:

ANITHA BEEMU 2ANITHA BEEMU 2

Trigger opportunity on Attachment (after insert)   
{

if(CheckRecursive.firstRun){

    CheckRecursive.firstRun = false;

    set<id> Oppids=new set<id>();
 Map<id, attachment> OpportunityAttachments = new Map<id, attachment>();
 Map<id, Opportunity> OpportunitiesWithAccount = new Map<id, Opportunity>();
for(attachment an:trigger.new)
{
 if(an.ParentId.getSobjectType() == Opportunity.SobjectType) OpportunityAttachments.put(an.ParentId, an);
 //Oppids.add(an.ParentId);
}
 // not required, as using soql
 // list<Opportunity> OppAccmap =new list<Opportunity>([Select Account.id,Account.name from Opportunity where Id In:Oppids]);
list<attachment> accatt=new list<attachment>();
 //for(Opportunity Ot:OppAccmap){
// read "Explanation" why I have replaced Soql For loop with your approach
 if (OpportunityAttachments.keySet().size() > 0) // Read "Explanation" why this check is introduced.
{
OpportunitiesWithAccount = new Map<Id, Opportunity>([SELECT Id, Account.id FROM Opportunity WHERE Id IN :OpportunityAttachments.keySet()]);
 // for (attachment am :[select id, name,parentId, body from Attachment where ParentId in :Oppids]) // soql inside for loop is bad, bad thing.
for (attachment am: OpportunityAttachments.values() )
{
 if (OpportunitiesWithAccount.containsKey(am.ParentId) )
  {
  Attachment newFile = New Attachment(Name = am.name, body = am.body, ParentId = OpportunitiesWithAccount.get(am.ParentId).Account.id);
 accatt.add(newFile);
  }
 }
 }
   insert accatt;

}

 

}


after this i created an opportunity and added an file,then i checked in tha account that related to that particular opportunity,no files are available..
Raj VakatiRaj Vakati
Your code looks good for me .. But are using attachmnets or files??


https://douglascayers.com/2015/10/10/salesforce-convert-attachments-to-chatter-files/

 
Trigger opportunity on Attachment (after insert)   
{
    System.debug('atta');
    set<id> Oppids=new set<id>();
    Map<id, attachment> OpportunityAttachments = new Map<id, attachment>();
    Map<id, Opportunity> OpportunitiesWithAccount = new Map<id, Opportunity>();
    for(attachment an:trigger.new)
    {
        if(an.ParentId.getSobjectType() == Opportunity.SobjectType) {
            System.debug('-- Going 1 ');
            OpportunityAttachments.put(an.ParentId, an);
        }
        
    }
    
    list<attachment> accatt=new list<attachment>();
    
    if (OpportunityAttachments.keySet().size() > 0) // Read "Explanation" why this check is introduced.
    {
        OpportunitiesWithAccount = new Map<Id, Opportunity>([SELECT Id, Account.id FROM Opportunity WHERE Id IN :OpportunityAttachments.keySet()]);
        
        for (attachment am: OpportunityAttachments.values() )
        {
            if (OpportunitiesWithAccount.containsKey(am.ParentId) )
            {
                Attachment newFile = New Attachment(Name = am.name, body = am.body, ParentId = OpportunitiesWithAccount.get(am.ParentId).Account.id);
                accatt.add(newFile);
            }
        }
    }
    insert accatt;
    
    
    
    
    
}

 
ANITHA BEEMU 2ANITHA BEEMU 2
i made trigger on attachments..but i am uploaded in files..in opportunity in related list files portion i am uploading the files,i want the same file to be shown inthe opportunity related account..
Raj VakatiRaj Vakati
Then you need to use the contentDocument object not attachments .... 
ANITHA BEEMU 2ANITHA BEEMU 2
so i have make trigger on contentDOCUMENT
ANITHA BEEMU 2ANITHA BEEMU 2
HI Raj,can you help on content document trigger..i need files to be there in account..pls do help
ANITHA BEEMU 2ANITHA BEEMU 2
HI RAJ,Can you tell me is this code fine for my requirement:

Trigger opportunityfiles on ContentDocumentLink (after insert)  

{
  System.debug('atta');

    set<Id> LinkedEntityId=new set<id>();


    Map<id, ContentDocumentLink> OpportunityAttachments = new Map<id, ContentDocumentLink>();

    Map<id, Opportunity> OpportunitiesWithAccount = new Map<id, Opportunity>();

    for(ContentDocumentLink an:trigger.new)

    {

        if(an.LinkedEntityId.getSobjectType() == Opportunity.SobjectType) {

            System.debug('-- Going 1 ');

            OpportunityAttachments.put(an.LinkedEntityId, an);

        }

         

    }

     

    list<ContentDocumentLink> accatt=new list<ContentDocumentLink>();

     

    if (OpportunityAttachments.keySet().size() > 0) // Read "Explanation" why this check is introduced.

    {

        OpportunitiesWithAccount = new Map<Id, Opportunity>([SELECT Id, Account.id FROM Opportunity WHERE Id IN :OpportunityAttachments.keySet()]);

         

        for (ContentDocumentLink am: OpportunityAttachments.values() )

        {

            if (OpportunitiesWithAccount.containsKey(am.LinkedEntityId) )

            {

                ContentDocumentLink newFile = New ContentDocumentLink(LinkedEntityId = OpportunitiesWithAccount.get(am.LinkedEntityId).Account.id);

                accatt.add(newFile);

            }

        }

    }

    insert accatt;
}


i am getting error:

Variable does not exist: att at line 23
Invalid type: Ids at line 7..

I am not able to find the where is the error..can you help in this.. i made trigger on ContentDocumentLink
Raj VakatiRaj Vakati
Is its coming from the same trgger ? 
ANITHA BEEMU 2ANITHA BEEMU 2
Yes Raj, just changed object and corresponding fields.