You need to sign in to do that
Don't have an account?
Marketing 20112
Creating a custom field that gets values from another API, possible?
I want to create a custom field in the contacts page. I was wondering if its possible to set the field values as some data aquired by an API. Something like calling a javascript to get the values. I wasn't quite able to find anything like that when trying to create a new custom field.
You can create custom field and set default value but when you want to set value from API is not cleared to us.
As Avillion stated, if you are on VF page then it's possible. In that case related controller class can call external API soap/rest and fetch value, then assign it to the record in context.
If it's not case of VF page, you could write a aftre insert trigger, which can call @future method, which can then issue external API call and update the record in context.
Let us know your use case detail.
In the detailed page of a contact. I want to add a new Section. This sections values are stored in our database and can be accesed through our API, via a http request.
Can this be done? Sounds like the insert trigger might be the thing I'm looking for.
#1 This will walk you through future calls: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_future.htm
#2 This will help you out to write API calls:https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_http.htm
After walking through the links, if you feel you have queries, let us know.
Anyway, yes we call it VF/Visualforce pages. To support UI/VF page and fill/manipulate data you will need a backend code class, usually called controller class, which is written in java like simple language called Apex.
Not sure if you followed following documents but I highly recommend reading these:-
https://developer.salesforce.com/page/An_Introduction_to_Visualforce
https://developer.salesforce.com/page/An_Introduction_to_Apex
Once these done go through https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_http.htm
Not sure what is the result type of your API, you will need to parse result accordingly (Json or xml)
I could write the code but that will not help you in long term, if you left unanswered even after reading above documents, please post your query.
An inline visualforce page with controller can be helpful in scenario. In controller you can get contact record and on behalf of it you can query value from external API and display on inline visualforce page.
I got the page up and running. And its running the javascript I want. The problem now is that I get a 401(Unauthorized) response back from the HTTP post I want to make. I made it exactly the same as I did with a custom button. Is there a setting I'm missing?
Please check : https://help.salesforce.com/apex/HTViewHelpDoc?id=configuring_remoteproxy.htm&language=en (https://help.salesforce.com/apex/HTViewHelpDoc?id=configuring_remoteproxy.htm&language=en)
If it's not the issue, please add debug statement to and print your response.
This is the response I get from the POST sent from the visualforce page:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Error 401 Unauthorized</title>
</head>
<body><h2>HTTP ERROR 401</h2>
<p>Problem accessing /services/proxy. Reason:
<pre> Unauthorized</pre></p><hr /><i><small>Powered by Jetty://</small></i><br/>
<br/>
<br/>
</body>
</html>
{!RequireScript("/soap/ajax/11.1/connection.js")}
var n = '{!Contact.MobilePhone }'
postParams = {}
postParams.url = 'https://api.example.com/api/'
postParams.method = 'POST'
postParams.requestData = 'phonenumber='+n
postParams.requestHeaders = {}
postParams.requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded' //or another mime-type
postParams.requestHeaders['Content-Length'] = postParams.requestData.length //required for POST
postParams.onSuccess = processSuccess
postParams.onError = processError
sforce.connection.remoteFunction(postParams)
function processSuccess(message)
{ // do something with results
alert(message);
}
function processError(message)
{ // do something with the error
alert('error');
}
APEX code:
<apex:page standardController="Contact">
<apex:includeScript value="/soap/ajax/11.1/connection.js"/>
<script>
var n = '{!Contact.MobilePhone }'
postParams = {}
postParams.url = 'https://api.example.com/api/'
postParams.method = 'POST'
postParams.requestData = 'phonenumber='+n
postParams.requestHeaders = {}
postParams.requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded' //or another mime-type
postParams.requestHeaders['Content-Length'] = postParams.requestData.length //required for POST
postParams.onSuccess = processSuccess
postParams.onError = processError
sforce.connection.remoteFunction(postParams)
function processSuccess(message)
{ // do something with results
alert(message);
}
function processError(message)
{ // do something with the error
alert('error');
}
</script>
<h1>Congratulations</h1>
This is your new Page
</apex:page>
I think it could be issue of invalid session id for connection. Can you try setting session id like:-
It should be setup before calling remoteFunction. Let me know if it works