You need to sign in to do that
Don't have an account?
gender api - https://gender-api.com/
Hi,
I am an Admin but not a dev.
I came accross this API https://gender-api.com/ - I wanted to create a button in Salesforce that would automatically call this gender api and return the Gender specified; The app says use the following to call the API: https://gender-api.com/get?name=elizabeth&key=KcPhEJZQNtmsoClMjd
I tried it in the browser and got this:
{"name":"elizabeth","name_sanitized":"Elizabeth","country":"","gender":"female","samples":44677,"accuracy":99,"duration":"32ms","credits_used":1}
What I wanted to say is the word female and save it to my custom field.
Can anyone help me out - where to start and what to do?
Thank you!
1 . Craete a remote site setting
https://gender-api.com
2. Apex Class
3. Apex Trigger
Im' trying to use your script but I keep getting a LeadTrigger: execution of BeforeInsert caused by: System.QueryException: List has no rows for assignment to SObject () error message as soon as I try to save a new record.
Note that I've only changed the SObject (Lead, in my case) and class name. Here are my class and trigger:
public class GenderAPI {
public static void getData(String recId){
Lead con =[Select Id, Name, Gender__c from Lead where id=:recId] ;
String callouts ='https://gender-api.com/get?name='+con.Name+'&key=mykeyhere';
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint(callouts);
request.setMethod('GET');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
JsonParser parserCls = (JsonParser) System.JSON.deserialize(response.getBody(), JsonParser.class);
con.Gender__c =parserCls.gender ;
Update con ;
}
}
public class JsonParser{
public String name{get;set;} //elizabeth
public String name_sanitized{get;set;} //Elizabeth
public String country{get;set;} //
public String gender{get;set;} //female
public Integer samples{get;set;}//44677
public Integer accuracy{get;set;} //99
public String duration{get;set;} //19ms
public Integer credits_used{get;set;} //1
}
}
and the trigger:
trigger LeadTrigger on Lead (before insert , before update ) {
if(trigger.isBefore || trigger.isUpdate){
GenderAPI.getData(trigger.new[0].Id);
}
}
Are you able to spot the problem? It'd be a great help, thanks.
Fabrizio
Try this solution ,
global class RestCall {
@future (callout=true)
public static void getData(String recId){
Lead gen =[Select Id ,Gender__c,Name from Lead where id=:recId] ;
String callouts ='https://gender-api.com/get?name='+gen.Name+'&key=mZCzPpqxHeADFUcJkF';
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint(callouts);
request.setMethod('GET');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
JsonParser parserCls = (JsonParser) System.JSON.deserialize(response.getBody(), JsonParser.class);
gen.Gender__c = parserCls.gender ;
Update gen;
}
system.debug('gender' + gen.Gender__c );
}
public class JsonParser{
public String name{get;set;} //elizabeth
public String gender{get;set;} //female
public Integer samples{get;set;}//44677
public Integer accuracy{get;set;} //99
public String duration{get;set;} //19ms
}
}
Trigger :
trigger LeadTrigger on Lead (before insert , before update ) {
if(system.isFuture()) return;
if(trigger.isBefore || trigger.isUpdate){
RestCall.getData(trigger.new[0].Id);
}
}