Hi Devanshu Sood, The code you posted was wrong. In before insert triggers Ids won't be generated, because the record is not saved into the database. The saved records only generate ids. So If you want to bulkify the trigger you can use after insert trigger.
Use the following code and let me know if it helps or send your error that you are getting while you are working so that I can help you. trigger oppLineItem on OpportunityLineItem (after insert) { 02 list<id>oppId=new list<id>(); 03 list<id> aId=new list<id>(); 04 if(trigger.isinsert){ 05 for(OpportunityLineItem oli:trigger.new){ 06 oppId.add(oli.OpportunityId); 07 } 08 //insert oppId; 09 list<attachment>att=new list<attachment>([SELECT Id FROM Attachment whereparentId=:oppId]); 10 for(attachment a:att){ 11 if(a.id==null || a.Id==' '){ 12 a.addError('insert attachment first'); 13 } 14 15 } 16 } 17}
try this
All Answers
'
Explain your problem I don't get that. put your complete requirement
try this
Here is your trigger I don't reduce its complexity but it will work fine after few hours i will give an update if this was helpful mark best answer else let me know
Thank you
Avaneesh Singh
I think error have to put on OpportunityLineItem, not an attachment but it's fine if Questioner is satisfied
The code you posted was wrong. In before insert triggers Ids won't be generated, because the record is not saved into the database. The saved records only generate ids. So If you want to bulkify the trigger you can use after insert trigger.
Use the following code and let me know if it helps or send your error that you are getting while you are working so that I can help you.
trigger oppLineItem on OpportunityLineItem (after insert) {
02 list<id>oppId=new list<id>();
03 list<id> aId=new list<id>();
04 if(trigger.isinsert){
05 for(OpportunityLineItem oli:trigger.new){
06 oppId.add(oli.OpportunityId);
07 }
08 //insert oppId;
09 list<attachment>att=new list<attachment>([SELECT Id FROM Attachment whereparentId=:oppId]);
10 for(attachment a:att){
11 if(a.id==null || a.Id==' '){
12 a.addError('insert attachment first');
13 }
14
15 }
16 }
17}