0

Can anyone please tell me, what should I do to avoid this error.

Error : Loop variable must be of type Id.

What I am trying to do is,From incident object list and task object list, I am finding out the task which are linked to incident and collecting those task in list of type Id.

Place where I am getting error

Line number where there is an error

                                                                                                                                                                                                                          Below is the code which I have written. As you said to you use map here but for Id will it hold string type?   

    List<Id> t1=new List<Id>();
    for(Integer j=0;j<resolvedIncident.size();j++)
    {
        for(Integer k=0;k<task.size();k++)
        {
            if(resolvedIncident.get(j).Id==task.get(k).BMCServiceDesk__FKIncident__c)
            {

                t1.add(task.get(k).BMCServiceDesk__FKIncident__c);               
                count=t1.size();                    
            }                
        }

        List<BMCServiceDesk__Task__c> T = new List<BMCServiceDesk__Task__c>();
        for(BMCServiceDesk__Task__c updateTask : t1) // @ this line error 
        {
            updateTask.BMCServiceDesk__FKStatus__c = closedStatusID.id;
            T.add(updateTask);

        }  

Please suggest for the same. Thanks!

0

For iterating through for loop, you must have a List/Set type variable.

Since you've Id in t1 then better use for loop as below:

// Initialize a list of task which needs to be updated
List<BMCServiceDesk__Task__c> lstUpdateTask = new List<BMCServiceDesk__Task__c> ();
for(Integer j=0;j<resolvedIncident.size();j++)
{
    for(Integer k=0;k<task.size();k++)
    {
        if(resolvedIncident.get(j).Id==task.get(k).BMCServiceDesk__FKIncident__c)
        {
            // add matched task into the list which needs to be updated.
            lstUpdateTask.add(task.get(k));                  
        }                
    }
}   

// Now we've list of Task which needs to be updated, simply run a for loop on it
for(BMCServiceDesk__Task__c updateTask : lstUpdateTask) {
    updateTask.BMCServiceDesk__FKStatus__c = closedStatusID.id;
} 

// Once all values are updated, simply update it.
if(lstUpdateTask.size () > 0)
    update lstUpdateTask;
  • Actually this t1 is a list created above which stores task Id. So, I have written t1 here. How can I replace T with t1? As t1 holds task Id whose status I want to set to closed. – Emma Watson May 10 '17 at 6:43
  • @EmmaWatson Updated the answer. And it would be better to post a code instead of a snapshot. – Rohit Mourya May 10 '17 at 6:45
  • How will this update task object status then, if I wrote for(Id taskId: t1){// code block} Because list is of type Id and I want these task Id to be updated in task object status. Please suggest for the same. – Emma Watson May 10 '17 at 6:49
  • @EmmaWatson In that case you need to refine your logic using Map something like which I posted in my answer. – Rohit Mourya May 10 '17 at 6:52
  • Can anyone please update me on this??? – Emma Watson May 10 '17 at 7:22
0

The problem in the loop is that when we are iterating using the below code

for(BMCServiceDesk__Task__c updateTask : t1) // @ this line error 

The system is expecting the the variable t1 should be of type object - BMCServiceDesk__Task__c instead of having a list<id> try to use list<BMCServiceDesk__Task__c>

Hope this answers the questions :)

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.