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
Sam MalhotraSam Malhotra 

Create a Trigger OpportunityLineItems

Notes / Attachment on opporutnity only then allow to create Opportunity Line Item
Best Answer chosen by Sam Malhotra
Devanshu soodDevanshu sood
trigger oppLineItem on OpportunityLineItem (before insert) {
    list<id>oppId=new list<id>();
    list<id> aId=new list<id>();
    if(trigger.isinsert){
        for(OpportunityLineItem oli:trigger.new){
            oppId.add(oli.OpportunityId);
        }
        //insert oppId;
        list<attachment>att=new list<attachment>([SELECT Id FROM Attachment where parentId=:oppId]);
        for(attachment a:att){
            if(a.id==null || a.Id==''){
                a.addError('insert attachment first');
            }   
            
        }
    }
}

try this
 

All Answers

AvaneeshAvaneesh
Hi Sam
'
Explain your problem I don't get that. put your complete requirement 
 
Sam MalhotraSam Malhotra
Trigger on OpportunityLineItems when attachment files are attached on opportunity then allow to create Opportunity Line Item otherwise not....
 
KB Tech SolutionsKB Tech Solutions
Create a "before context" trigger on Opportunity. Query for related Attachements. If there are none, create an error on the record.
Devanshu soodDevanshu sood
trigger oppLineItem on OpportunityLineItem (before insert) {
    list<id>oppId=new list<id>();
    list<id> aId=new list<id>();
    if(trigger.isinsert){
        for(OpportunityLineItem oli:trigger.new){
            oppId.add(oli.OpportunityId);
        }
        //insert oppId;
        list<attachment>att=new list<attachment>([SELECT Id FROM Attachment where parentId=:oppId]);
        for(attachment a:att){
            if(a.id==null || a.Id==''){
                a.addError('insert attachment first');
            }   
            
        }
    }
}

try this
 
This was selected as the best answer
AvaneeshAvaneesh
Hi Sam 
Here is your trigger I don't reduce its complexity but it will work fine after few hours i will give an update 
trigger TriggerForOppLineItem on OpportunityLineItem (before insert) {

    Set<id> oppIdSet = new Set<id>();
    for(OpportunityLineItem oli :trigger.new){
        if(oli.OpportunityId !=NULL){
            oppidSet.add(oli.OpportunityId);
        }
    }
    
    Map<id, Attachment> allatt = new Map<id,Attachment>();
    for(attachment att:[select id,name,parentid from attachment where parentid In :oppIdSet]){
        allatt.put(att.ParentId, att);
    }
    for(OpportunityLineItem oli :trigger.new){
        if(oli.OpportunityId !=NULL && allatt.containsKey(oli.OpportunityId) && allatt.get(oli.OpportunityId) ==NULL){
           oli.addError('Trigger error because no attachment');
        }
   }
}
if this was helpful mark best answer else let me know 
Thank you
Avaneesh Singh
AvaneeshAvaneesh
Hi Devanshu sood,
I think error have to put on OpportunityLineItem, not an attachment but it's fine if Questioner is satisfied 

 
Sam MalhotraSam Malhotra
Hi Avaneesh Singh Your Trigger  is  not working...
mohan s 37mohan s 37
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}