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
Marketing 20112Marketing 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.
AshlekhAshlekh
Hi,

You can create custom field and set default value but when you want to set value from API is not cleared to us.
AshwaniAshwani
If you are on visualforce then it is possible, from schema declaration it is not possible natively.
Rajendra ORajendra O
Not sure if you are talking about setting default value at record level, if that is what you mean, it's not possible in given manner.

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.
Marketing 20112Marketing 20112
Yeah, I'll describe the case abit more detailed. 

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. 

Rajendra ORajendra O
I believe you should go through follwoing documentation.
#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.
Marketing 20112Marketing 20112
I'm sorry if I didn't make it clear. I'm working directly on the salesforce webpage. Is that was it means by visualforce? I just started working with salesforce yesterday, haven't caught up with all the terms. 
Rajendra ORajendra O
Alright, you should have mentioned this in your original post, community would be in better position to address your query.

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.
AshwaniAshwani
Marketing 20112

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.
Marketing 20112Marketing 20112
Thanks for the replies! I will read up on the recommended stuff. If any more questions arise I will post them here.
Marketing 20112Marketing 20112
Hey again :) 

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? 
Rajendra ORajendra O
There could be lot of reasons for this but for the start I guess you may have not added API link to the remote site:-
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.
Marketing 20112Marketing 20112
Yeah, I've added it to remote sites. I tried it with just a custom button to see that I could get the correct json response, and it did work. 

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>
Rajendra ORajendra O
If it's working by button, it should have worked via APEX as well. can you post your working button code and non-working version of same code.
Marketing 20112Marketing 20112
Button code: 

{!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>
Rajendra ORajendra O
When you said it's not working via VF, i thought you are issuing http call via Apex.
I think it could be issue of invalid session id for connection. Can you try setting session id like:-
sforce.connection.sessionId = "{!$Api.Session_ID}";
It should be setup before calling remoteFunction. Let me know if it works
Marketing 20112Marketing 20112
Works like a charm! Thanks! I might consider actually making the call in the Apex code though. Thanks so much for all your help