0

I have a trigger in place for Opportunity. This trigger is meant to fire when an opportunity reaches the 'VoB Pending' stage.

trigger opportunityOwnerAlert on Opportunity (before insert,before update) {
Id[] oppList = new List<Id>();
    if(trigger.isBefore && (trigger.IsInsert || trigger.IsUpdate) && lightningStep.isFirstTime){
        lightningStep.isFirstTime = false; //prevents recursion
        for(Opportunity opp : trigger.new){
            if(opp.stagename == 'VoB Pending' && opp.Push_To_Lightning_Step__c == True){
                    oppList.add(opp.Id);
            }
        }
    }
     if(!oppList.isEmpty()) {
             lightningStep.Sendtest(oppList);
        }

}

The only time it fails is when I create an opportunity and set the stage to 'VoB Pending', soon as i hit the create button (Save), i get the folowing error.

opportunityOwnerAlert: execution of BeforeInsert caused by: System.QueryException: List has no rows for assignment to SObject Class.lightningStep.Sendtest: line 10, column 1 Trigger.opportunityOwnerAlert: line 12, column 1

line 10 is just a soql query,

Opportunity opp = [SELECT Id FROM Opportunity WHERE Id = : oppIds LIMIT 1];

Im a little lost as to why its not being picked up by the trigger since i have the 'before insert' on the trigger. Any help is appreciated.

1 Answer 1

1

The Id of a SObject is not available in BeforeInsert context. As this is running before the records are there in database.

Hence firing this

[SELECT Id FROM Opportunity WHERE Id = : oppIds LIMIT 1];

will return an empty List and when you try to store the return value into Opportunity opp, Salesforce tries to internally fetch the first item from list and store it in opp.

But as list is empty, hence you see an exception List has no rows for assignment to SObject

I am not aware of full use case, but you can try shifting your context to AfterInsert.

BTW, why to just query Id again, if you already have an Id from the trigger.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .