0

with a list of "mails", I want to search for emails in my salesforce object "PrincipalProduit__c", but I find errors in my variable "var and searchList"

public  List<String> getValo(){
List<String> mailList=getMail();
List<sObject> searchList = new List<sObject>();
List<String> var=new List<String>();
for(integer l=0;l<mailList.size();l++){
    searchList = [FIND :mailList[l] IN EMAIL FIELDS RETURNING PrincipalProduit__c(id)];   
    var.add((List<PrincipalProduit__c>)searchList[0]);
}
system.debug(var);
return var;
}

help me find the type I need to use to display my id

1

There are few things you need to change in your code so that it returns the List<PrincipalProduit__c> or List<String>

  • SOSL returns List<List<sObject>>, hence you should assign the returned results to a List<List<sObject>>'s instance.
  • You are adding List<PrincipalProduit__c> to a List<String> whereas it should be added to List<PrincipalProduit__c>'s instance.
  • As you will be adding List<PrincipalProduit__c> to a list i.e. var, you should be using the addAll method of List class.
  • Return type of your method should be List<PrincipalProduit__c> instead of List<String> or else, you can loop through the List<PrincipalProduit__c> and get the required string and then add it to the List that needs to be returned.
  • You should avoid using any query in for loop, having queries in loop would lead you to hitting the governor limits. Hence, you will have to remove your SOSL query from for loop to avoid hitting the limits.

A more optimized version of your method would be something like below, where SOSL has been removed from for loop and it will fetch all the needed records in one hit and it will return you List<PrincipalProduit__c>

public  List<PrincipalProduit__c> getValo(){
    List<String> mailList=getMail();
    List<List<sObject>> searchList = new List<List<sObject>>();
    List<PrincipalProduit__c> var=new List<PrincipalProduit__c>();

    string searchString = '';
    string finalSearchString = '';
    List<string> secondMailList = new List<String>();
    for(string mail : mailList){
        searchString = '("' + mail + '")';
        secondMailList.add(searchString);
    }
    finalSearchString = string.join(secondMailList,' OR ');
    searchList = [FIND :finalSearchString IN EMAIL FIELDS RETURNING PrincipalProduit__c(id)];   
    var.addAll((List<PrincipalProduit__c>)searchList[0]);

    system.debug(var);
    return var;
}

If you want your method to return List<String> instead of List<PrincipalProduit__c>, then your method should be

public  List<String> getValo(){
    List<String> mailList=getMail();
    List<List<sObject>> searchList = new List<List<sObject>>();
    List<String> var=new List<String>();

    string searchString = '';
    string finalSearchString = '';
    List<string> secondMailList = new List<String>();
    for(string mail : mailList){
        searchString = '("' + mail + '")';
        secondMailList.add(searchString);
    }
    finalSearchString = string.join(secondMailList,' OR ');
    searchList = [FIND :finalSearchString IN EMAIL FIELDS RETURNING PrincipalProduit__c(id)];   
    for(PrincipalProduit__c principalProd : (List<PrincipalProduit__c>)searchList[0]){
        var.add(principalProd.Id);
    }

    system.debug(var);
    return var;
}

You can get further help on SOSL at this link. If you still want to use your existing method where you have SOSL in loop, then this is how it would look like.

Note : Below method will hit the governor limits, if you have more than 20 email addresses in your mail list. You can find the governor limits of SOSL at this link.

This will return List<PrincipalProduit__c>

public  List<PrincipalProduit__c> getValo(){
    List<String> mailList=getMail();
    List<List<sObject>> searchList = new List<List<sObject>>();
    List<PrincipalProduit__c> var=new List<PrincipalProduit__c>();
    for(integer l=0;l<mailList.size();l++){
        searchList = [FIND :mailList[l] IN EMAIL FIELDS RETURNING PrincipalProduit__c(id)];   
        var.addAll((List<PrincipalProduit__c>)searchList[0]);
    }
    system.debug(var);
    return var;
}

This will return List<String>

public  List<String> getValo(){
    List<String> mailList=getMail();
    List<List<sObject>> searchList = new List<List<sObject>>();
    List<String> var=new List<String>();
    for(integer l=0;l<mailList.size();l++){
        searchList = [FIND :mailList[l] IN EMAIL FIELDS RETURNING PrincipalProduit__c(id)];   
        for(PrincipalProduit__c principalProd : (List<PrincipalProduit__c>)searchList[0]){
            var.add(principalProd.Id);
        }
    }
    system.debug(var);
    return var;
}
| improve this answer | |
  • @Ganji : I used your method and she shows me "Too many SOSL queries: 21" – corentin KOUNOU May 6 '19 at 8:41
  • You will get that error because you are doing your SOSL in for loop, you will have to change your logic to avoid doing a query in for loop. – Vijay Ganji May 6 '19 at 8:43
  • @ Ganji same erreur – corentin KOUNOU May 6 '19 at 8:57
  • I think you are getting this error because your mailList.size() is greater than 20, and in each iteration of for loop you are executing SOSL query. Maximum number of SOSL query in a single transaction can be 20. You need to separate the transactions here using batch apex . Execute it with the batch size of 20 or less. – Manish Anand May 6 '19 at 9:11
  • @Manish A i don't think beacause in my object, i have that seven données – corentin KOUNOU May 6 '19 at 9:15
0

SOSL returns list of list of Sobject (Two-Dimensional array) searchlist should be

List<List<Sobject>> searchlist = new List<List<Sobject>> ();

and var should be

List<PrincipalProduit__c> var = new List<PrincipalProduit__c> ();
| improve this answer | |
0

SOSL queries return a list of lists of SObject (List<List<SObject>>). Additionally, if you want to dynamically generate the search fields you will need to use dynamic SOSL like:

String query = 'FIND \'' + String.join(mailListWithQuotesEscaped, '\',\'') + '\' IN EMAIL FIELDS RETURNING ' + buildListOfFields();
List<List<SObject>> searchList = Search.query(query);

My final point; you should avoid performing any form of query in a for loop. Try to bulkify the query so you build a single query then process the returned values to split the data back up again.

Take a look at the SOSL documentation, including the trailhead, handling the results, the dynamic SOSL stuff and the reference.

| improve this answer | |
  • I have an error "unexpected token: 'FINDeasyfit.vlg'" after trying your method – corentin KOUNOU May 6 '19 at 8:35
  • I forgot to show adding the quotes. I have updated. – Phil W May 6 '19 at 8:37

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.