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
LaaralLaaral 

System.QueryException: List has no rows for assignment to SObject

Hi , I'm getting this error message : System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, SetupChangeTechnologyListCheck: execution of AfterInsert caused by System.QueryException: List has no rows for assignment to SObject: : line 8, column 1

I don't understand why it doesn't find account objects, because there are alot of them in SF ?  Where should I start looking for an answer to this? Many thanks already, if you could somehow help me understand this better.

 

trigger SetupChangeTechnologyListCheck on Setup__c (after insert, after update)
{

    for(Setup__c s : trigger.new)
    {
        
        Account acc = [SELECT Name, Service_Type_List__c FROM Account WHERE RecordType.Name = 'Main Account' AND Name = :s.Main_Account__c];
                
        UpdateMainAccountTechnologyList techList = new UpdateMainAccountTechnologyList();
        techList.UpdateList(acc);
                
    }
}

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

The exception is because of you are not getting any record in your SOQL query..

which meet the where criteria so it's give error.

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

All Answers

hitesh90hitesh90

The exception is because of you are not getting any record in your SOQL query..

which meet the where criteria so it's give error.

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

This was selected as the best answer
RoshRosh

Hi,

 Its always a best practice to check if list is not empty before doing any DML operation.

 

Your code could be modified tso that it doen't throw an exception:

 

trigger SetupChangeTechnologyListCheck on Setup__c (after insert, after update)
{

for(Setup__c s : trigger.new)
{

list<Account> accountList = [SELECT Name, Service_Type_List__c FROM Account WHERE RecordType.Name = 'Main Account' AND Name = :s.Main_Account__c];
Account acc = (accountList != null && accountList.size()>1) ? (accountList[0] : null;
if (acc != null) {
UpdateMainAccountTechnologyList techList = new UpdateMainAccountTechnologyList();
techList.UpdateList(acc);
}
}
}

 

Dmytro KuryloDmytro Kurylo
trigger SetupChangeTechnologyListCheck on Setup__c (after insert, after update) {
    AccountServices.doSomethingWithMainAccountTechnologyList( Trigger.new );
}

public with sharing class Accountservices {
    public static final String MAIN_ACCOUNT_RECORDTYPE_NAME = 'Main Account';
    public static void doSomethingWithMainAccountTechnologyList( List<Setup__c> newItems ) {
        Set<String> mainAccounts = new Set<String>();
        for( Setup__c s : newItems ) {
            mainAccounts.add( s.Main_Account__c );
        }
        mainAccounts.remove( null );
        if( !mainAccounts.isEmpty() ) {
            for( Account account : [ SELECT Name, Service_Type_List__c
                                                  FROM Account
                                                  WHERE RecordType.Name = : MAIN_ACCOUNT_RECORDTYPE_NAME
                                                       AND Name IN : mainAccounts ] {
                UpdateMainAccountTechnologyList techList = new UpdateMainAccountTechnologyList();
                techList.UpdateList( account );
            }
        }
    }
}