You need to sign in to do that
Don't have an account?
cml9
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.
It is saying Unexpected Error on List.
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.
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
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
In Apex you need to specify the data type when you are declaring list or set or map.
So above lines should be
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
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?
You loop should be
for( OppList att : trigger.isDelete){
Thanks and Regards
Sandhya
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