0

UPDATE:

trigger ListCheck on Setup__c (after update)
{
   list<Id> empIdList = new list<Id>(); 
   for(Setup__c s : trigger.new)
    {
      empIdList.add(s.EmpId__c);

    }

    list<Employee__c> empList = [SELECT Id, Name FROM Employee__c WHERE Id in : empIdList];

    if(empList.size() > 0){
     for(EMployee__c e: empList){
       e.status = 'completed'; 
       e.last_num__c = empIdList.Id;  //<<<<<error
      }
    update empList;
   }
}

Error:

Initial term of field expression must be a concrete SObject: List

I'm getting this error message : I have searched before I'm posting and could not find the solution to my issue, please have a look below.

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

trigger ListCheck on Setup__c (after update)
{
    for(Setup__c s : trigger.new)
    {
        Employee__c emp = [SELECT Id, Name FROM Employee__c WHERE Id =: s.Id];

        if(emp != null)
        {
          //do more work.... 
        }               
    }
}
2
 trigger ListCheck on Setup__c (after update)
{
   list<Id> empIdList = new list<Id>(); 
   for(Setup__c s : trigger.new)
    {
      empIdList.add(s.EmpId__c);

    }

    list<Employee__c> empList = [SELECT Id, Name FROM Employee__c WHERE Id in : empIdList];

    if(empList.size() > 0){
     for(EMployee__c e: empList){
       e.status = 'completed'; 
      }
    update empList;
   }
}
  • inside for loop is it safe to use the update ? for an example: for(EMployee__c e:empList){ e.status = 'completed'; update e; ? – Nick Kahn Jun 15 '15 at 19:41
  • @Abu Hamzah u need to do outside for loop.I just updated – sfdc Jun 15 '15 at 19:43
  • Thanks I have updated my question, getting an error please have look at my question. – Nick Kahn Jun 15 '15 at 19:49
  • Are you trying to update e.last_Num__c with employee Id (e.Id) to..if so then u need to update e.last_num___c = e.Id; – sfdc Jun 15 '15 at 19:57
1

Firstly, the query is incorrect,

Employee__c emp = [SELECT Id, Name FROM Employee__c WHERE Id =: s.Id];

the error is due to the fact that you are querying within Employee__c object and sending id of Setup__c. As there are no records, the resultant query is unable to assign the result into "Employee__c emp"

So, you should revisit, how you want to retrieve employees using Setup__C id (probably some relationship that you have setup there)

Secondly, it is incorrect to use a query within For loop, as it can lead to breach of governor limits. Suggest you to refer:-

https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code

General trigger bulkification - best practices

  • Thanks for the suggestion and I do understand that if there is no match then why throwing the error? am I missing something? – Nick Kahn Jun 15 '15 at 19:29
  • Employee__c emp = [SELECT Id, Name FROM Employee__c WHERE Id =: s.Id]; This code tries to take the first item from list and assign it to the variable, as the list (query result) is null, hence the error. – Anshul Jun 15 '15 at 19:30
  • I'm not sure if I understand the logic here but just fyi I'm not using a List of records here just get me the record if match otherwise null – Nick Kahn Jun 15 '15 at 19:33
  • The query statement "[SELECT Id, Name FROM Employee__c WHERE Id =: s.Id]" implicitly returns a list, which is having a result as null. As you are directly assigning result to a variable, you can make it as "List<Employee__c> emp" , to avoid this error. – Anshul Jun 15 '15 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.