Activity Forums Salesforce® Discussions Can we use SOSL statements in Salesforce triggers?

  • shradha jain

    Member
    August 20, 2018 at 7:32 am

    Hello Sanjana,

    Yes, you can write SOSL inside triggers.You can test using the following Code in your Org.
    trigger trg_AccountSOSL on Account (before insert, before update) {
    List<List<SObject>> searchList = [FIND ‘map*’ IN ALL FIELDS RETURNING Account (Id, Name), Contact, Opportunity, Lead];
    List<account> myAcc = ((List<Account>)searchList[0]);
    system.debug(myAcc[0].name);
    }

    Thanks.

  • Parul

    Member
    September 14, 2018 at 4:28 am

    Yes you can use SOSL in which trigger in which the data resides.

    Retrieve data for a specific term that you know exists within a field. SOQL can searches are faster and can return more relevant results.
    It finds the search text efficiently from multiple objects whether they are related to each other or not.
    Retrieve data that’s in Chinese, Japanese, Korean, or Thai. Morphological tokenization for CJKT terms helps ensure accurate results.

     

    List<list<SObject>> accountSearchList =newList<List<SObject>>();
    accountSearchList=[FIND {Joe Smith}IN Name Fields RETURNING
    lead (name, phone Where createddate = THIS_FISCAL_QUARTERLIMIT 20) ];
    list<lead > accs;
    accs=(list<lead>)result[0];
    system.debug(accs);
    Example of SOSL

     

    Thanks

  • shariq

    Member
    September 14, 2018 at 6:31 am

    Hi,

    Yes, you can write SOSL inside triggers. Below are some points about SOSL :

    SOSL is a search language in salesforce and the important feature is that Unlike SOQL, we can search in multiple objects at same time using SOSL. In SOQL, we can query only one object at a time but in SOSL, We can search for some specified string like ‘testString’ in multiple objects at the same time.

    We can search for some specified string like ‘testString’ in multiple objects at the same time.
    We can mention in which fields of all the sObjects,we want to search for the string specified.
    The SOSL query start with the keyword ‘FIND’.
    You can specify, which fields to return for each object mentioned in SOSL query. Suppose you have performed search on three objects Account, Contact & Opportunity. Then you can mention like, for list returned with Account results only (Name, Industry) fields should be returned, and for Contacts results (firstName, lastName) should be returned and similarly for Opportunity.
    The result of SOSL is a list of lists of sObjects “List<List<sObject>>”.
    The returned result contains the list of sObjects in the same order as order mentioned in SOSL  query.
    If a SOSL query does not return any records for a specified sObject type, then search results include an empty list for that sObject.
    The search string should be at least two characters long.

    Hope this helps !

  • Avnish Yadav

    Member
    September 14, 2018 at 1:35 pm

    Hello,

    We can't use SOSL in Triggers Because the apex developer's guide says so-

    SOSL statements evaluate to a list of lists of sObjects, where each list contains the search results for a particular sObject type. The result lists are always returned in the same order as they were specified in the SOSL query. SOSL queries are only supported in Apex classes and anonymous blocks. You cannot use a SOSL query in a trigger.

    Thanks.

     

Log In to reply.

Popular Salesforce Blogs