+ Start a Discussion
BobBob 

Visualforce Error System.DmlException: Insert failed. First exception on row 0

I would like some help on a trigger and apex class i am working on. Below is the code.  It occurs when i try to submit the survey.

ERROR CODE:
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Trigger_SurveyAfterInsert: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject Trigger.Trigger_SurveyAfterInsert: line 8, column 1: []
Error is in expression '{!insertSurvey}' in component <apex:commandButton> in page vf_sales_survey: Class.VFController_survey_opps.insertSurvey: line 44, column 1

Class.VFController_survey_opps.insertSurvey: line 44, column 1



TRIGGER:
trigger Trigger_SalesSurveyAfterInsert on Survey__c(after insert) {

   
     for(Survey__c event: System.Trigger.New){
        List<Survey__c> lobjects;
        string name;
        lobjects = [select Id, Opportunity__c, OppId__c from Survey__c where Id=:event.Id];
        if(lobjects.size() == 1){
            Survey__c srv = lobjects[0];
            Opportunity opp = [select Id, AccountId from Opportunity where Id=:srv.Opportunity__c  ];
            srv.Opportunity__c = opp.Id ;
            srv.Account__c = opp.AccountId ;
           
            update srv;
        }
    }


}


APEX CODE:
public with sharing class VFController_survey_opps {
public String oppId {get;set;}

        public Integer q1{get;set;}
        public Integer q2{get;set;}
        public Integer q3{get;set;}
        public Integer q4{get;set;}
        public Integer q5{get;set;}
        public Integer q6{get;set;}
        public Integer q7{get;set;}
        public Integer q8{get;set;}
        public Integer q9{get;set;}
        public Integer q10{get;set;}
        public Integer q11{get;set;}
        public Integer q12{get;set;}
        public Integer q13{get;set;}
        public Integer q14{get;set;}
        public String comments{get;set;}
       
        public VFController_survey_opps()
        {
                oppId = ApexPages.currentPage().getParameters().get('oppId');
        }
       
        public PageReference insertSurvey(){
                Survey__c sv = new Survey__c();
             
                sv.OppId__c = oppId;
                sv.Question_1__c = q1;
                sv.Question_2__c = q2;
                sv.Question_3__c = q3;
                sv.Question_4__c = q4;
                sv.Question_5__c = q5;
                sv.Question_6__c = q6;
                sv.Question_7__c = q7;
                sv.Question_8__c = q8;
                sv.Question_9__c = q9;
                sv.Question_10__c = q10;
                sv.Question_11__c = q11;
                sv.Question_12__c = q12;
                sv.Question_13__c = q13;
                sv.Question_14__c = q14;
                sv.Comments__c = comments;
                insert sv;
               
                //put a redirect to a new thank you vf page
               
                PageReference cPage = new PageReference('/apex/vr_survey_thank_you');
        cPage.setRedirect(true);
        return cPage;
       
                return null;
        }
       
         public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('5','Excellent'));
        options.add(new SelectOption('4','Good'));
        options.add(new SelectOption('3','Average'));
        options.add(new SelectOption('2','Fair'));
        options.add(new SelectOption('1','Poor'));
        options.add(new SelectOption('0','NA'));
       
       
        return options;
    }
   
    public List<SelectOption> getItems2() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('5','Excellent'));
        options.add(new SelectOption('4','Good'));
        options.add(new SelectOption('3','Average'));
        options.add(new SelectOption('2','Fair'));
        options.add(new SelectOption('1','Poor'));
        options.add(new SelectOption('0','NA'));
       
        return options;
    }

 
}
kibitzerkibitzer
You have:

lobjects = [select Id, Opportunity__c, OppId__c from Survey__c where Id=:event.Id];

The ID field of Survey__c will never be the same as the ID of the event.

The query is returning no results (null) and thus failing with an exception


AshlekhAshlekh
Hi,

Your trigger code is totally wrong because you are not following best practise and the record which you are getting through trigger.new then why are run soql to retrive it again.

Below is the link which help you to understand the best practices of trigger

https://developer.salesforce.com/page/Apex_Code_Best_Practices


BobBob
What should I replace event.Id with? Robert A. Poliquin Salesforce.com Administrator CYBEX 10 Trotter Drive Medway, MA 02053 USA t 774-324-8181 f 508.533.5500 e bpoliquin@cybexintl.com w www.cybexintl.com [sf_cert_adm_rgb] Join CYBEX on Facebook & Twitter! Check out our Blog
kibitzerkibitzer
Hi Bob:

Sorry - I was mistaken. I got confused between event objects and your use of 'event' as a variable.

On the survey__c object, you set the field sv.OppId__c = oppId;
But in the query you query for Id=:srv.Opportunity__c

Looks like you have two different opportunity lookup fields and you're setting one and querying the other.

Dan