• #29221
    Salesforce | Sanjana Sanjana #311

    Can we use SOSL statements in Salesforce triggers?

    Can we use SOSL statements in triggers?



    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]);


    Salesforce | Parul Parul #2

    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;
    Example of SOSL



    Salesforce | shariq shariq #1


    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 !



    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.



Viewing 5 posts - 1 through 5 (of 5 total)

Please to reply to this topic.


We're not around right now. But you can send us an email and we'll get back to you, asap.


About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2019 Forcetalks. All Right Reserved.

Log in with your credentials


Forgot your details?