+ Start a Discussion

APEX Trigger - Script Exception



I have written the following trigger for my custom object -


trigger field_update on PM_Requests__c (after insert, after update, after delete) {
    for (PM_Requests__c pm : Trigger.new) {
        Product2 p = [Select p.name from Product2 p where p.name= :pm.Products__c];


 It does not seems to be working properly and produces an exception (whenever I add a new record for that object) as -


Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger field_update caused an unexpected exception, contact your administrator: field_update: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.field_update: line 3, column 22


Please try to help me on this.


Thanks in advance...

The assignment to a single object requires the query to return a single row, if the query may not return a row, you should assign it to an array, then examine the size of the array to determine if you got a row or not, and take the appropriate action.

Hey, for this exception, I am getting an email every time it occurs...


Can you tell me how do I stop receiving emails for the same?