0

I'm new to Salesforce and this loop is bugging me:

public String matchingDataGridIDs {get;set;}

    public void GetData(string defaultUserID)
    {
        //retrieve data for selected user
        List<myData> matchingDataGrid = [Select Id, Name, CreatedById, CreatedDate where Id = :defaultUserID];

        for (List<myData> i :matchingDataGrid.Count)
        {   
            matchingDataGridIDs =+ matchingDataGrid.Id + ',';
        }
}

I get the following error:

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

1
  • Your query will only ever return one or zero rows as you have where Id = :defaultUserID. Is that what you intend? – Keith C Oct 19 '15 at 13:28
3

The way that your code reads here

List matchingDataGrid = [Select Id, Name, CreatedById, CreatedDate where Id = :defaultUserID];

    for (List<myData> i :matchingDataGrid.Count)

You are assuming that matchingDataGrid is a list of lists, which it is not. You want to loop over each object within the list, so the format for doing that would be

for(myData i : matchingDataGrid)

The full code would look something like this.

public String matchingDataGridIDs {get;set;}

    public void GetData(string defaultUserID)
    {
        //retrieve data for selected user
        List<myData> matchingDataGrid = [Select Id, Name, CreatedById, CreatedDate where Id = :defaultUserID];

        for (myData i :matchingDataGrid)
        {   
            matchingDataGridIDs += i.Id + ',';
        }
}
5

There are multiple problems in your code.

1.

List<myData> matchingDataGrid = [Select Id, Name, CreatedById, CreatedDate where Id = :defaultUserID];

you should also add a FROM clause to your query. Also, if you use a custom object do not forget the __c after it

2.

for (List<myData> i :matchingDataGrid.Count)

if you use count() method, do not forget the () at the end. Also, using List<myData> is only possible if you have the list of lists or a map with lists. However, I believe that you want something else:

for (myData i : matchingDataGrid)

this will loop through the list and each element will be i in the loop, so you can replace this:

matchingDataGridIDs =+ matchingDataGrid.Id + ',';

with this:

matchingDataGridIDs =+ i.Id + ',';
  1. And last, your query will only return at most 1 record, because Id is unique, so you will have 0 or 1 records in your list. Why would you want to loop through 1 element?

2

On the general point of looping through collections, I suggest it is worth getting familiar with the SOQL For Loops option where the query is done inline:

public void populateMatchingDataGridIDs(Id defaultUserId) {
    Id[] ids = new Id[] {};
    // Note this query will return at most one row so a loop is a bit OTT
    for (MyObject__c o : [
            select Id
            from MyObject__c
            where Id = :defaultUserId
            ]) { 
        ids.add(o.Id);
    }
    matchingDataGridIDs = String.join(ids, ',');
}

This has the advantage (for large collections) of automatically working though the data in batches and so reducing heap requirements. It also gets rid of an unnecessary variable. So IMHO it is the default pattern to use.

If you don't care about the order of the IDs (which presumably you don't as there is no order by in the where) there is a convenient map constructor available that could be used in this case:

public void populateMatchingDataGridIDs(Id defaultUserId) {
    Map<Id, MyObject__c> m = new Map<Id, MyObject__c>{[
            select Id
            from MyObject__c
            where Id = :defaultUserId
            ]});
    matchingDataGridIDs = String.join(new List<Id>(m.keySet()), ',');
}
1

You need to modify your code in following way

public String matchingDataGridIDs {get;set;}

public void GetData(string defaultUserID)
{
    //retrieve data for selected user
    List<myData> matchingDataGrid = [Select Id, Name, CreatedById, CreatedDate from myData where Id = :defaultUserID];

    //here you have problem with your code. you are passing matchingdatagrid.count but this for loop accept list of items
    for (myData i :matchingDataGrid)
    {   
        matchingDataGridIDs =+ i.Id + ',';
    }
}

I noticed one more thing. your SOQL return only 1 single row because you are querying data on the basis of id, I assume that you will update that with any other logic

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.