+ Start a Discussion
cml9cml9 

Help in Trigger on Attachments.

Hi Experts,

I am not sure why this is not working, I found this code in the net and I thought this is working just fine. There is an error about list. Please help.
 
trigger triggerOnAttachment on Attachment (before insert) {
    List OppList = new List();
    Set OppIds = new Set();
    for(Attachment att : trigger.New){
         //Check if added attachment is related to Account or not
         if(att.ParentId.getSobjectType() == Oppportunity.SobjectType){
              OppIds.add(att.ParentId);
         }
    }
    accountList = [select id, hasAttachment__c from Opportunity where id in : OppIds];
    if(OppList!=null && OppList.size()>0){
        for(Opportunity Opp : OppList){
            Opp.hasAttachment__c = true;
        }
        update OppList;
    }
}

It is saying Unexpected Error on List. 
Best Answer chosen by cml9
Gnani MatavalamGnani Matavalam
Hello, check this out.

if(trigger.isDelete)
{
    for(Attachment att : trigger.old)
    {

         //Check if added attachment is related to Opportunity or not

         if(att.ParentId.getSobjectType() == Opportunity.SobjectType){

              OppIds.add(att.ParentId);

         }

     }
}

Cheers,
Gnani

All Answers

Gnani MatavalamGnani Matavalam
Hello There :),

Just replace List OppList = new List () with List<Opportunity> = new List<Opportunity>();
Also on line 10, you need to repalce accountList with Opplist. this is just a bonus for your question ;).
Have a good luck mate.

Cheers,
Gnani
SandhyaSandhya (Salesforce Developers) 
Hi ,

In Apex you need to specify the data type when you are declaring list or set or map.

So above lines should be
 
List <opportunity> OppList = new List<opportunity>();
    Set<id> OppIds = new set<id>();

Please refer below link for more information on collections.

http://blog.jeffdouglas.com/2011/01/06/fun-with-salesforce-collections/
 
Hope this helps you!

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
Sandhya

 
cml9cml9
Thanks everyone I got it working in updating the List but now I am having another error during trigger.isDelete.
 
trigger triggerOnAttachment on Attachment (before insert, before delete) {
   List <opportunity> OppList = new List<opportunity>();
    Set<id> OppIds = new set<id>();
    for(Attachment att : trigger.New){
         //Check if added attachment is related to Opportunity or not
         if(att.ParentId.getSobjectType() == Opportunity.SobjectType){
              OppIds.add(att.ParentId);
         }
    }
    OppList = [select id, hasAttachment__c from Opportunity where id in : OppIds];
    if(OppList!=null && OppList.size()>0){
        for(Opportunity Opp : OppList){
            Opp.hasAttachment__c = true;
        }
        update OppList;
        
    }
   // if attachment is deleted
    
    for(Attachment att : trigger.isDelete){
         //Check if added attachment is related to Opportunity or not
         if(att.ParentId.getSobjectType() == Opportunity.SobjectType){
              OppIds.add(att.ParentId);
         }
    }
    OppList = [select id, hasAttachment__c from Opportunity where id in : OppIds];
    if(OppList!=true && OppList.size()>0){
        for(Opportunity Opp : OppList){
            Opp.hasAttachment__c = False;
        }
        update OppList;
    }
}

The error I am having now is Loop must iterate over a collection type: Boolean

is my logic correct for deleting an Attachement where the hasAttachment__c will update to False?
SandhyaSandhya (Salesforce Developers) 
Hi,

You loop should be 
for( OppList att : trigger.isDelete){

Thanks and Regards
Sandhya
cml9cml9
I am already using it on line 20.
Gnani MatavalamGnani Matavalam
Hello, check this out.

if(trigger.isDelete)
{
    for(Attachment att : trigger.old)
    {

         //Check if added attachment is related to Opportunity or not

         if(att.ParentId.getSobjectType() == Opportunity.SobjectType){

              OppIds.add(att.ParentId);

         }

     }
}

Cheers,
Gnani
This was selected as the best answer
cml9cml9
Thanks man! its now working!