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
BobBob 

Trigger to update custom object opportunity Id field

I am trying to add the opportunity object to this following code. I need the trigger to insert the opportunity Id (OppId__c) on the custom object Survey__c. This is what i have for my trigger code. Also,  when i try filling out the survey  I get the following error.

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


TRIGGER CODE:
trigger Trigger_SurveyAfterInsert on Survey__c (after insert) {
    for(Survey__c event: System.Trigger.New){
        List<Survey__c> lobjects;
        string name;
        lobjects = [select Id, Case__c, CaseId__c,Opportunity__c,OppId__c from Survey__c where Id=:event.Id];
        if(lobjects.size() == 1){
            Survey__c srv = lobjects[0];
            Case cs = [select Id, AccountId, ContactId, Service_Provider__c from Case where Id=:srv.CaseId__c ];
            Opportunity opp = [select Id, AccountId  from Opportunity where Id=:srv.OppId__c ];
            srv.Case__c = cs.Id;
            srv.Contact__c = cs.ContactId;
            srv.Account__c = cs.AccountId;
            srv.Service_Provider__c = cs.Service_Provider__c;
            srv.Opportunity__c = opp.Id;
            update srv;
        }
    }
    /*for(Survey__c svr:Trigger.new){
        svr.Case__c = svr.CaseId__c;
       
    }*/

}
Ramu_SFDCRamu_SFDC
try the workaround mentioned in this article 
http://help.salesforce.com/HTViewSolution?id=000159853&language=en_US
Boom B OpFocusBoom B OpFocus
I'm not sure how you get the values for CaseId__c and OppId__c, but here is my modified trigger:

trigger Trigger_SurveyAfterInsert on Survey__c (before insert) {
    Set<Id> setCaseIds = new Set<Id>();
    for(Survey__c srv: =Trigger.New){
         if (srv.caseId__c != null) setCaseIds.add(srv.caseId);
    }
    Map<Id, Case> mapCases = new Map<Id, Case>([select Id, AccountId, ContactId, Service_Provider__c from Case where Id in :setCaseIds]);
    for(Survey__c srv: =Trigger.New){
         if (mapCases.containsKey(srv.caseId__c)) {
               Case c = mapCases.get(srv.caseId__c);
              srv.Case__c = c.Id;
              srv.Account__c = c.AccountId;
              srv.Contact__c  = c.ContactId;
              srv.Service_Provider__c = c.Service_Provider__c;
         }
         if(srv.oppId__c != null) srv.Opportunit__c = srv.oppId__c;
    }
}

Best,

Boom