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
Komal SethiKomal Sethi 

Write a trigger on Opportunity LineItem , when Line Item is deletes, delete an opportunity as well.

Write a trigger on Opportunity LineItem , when Line Item is deletes, delete an opportunity as well.?
AnudeepAnudeep (Salesforce Developers) 
Hi Komal, 

You can use the following code to achieve your functionality
trigger OpportunityLineItemTrigger on OpportunityLineItem (after delete) {
    List<Id> oppIds = new List<Id>();
    List<Opportunity> oppList = new List<Opportunity>();
    if(trigger.isDelete) {
        for (OpportunityLineItem oli3: trigger.old){
        oppList=[Select Id, Name from Opportunity where Id IN:oppIds]; 
        delete oppList;

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too. Thank You!

Komal SethiKomal Sethi
Nice, Firsly before delete, confirm Opportunity Line Item Value exist or not, how to calculate size using Map.
Mohan Kalapuram 4Mohan Kalapuram 4
Hi All

Anyone have written trigger to share record with approver?

bhavesh bhatibhavesh bhati
thanks for this useful info, if you are looking to shift in hawaii than follow this guide

Can you share your sample code, can't understand what are you trying to achieve?

Caleb Kuester 27Caleb Kuester 27
Just a random word of caution, if deleting a single line item deletes the opportunity, then you have to decide what to do with all of the other line items. If they're master/detail, which I don't know if they are off-hand, then the other line items will be deleted also, possibly triggering this action again for each item, but finding no opportunity, maybe causing a rollback of the transaction.

If they aren't master/detail, then you could have a lot of orphan records.
Mohan Kalapuram 4Mohan Kalapuram 4
Can anyone help writing test class for below trigger?

trigger JobApexSharing on Job__c (after insert) {
        // Create a new list of sharing objects for Job
        List<Job__Share> jobShrs  = new List<Job__Share>();
        // Declare variables for recruiting and hiring manager sharing
        Job__Share recruiterShr;
        Job__Share hmShr;
        for(Job__c job :{
            // Instantiate the sharing objects
            recruiterShr = new Job__Share();
            hmShr = new Job__Share();
            // Set the ID of record being shared
            recruiterShr.ParentId = job.Id;
            hmShr.ParentId = job.Id;
            // Set the ID of user or group being granted access
            recruiterShr.UserOrGroupId = job.Recruiter__c;
            hmShr.UserOrGroupId = job.Hiring_Manager__c;
            // Set the access level
            recruiterShr.AccessLevel = 'edit';
            hmShr.AccessLevel = 'read';
            // Set the Apex sharing reason for hiring manager and recruiter
            recruiterShr.RowCause = Schema.Job__Share.RowCause.Recruiter__c;
            hmShr.RowCause = Schema.Job__Share.RowCause.Hiring_Manager__c;
            // Add objects to list for insert
        // Insert sharing records and capture save result 
        // The false parameter allows for partial processing if multiple records are passed 
        // into the operation 
        Database.SaveResult[] lsr = Database.insert(jobShrs,false);
        // Create counter
        Integer i=0;
        // Process the save results
        for(Database.SaveResult sr : lsr){
                // Get the first save result error
                Database.Error err = sr.getErrors()[0];
                // Check if the error is related to a trivial access level
                // Access levels equal or more permissive than the object's default 
                // access level are not allowed. 
                // These sharing records are not required and thus an insert exception is 
                // acceptable. 
                if(!(err.getStatusCode() == StatusCode.FIELD_FILTER_VALIDATION_EXCEPTION  
                                               &&  err.getMessage().contains('AccessLevel'))){
                    // Throw an error when the error is not related to trivial access level.
                       'Unable to grant sharing access due to following exception: '
                       + err.getMessage());