+ Start a Discussion
ThomasmThomasm 

Trigger problem

I am trying to get build a trigger when the custom object sitevist has the record type daily select and the field baseboard_stairway__C is false then create a care.  i saved without any errors but then i save the new sitevisit i get the following error

 

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

 

Here is my code:  please show me what i am missing

 

 

trigger SV_Cases on SiteVisit__c (after insert) {
 List<Case>cases=new List<Case>();
  Id rtId = [select Id,name from RecordType where name='Label of the record type' and SObjectType='sitevisit' limit 1].Id;  
    for(sitevisit__C b:Trigger.new)
        {
             if(rtID=='012a0000001NYfi'){
     if(b.baseboard_stairway__c == 'False' )
    {
    Case newCase=new Case(subject='Test',Status='New',Origin='Web');
    insert newCase; 

Starz26Starz26

This line:

 

Id rtId = [select Id,name from RecordType where name='Label of the record type' and SObjectType='sitevisit' limit 1].Id;  

 

Is not returning any records.

 

Change it to:

 

RecordType[] rtId = [select Id,name from RecordType where name='Label of the record type' and SObjectType='sitevisit' limit 1];

If rtid.isEmpty(){

  //Throw error as nothing was found

}else{

//Process code here

}

 

ThomasmThomasm
how to i get it to run the record type
Satyendra RawatSatyendra Rawat

Hi,

 

Update the your Code.

1.)

Id rtId = [select Id,name from RecordType where name='Label of the record type' and SObjectType='SiteVisit__c' limit 1].Id;  

2.)

  if(rtID==b.RecordTypeId)