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
sandy sfdcsandy sfdc 

Apex script unhandled trigger exception by user/organization

Hi All,

I got the below runtime error,please suggest on this.

Apex script unhandled trigger exception by user/organization: 00570000004Tasp/00D900000008xzp

Opportunitytrigger: execution of BeforeUpdate

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

Trigger.Opportunitytrigger: line 3, column 1

My Trigger Code is like:
trigger OpportunityTrigger on Opportunity(before insert,before update,after insert , after update , before delete) 
{
    User validuser=[select Namespace.Active__c,Namespace.Pset__c,Namespace.code__c from User where Id=:UserInfo.getUserId()];

   if(validuser.Namespace.Active__c==true && validuser.Namespace.Pset__c==true && validuser.Namespace.code__c!=null)
   {

      //code

    }
Best Answer chosen by sandy sfdc
VIVEK 998VIVEK 998
I would suggest you to go with 1st as In 2nd if you won't get record of User than you will get a null reference error in if condition.
Mark as answer if you find correct.
Thanks!

All Answers

VIVEK 998VIVEK 998
Can you please update your query like this :
User validuser;
for(User usr : [select Namespace.Active__c,Namespace.Pset__c,Namespace.code__c from User where Id=:UserInfo.getUserId()]){
    validuser = usr;
}
 
sandy sfdcsandy sfdc
Thanks ur response,

In my view below two possibilities are working properly.
Can u please suggest on this.

List<User> validuser=[select Namespace.Active__c,Namespace.Pset__c,Namespace.code__c from User where Id=:UserInfo.getUserId()];

if(validuser!=null && validuser.size()==1 && validuser[0].Namespace.Active__c==true && validuser[0].Namespace.Pset__c==true && validuser[0].Namespace.code__c!=null)
{

}


or 


User validuser=null;
try{
     User validuser=[select Namespace.Active__c,Namespace.Pset__c,Namespace.code__c from User where Id=:UserInfo.getUserId()];
}
catch(execption e){}

if(validuser.Namespace.Active__c==true && validuser.Namespace.Pset__c==true && validuser.Namespace.code__c!=null)
{

}


 
VIVEK 998VIVEK 998
I would suggest you to go with 1st as In 2nd if you won't get record of User than you will get a null reference error in if condition.
Mark as answer if you find correct.
Thanks!
This was selected as the best answer
sandy sfdcsandy sfdc
1st is the best solution,
By adding  ​validuser!=null to if condition in 2nd solution it will be solved.