Showing posts with label Calling Web Services from a Trigger. Show all posts
Showing posts with label Calling Web Services from a Trigger. Show all posts

Thursday, May 5, 2011

Calling Web Services from a Trigger in Salesforce

Please go through the below example for that
we have and object Rndcenter which have two filed tempratureinC,tempratureinF.
When we entered any new record in to this object, user entered firstvalue(tempratureinC) and whith the help of trigger and webservice we got the second value(tempratureinF) and undate the recodes over the object.

Now i am going to drill down the concepts in steps:-
Step 1: Create a object RndCenter
Step 2: Consume WebService
web service information

http://www.w3schools.com/webservices/tempconvert.asmx
http://www.w3schools.com/webservices/tempconvert.asmx?WSDL
and create a class over the sfdc

Step 3: Create Trigger

trigger rnd1 on RndCenter__c (after insert)
{
RndCenter__c rnc=trigger.new[0];
TempConvertWebService.callTempConvertWebService(rnc.id);
}
step 4 :Create a class which call webservice

public class TempConvertWebService
{
@future (callout=true)
public static void callTempConvertWebService(string inputval1)
{
try
{
RndCenter__c rnc=new RndCenter__c();
rnc=[select tempratureinC__C,tempratureinF__c from RndCenter__c where id=: inputval1];
tempuriOrg.TempConvertSoap objtem=new tempuriOrg.TempConvertSoap();
string outtem= objtem.FahrenheitToCelsius(rnc.tempratureinC__C);
rnc.tempratureinF__c=outtem;
update rnc;
}
catch(System.AsyncException e)
{ System.debug( '****Exception' +e ) ;}
}
}

Some time we faced some exception
System.AsyncException: Future method cannot be called from a future method.
Above exception comes due to recursive call of the method(Future),we can resolve this by using a static variable in the class .