質問

I would like to have an apex trigger fire any time an attachment object is added/deleted/modified. When this trigger fires I would like to pass the attachment's parent ID through an apex class which will pass that ID to a PHP page on an external website (using GET is fine).

I do not have to display the PHP page, I just need the PHP page to run. The php page just stores the ID in a MySQL database. I also do not need to know what happened to the attachment (if it was added/deleted/modified).

Any help you can offer is greatly appreciated. I have tried several different methods but I simply run into many errors that I can't figure out every time. I don't think any of my attempts are worth sharing here.

Thank you very much!

役に立ちましたか?

解決

In order to have a callout working from within a trigger, you need to make one more step: An @future-Call. That means, from within your trigger, you must collect all the IDs that were inserted/updated/deleted. Then, you must create a Class that implements a static void with the @future-Annotation (thus, this function is always executed asynchronously). Last, you call this function from the trigger and run your callout in the function.

trigger Attachment_AfterAll_Callout on Attachment (after insert, after update, after delete) {
    Set<Id> ids = new Set<Id>();
    //collect your ids here
    //you might get away with just using ALL ids: ids = Trigger.newMap.keyset()
    //call @future Function
    WebserviceHandler.attachmentCall(ids);
}

In a class file:

public class WebserviceHandler {
     @future(callout=true)
     public static void attachmentCall(Set<Id> ids) {
         //make your WS Callout here. 
         //Note, this is not guaranteed to run inline with the trigger,
         //execution may be delayed!
     }
}

Please note that there are limits on how many @future Calls you may have running, so you should batch the execution.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top