+ Start a Discussion
ANITHA BEEMU 2ANITHA BEEMU 2 

Hi,can anyone help on this,i need to have all attachments of opportunity in their respective accounts too..

I tired following code :

Trigger CopyopportunityAttachtoAccount on Attachment (after insert)     
{
    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(am.ParentId);
                accatt.add(newFile);
            }
        }
    }
    insert accatt;
}

GETTING ERROE OVER HERE:

Attachment newFile = New Attachment(Name = am.name, body = am.body, ParentId=OpportunitiesWithAccount(am.ParentId);
                accatt.add(newFile);

AND Incorrect SObject type ..PLS DO HELP
Steven NsubugaSteven Nsubuga
Try this
Trigger CopyopportunityAttachtoAccount on Attachment (after insert)     
{
    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=am.ParentId;
                accatt.add(newFile);
            }
        }
    }
    insert accatt;
}

 
ANITHA BEEMU 2ANITHA BEEMU 2
Hi Thank you for Response,getting error  Incorrect SObject type,i am firing the trigger on opportunity..
Steven NsubugaSteven Nsubuga
What  line is giving that error?
Try
Trigger CopyopportunityAttachtoAccount on Attachment (after insert)     
{
    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(am.ParentId).Account.id);
                accatt.add(newFile);
            }
        }
    }
    insert accatt;
}

 
ANITHA BEEMU 2ANITHA BEEMU 2
Hi,Pls find the error..

Compile Error: Trigger name, CopyopportunityAttachtoAccount, exists on different SObject type: Opportunity at line -1 column -1

i am placing the trigger in opportunity object..
Steven NsubugaSteven Nsubuga
This means you need to give the trigger a different name. Try
Trigger CopyopportunityAttachmentToAccount on Attachment (after insert)     
{
    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(am.ParentId).Account.id);
                accatt.add(newFile);
            }
        }
    }
    insert accatt;
}

 
ANITHA BEEMU 2ANITHA BEEMU 2
i changed the name,still same error..i am an admin,actually trigger is there  on attachments,but i am firing on opportunity,is thats the issue..
pls help..
Steven NsubugaSteven Nsubuga
Try using different names, the issue is with the name, use something you are sure does not exist. Try 
Trigger CopyOppAttachmentsToAccount on Attachment (after insert)     
{
    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(am.ParentId).Account.id);
                accatt.add(newFile);
            }
        }
    }
    insert accatt;
}

 
ANITHA BEEMU 2ANITHA BEEMU 2
hi i used the trigger through Developer console,it saved..but no attachments are adding to account object ..is there any wrong in my code..can you pls check and let me know.
ANITHA BEEMU 2ANITHA BEEMU 2
This is the code i used:

Trigger opportunityon Attachment (after insert)    

{

    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=am.ParentId;

                accatt.add(newFile);

            }

        }

    }

    insert accatt;

}
 
Steven NsubugaSteven Nsubuga
Use this 
Trigger opportunityon Attachment (after insert)    

{

    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, OpportunitiesWithAccount(am.ParentId).Account.id);

                accatt.add(newFile);

            }

        }

    }

    insert accatt;

}

 
ANITHA BEEMU 2ANITHA BEEMU 2
Getting Error:

1.Field does not exist :OpportunitieswithAccount on Attachment
2.missing '=' at '('

both errors i am getting on this one.
 

                Attachment newFile = New Attachment(Name = am.name, body = am.body, OpportunitiesWithAccount(am.ParentId).Account.id);
 
ANITHA BEEMU 2ANITHA BEEMU 2
used this one got this error now:

Method does not exist or incorrect signature:void Opportunitieswithaccount(Id) from the type opportunity
Steven NsubugaSteven Nsubuga
Trigger opportunityon Attachment (after insert)    

{

    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;

}

 
ANITHA BEEMU 2ANITHA BEEMU 2
now it saved..but still when i add an file to opportunity,its not reflecting under that account files one..pls help on this..
Steven NsubugaSteven Nsubuga
Check again using an opportunity that has an account. I have just tested it and it works. Use the last code I gave you, make sure that you have this bold line in the code: 
Attachment newFile = New Attachment(Name = am.name, body = am.body, ParentId = OpportunitiesWithAccount.get(am.ParentId).Account.id);
Steven NsubugaSteven Nsubuga
In fact, the trigger runs twice, make this change
Create a new class, see below
public class CheckRecursive { 
   public static boolean firstRun = true; 
}

Then in the trigger
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;
}

}

ANITHA BEEMU 2ANITHA BEEMU 2
i tired the same steven,still same issue..where are u firing the trigger,on which object..

i fired on attachments ,when i am saving attachment which is having account,its not showing on that account.
ANITHA BEEMU 2ANITHA BEEMU 2
same  issue steven..i am sorry,i dont know where is the issue,its not working for me..
Steven NsubugaSteven Nsubuga
Post your current trigger code here.
ANITHA BEEMU 2ANITHA BEEMU 2
in developer console on attachments i fired this,this is my code:

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..
Steven NsubugaSteven Nsubuga
The code is fine. Can you check that the Attachment was actually created for the Opportunity?
ANITHA BEEMU 2ANITHA BEEMU 2
its there in files object..
ANITHA BEEMU 2ANITHA BEEMU 2
its there in files object.. for that opportunity..