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
debiken88debiken88 

Raising an exception on a callout by trigger, updating a hidden field on the object?

Hey Everyone,
I have a situation I'm trying to figure out a work around for.  So I have a webservice returning JSON data that is working very well, my apex code calls out to the webservice, returns the data and I update the appropriate fields.  In one instance, I need to go get the latest data when a field is changed on the account object.  So I created a trigger that invokes the apex class, calling out to the the webservice with an @future method (which in turn updates the account object).  If the user has entered an incorrect value, the JSON data returns nulls. We don't know they've entered an incorrect value until the callout hppens.  So I'm trying to figure out a way to notify the user that the data entered wasn't valid.    I was thinking I would update a hidden "invalid data" field on the account object and raise an error on an update trigger, but this seems like it could be problematic.

Anyone have any ideas?  I would be REALLY grateful!! :)
Ramu_SFDCRamu_SFDC
Implement a before trigger and use the adderror method to raise an error if the data is incorrect, the transaction will be failed and no after triggers are run which would make a call out.  Would this work?

If this works you might have to look at the following posts for more information

http://salesforce.stackexchange.com/questions/18238/check-that-criteria-are-met-in-a-before-insert-trigger

https://developer.salesforce.com/forums/ForumsMain?id=906F00000009D9XIAU

debiken88debiken88
Thanks for your quick reply,  I don't think this will help because i have to invoke the callout to verify if the data is correct (it fetches some account detail based on an id that is stored in an external database, the user enters this id, but may enter it incorrectly).  So the same method in the Apex class that makes the callout either returns JSON data that is valid, or a JSON string that has null values...but either way I have to make the callout to the web service to get the data.  I'm trying not to make it too much of a hack.  
Ramu_SFDCRamu_SFDC
You may probably consider using workflow rules specifically time based workflow rules to fire an email alert after 1 hour with the message that the data entered was incorrect. The criteria being the account field is null. 

I do not think there is any other way around to do this other than the trigger and workflow rule combinations. Also the before and after triggers are executed if the workflow rule is executed in between, you might also consider this situation. But either ways it will not be immediate if that is not the concern.
Scott McClungScott McClung
Hi debiken88,
Just a couple random out of the box ideas...  If your user data entry workflow is simple enough you could replace it with a flow and have the callout done synchronously with an apex plugin before the record is updated.  
Another possibility could be a visualforce page and a controller extension that does the callout and giving the user feedback before the record is saved.