function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
renu anamalla 3renu anamalla 3 

Can we write SOSL statements in triggers

Can we write SOSL statements in triggers
Abhishek_DEOAbhishek_DEO
Yes, you can write SOSL inside triggers.There is no such restriction.
Rupal KumarRupal Kumar
hi renu,

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
Rupal Kumar
Mirketa Software Pvt Ltd
http://mirketa.com/index.html
Abhishek_DEOAbhishek_DEO

Hi Rupal,

Sorry; but can you tell on which page of apex developer's guide, it is mentioned? As long as I know there is no such restriction.

Thanks,

Abhishek
 

Abhishek_DEOAbhishek_DEO
Have tested into dev org and based on results, I can say we can use SOSL in Triggers. 
 
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);
}

 
Rupal KumarRupal Kumar
hi,
Sorry ,Earlier this was not allowed but now sosl are allowed in triggers I read wrong content about this
https://developer.salesforce.com/forums/?id=906F00000008yZiIAI
It was a issue before that we can not have SOSL inside trigger ,However you can write SOSL inside trigger now .This is implemented in Spring 11 .So you can write SOSL inside trigger .



Thanks
Rupal Kumar
Mirketa Software Pvt Ltd
http://mirketa.com/index.html