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
KaityKaity 

webservice' keyword in trigger


Hi,
My understanding was, we can not use 'webservice' keyword in trigger. However, I saw in apex guide, we can use 'webservice' key word.

You can only use the webService keyword in a trigger when it is in a method defined as asynchronous; that is, when the
method is defined with the @future keyword.


Can someone clear me this idea? How to write a code here, any example..

Thank you.
 
bob_buzzardbob_buzzard
That documentation looks to be out of date - I can't find it in the developer guide now.

From the docs:

---
You cannot use the webService keyword in a trigger because you cannot define a method in a trigger. 
---

It looks like the original documentation was referring to callouts rather than webservice methods, as you can only execute callout methods marked as @future so that the transaction isn't delayed waiting for a response.
nickwick76nickwick76
Hi Kaity,
Callouts cannot be made from triggers as that would hold up the database transaction until the callout completed, which can be up to 120 seconds from a limits perspective. The way to solve that is to make the callout in a future method which is separate from the transaction and hence ansynchronous.

A webservice method can be called from a trigger as you see in this post (http://salesforce.stackexchange.com/questions/45520/calling-webservices-in-apex-trigger) on Stackexchange but this is not a callout and maybe the docs need to be updated as the replier says.

// Niklas
KaityKaity
Thanks Bob for replying. At first, I would thank for sharing such good knowledge. I really thank you.
In this context, I am currently reading - Force.com Apex Code Developer's Guide; Version 32.0, Winter ’15.  Please see page 198.

Thanks, 
Kaity
bob_buzzardbob_buzzard
That documentation looks plain wrong to me.  Webservice is used to make a method externally accessible so there is no reason why that would affect a trigger.  I reckon it should be referring to callouts myself.
PatlatusPatlatus

Agree. The documentation is totally wrong. 

I have just tried to create webservice method inside a trigger.

When we try to create a method within trigger, we obtain error:

Error: Compile Error: Webservice methods must be contained in a global class at line 2 column 32
If we try to add "global" token before "trigger" we will get

Error: Compile Error: unexpected token: global at line 1 column 0

However, to my great surprise, you can actually add methods to trigger!

 

Anyway, even if you to write code like this

trigger co on CO__c (after undelete) {
        @future
        static void met() {
            public webservice  String x {get; set;}
        }
        
        met();
}

You will get an error saying:

Error: Compile Error: Variables cannot be marked as web services at line 4 column 39

So, actually you can define methods inside a class, despite how weird it sounds, but you still cannot use webservice keyword inside a trigger no matter what.