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
prabhat jhaprabhat jha 

list has no rows for assignment to Sobject in triggers

i have written one trigger to insert a new Account species/consultation record  (ZTS_EU_Account_Species_Potential__c) if species name matches the species listed in my custom setting (SpeciesList__c) which has three fields parentspeciesname,childspeciesname  and one factor (number field).but my insert statement fails and throws the mentioned error ,i am stuck with this error for previous 24 hrs ,any help would be appreciated.here species is a different object (ZTS_EU_Species__c ) and lookup field of Account consultation records.
here is my trigger:
trigger createChildSpecies on ZTS_EU_Account_Species_Potential__c (before Insert) {
    ID accctid;
    ID Speciesid;
    ID childspeciesid;
    String Speciesname;
    Double Parentcount;
    String CHLDSpeciesname;
    String myCCVal ;
    ZTS_EU_Species__c test=new ZTS_EU_Species__c ();
    ZTS_EU_Species__c childtestforid=new ZTS_EU_Species__c ();
    
    
    
    ZTS_EU_Account_Species_Potential__c PARENTRECORD;
    ZTS_EU_Account_Species_Potential__c chld=new ZTS_EU_Account_Species_Potential__c();
    for(ZTS_EU_Account_Species_Potential__c a:trigger.new)
    {
        //PARENTRECORD.Account__c=a.Account__c;
        Speciesid=a.ZTS_EU_Species_Potential__c;
        system.debug('parentspeciesnameeee===='+Speciesid);
        test=[SELECT Name FROM ZTS_EU_Species__c WHERE ZTS_EU_Market__c = 'Spain' AND Id =:Speciesid];
        Speciesname=test.Name ;
        system.debug('speciesname===='+Speciesname);
        Map<String, SpeciesList__c> mapStatusCodeCustomSetting = SpeciesList__c.getAll();
        for(SpeciesList__c mandatoryRoles : mapStatusCodeCustomSetting.values()){
            if (mandatoryRoles .ParentSpeciesName__c==Speciesname)
            {
                myCCVal = mandatoryRoles .ChildSpeciesName__c;
                childtestforid=[SELECT Id FROM ZTS_EU_Species__c WHERE ZTS_EU_Market__c = 'Spain' AND Name =:myCCVal ];
                system.debug('myCS1 ===='+myCCVal);
                
            }
        }
            
            
            
            chld.Account__c=a.Account__c;         
            childspeciesid=childtestforid.Id;
            system.debug('childspeciesid===='+childtestforid);
            chld.ZTS_EU_Species_Potential__c=childspeciesid;
            chld.ZTS_EU_Animal_Consultations_new__c=10;
            insert(chld);
    }
    
}

 
Best Answer chosen by prabhat jha
prabhat jhaprabhat jha
@Amit Chaudhary 8
i solved it using Recursion to stop Firing of Recursive Trigger.
Thanks for your help

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Try to change your code like below
trigger createChildSpecies on ZTS_EU_Account_Species_Potential__c (before Insert) {
    ID accctid;
    ID Speciesid;
    ID childspeciesid;
    String Speciesname;
    Double Parentcount;
    String CHLDSpeciesname;
    String myCCVal ;
    ZTS_EU_Species__c test=new ZTS_EU_Species__c ();
    ZTS_EU_Species__c childtestforid=new ZTS_EU_Species__c ();
    
    
    
    ZTS_EU_Account_Species_Potential__c PARENTRECORD;
    ZTS_EU_Account_Species_Potential__c chld=new ZTS_EU_Account_Species_Potential__c();
    for(ZTS_EU_Account_Species_Potential__c a:trigger.new)
    {
        //PARENTRECORD.Account__c=a.Account__c;
        Speciesid=a.ZTS_EU_Species_Potential__c;
        system.debug('parentspeciesnameeee===='+Speciesid);
		
        List<ZTS_EU_Species__c> Listtest=[SELECT Name FROM ZTS_EU_Species__c WHERE ZTS_EU_Market__c = 'Spain' AND Id =:Speciesid];
		if(Listtest.size() > 0 )
		{
			Speciesname = Listtest[0].name;
			Speciesname=test.Name ;
		}
        system.debug('speciesname===='+Speciesname);
        Map<String, SpeciesList__c> mapStatusCodeCustomSetting = SpeciesList__c.getAll();
        for(SpeciesList__c mandatoryRoles : mapStatusCodeCustomSetting.values())
		{
            if ( Speciesname != null && mandatoryRoles.ParentSpeciesName__c==Speciesname)
            {
                myCCVal = mandatoryRoles .ChildSpeciesName__c;
                
				List<ZTS_EU_Species__c> lstchildtestforid = [SELECT Id FROM ZTS_EU_Species__c WHERE ZTS_EU_Market__c = 'Spain' AND Name =:myCCVal ];
				if(lstchildtestforid.size() > 0 )
				{
					childtestforid=lstchildtestforid[0];
                }
				system.debug('myCS1 ===='+myCCVal);
                
            }
        }
            
            
            
            chld.Account__c=a.Account__c;    
			if(childtestforid != null)		
				childspeciesid=childtestforid.Id;
            
			system.debug('childspeciesid===='+childtestforid);
            chld.ZTS_EU_Species_Potential__c=childspeciesid;
            chld.ZTS_EU_Animal_Consultations_new__c=10;
            insert(chld);
    }
    
}

Let us know if this will help you
 
prabhat jhaprabhat jha
i tried your code but it throws an errror
(Review all error messages below to correct your data.
Apex trigger createChildSpecies caused an unexpected exception, contact your administrator: createChildSpecies: execution of BeforeInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, createChildSpecies: maximum trigger depth exceeded ZTS_EU_Account_Species_Potential trigger event BeforeInsert for [new] ZTS_EU_Account_Species_Potential trigger event BeforeInsert for [new] ZTS_EU_Account_Species_Potential trigger event BeforeInsert for [new] ZTS_EU_Account_Species_Potential trigger event BeforeInsert for [new] ZTS_EU_Account_Species_Potential trigger event BeforeInsert for [new] ZTS_EU_Account_Species_Potential trigger event BeforeInsert for [new] ZTS_EU_Account_Species_Potential trigger event BeforeInsert for [new] ZTS_EU_Account_Species_Potential trigger event BeforeInsert for [new] ZTS_EU_Account_Species_Potential trigger event BeforeInsert for [new] ZTS_EU_Account_Species_Potential trigger event BeforeInsert for [new] ZTS_EU_Account_Species_Potential trigger event BeforeInsert for [new] ZTS_EU_Account_Species_Potential trigger event BeforeInsert for [new] ZTS_EU_Account_Species_Potential trigger event BeforeInsert for [new] ZTS_EU_Account_Species_Potential trigger event BeforeInsert for [new] ZTS_EU_Account_Species_Potential trigger event BeforeInsert for [new] ZTS_EU_Account_Species_Potential trigger event BeforeInsert for [new]: []: Trigger.createChildSpecies: line 55, column 1)
prabhat jhaprabhat jha
@Amit Chaudhary 8
i solved it using Recursion to stop Firing of Recursive Trigger.
Thanks for your help
This was selected as the best answer