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
sridhar sridharsridhar sridhar 

Trigge query exception

Apex trigger casetriger caused an unexpected exception, contact your administrator: casetriger: execution of BeforeUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.casetriger: line 63, column 1
kirubakaran viswanathankirubakaran viswanathan
Hi Sridhar,

Before assigning the List values to Sobject, check the list size and proceed further.
This will solve the List has no rows exceptions. Example
 
Account[] records = [SELECT Id, Name, AccountNumber FROM Account];

if(records!=null && !records.isEmpty()) {
    for(Account record: records) {
        // Do Something Here
    }
}

 
sridhar sridharsridhar sridhar
Hi Viswanatha
thanks for reply,
the below is the error throwing line in the code
user  caoner = [select id,profileid,profile.name from User where id = :caseerid];
kirubakaran viswanathankirubakaran viswanathan
Hi Sridhar,

Can you please copy your trigger here. I believe Caseerid doesn't have any value.
It will be easy to troubleshoot if you paste the whole trigger.

Thanks,
Kiruba. V
Amit Chaudhary 8Amit Chaudhary 8
Please update your code like below
List<user>  Listcaoner = [select id,profileid,profile.name from User where id = :caseerid];
User caoner;

If(Listcaoner.size() > 0 )
{
	caoner = Listcaoner[0];
}

Let us know if this will help you
 
sridhar sridharsridhar sridhar
Hi Amit .

I am not getting error now.. but thing is .. my purpose for the trigger is not fullfilled.
the above trigger should restrict the users to close the case except those two profiles(Distribution division and Finance Division).

requirment is: No profile user allowed to close the cases of other profile users.
conditon: only those two profiles allowed to close the cases each other...

i am not getting what is the wrong with the trigger..

if you do the changes as per requirment please do it.. thanks for the replies
sridhar sridharsridhar sridhar
There are 50 profiles is there in my org...
requirment: should not allow users to close other users cases
conditions: but only 3 profile users are allowed to close cases each other and thery are not allowed to close remaining 47 profile users  and queue cases.
Amit Chaudhary 8Amit Chaudhary 8
Try to update your code like below
trigger caseontr on Case (before insert, before update) {
 
    if(Trigger.isBefore)
	{
        if(Trigger.isUpdate )
	    {
			set<string> ktpr = new set<string>();  

			user ur = [select id,profileid,profile.name from User where id =: UserInfo.getUserId()];
			system.debug('userprofile' +ur.profile.name);
			ktpr.add(ur.profile.name);
			string loginuserprof = ur.profile.name;
			string otherprofuser1 = 'Distribution Division';
			string otherprofuser2 = 'Finance devision';
			system.debug('Loginuserprofile' +pr);
			 set<id> loginuserids = new set<id>();
			 set<id> recordowner = new set<id>();
			 map<id,string> listcase = new map<id,string>();
			 id caseownerid;
			 id casegroupownerid;
			 boolean casestatus;  
			 for(case c: trigger.new)
			 {
			 
				system.debug('ownerid'+c.Ownerid);
				 
				 if(c.Status == 'closed'){
					casestatus = true;
				 
				 // This is a user owned case
				  if(c.owner.type=='User') 
				  {
					system.debug('ownerid'+c.Ownerid);
					caseownerid = c.ownerid;
				  }
				   // This is a queue owned case
				
				  if(c.owner.type=='Queue') 
				  {
					system.debug('Queueownerid'+c.Ownerid);
					casegroupownerid = c.ownerid;
				 }
				// recordowner.add(c.ownerid);
				 //listcase.put(c.ownerid,c.Status);
			 }   }        
			 Boolean accesstrue;
			 if(casestatus == true){
				 system.debug('listcase keyset'+ listcase.keySet() );
				 
				if(caseownerid != null)
				{  
					
					List<user>  Listcaoner = [select id,profileid,profile.name from User where id = :caseownerid];
					User caseowner;

					If(Listcaoner.size() > 0 )
					{
						caseowner = Listcaoner[0];
					}
					
					//user  caseowner = [select id,profileid,profile.name from User where id = :caseownerid];
					
					system.debug('caseowner'+caseowner);
					if(	caseowner != null && (caseowner.profile.name==otherprofuser1  || caseowner.profile.name== otherprofuser2) )
					{
						if(loginuserprof == otherprofuser1  || loginuserprof == otherprofuser2)
						{
							accesstrue = true;
						}
					}
						system.debug('Boolean flag true'+accesstrue );
						if(caseowner != null && ( caseowner.profile.name == loginuserprof || accesstrue == true) )
						{
							system.debug('caseownerprofile valied');
						}
						else
						{
							system.debug('caseownerprofile not valied');
							Trigger.new[0].addError(('Please check you do not have permission to work on this Case.'));
					   }
				}
				else
				{
					list< user > caseowner = [select id,profileid,profile.name from User where id = :casegroupownerid];
					for( user u : caseowner)
					{
						system.debug('Queuecaseowner'+ u);
							if(u.profile.name==otherprofuser1  || u.profile.name== otherprofuser2 )
							{
								if(loginuserprof == otherprofuser1  || loginuserprof == otherprofuser2)
								{
									accesstrue = true;
								}
							}
					}
					
					 system.debug('Boolean flag true'+accesstrue );
					 if( caseowner != null && (caseowner.profile.name == loginuserprof || accesstrue == true) )
						{
					   system.debug('caseownerprofile valied');
					   }else{
					   system.debug('caseownerprofile not valied');
						Trigger.new[0].addError(('Please check you do not have permission to work on this Case.'));
					   }
				}
			  
				
					   
				 }
		}
    }
	
}

 
sridhar sridharsridhar sridhar
Error: Compile Error: Initial term of field expression must be a concrete SObject: List<User> at line number 98