1. Use @Future Method
synchronous webservice cann't be call by trigger..to make Asynchronous.
2. Use Rempte Site Setting..
3. when you use web service you need to use @future(callout=true)
==============================================================================================
public class Http_request_title {
@future(callout=true)
public static void show(set<id> leadIds){
list<lead> leads=[select id,name,id__c,title__c from lead where id in:leadIds];
for(lead new_lead:leads){
http p=new http();
httprequest req=new httprequest();
string url='https://jsonplaceholder.typicode.com/photos/'+new_lead.id__c;
req.setEndpoint(url);
req.setMethod('GET');
httpresponse res=p.send(req);
string body=res.getBody();
System.JSONParser jp=JSON.createParser(Body);
while(jp.nextToken()!=NUll){
if(jp.getText()=='Title'){
jp.nextToken();
new_lead.title__c=jp.getText();
}
}
new_lead.Description=body;
}update leads;
}
}
========================================================================
trigger Http_request_title_trigger on Lead (after insert) {
if(trigger.isinsert && trigger.isafter){
map<id,lead> leadMap=trigger.newmap;
set<id> ids=leadMap.keySet();
Http_request_title.show(ids);
}
}
========================================================================
No comments:
Post a Comment