Skip to main content
Philipp Mathis 님이 #Data Management에 질문했습니다

Is there a way to create a field that automatically shortens an URL link/address that gets copy pasted into that field upon saving?

 

We have the need to add distinct urls on opptys that will link to a personal file stored outside of SF (URL is always different for each oppty). At the moment we have a simple URL field but the URL is very long and therefore that field is eating upt too much space. The alternative of first applying an URL shortener is not possible as we do not want to push this add. task to our end users.

 

Our end users should just copy&paste the full URL into a field and then SF should shoren that URL.

 

Is this possible?

 

 

Thanks,

 

Philipp
답변 8개
  1. 2020년 2월 14일 오후 5:43
    Hello Philipp,

     

    You need 2 Apex Classes and 1 Process Builder.

     

    Apex Class #1. BitlyService.

     

     

    /**

    * Simple service to make http callout to

    * Bitly url shortener service.

    */

    public class BitlyService {

    // reusable access token for oauth,

    // required when making API requests

    private String accessToken;

    public BitlyService() {

    this.accessToken = getAccessToken();

    }

    /**

    * Given a long URL will return the shortened version.

    * http://dev.bitly.com/links.html#.v3_shorten

    */

    public String shorten( String url ) {

    HttpRequest req = new HttpRequest();

    req.setEndpoint(

    'callout:Bitly/v3/shorten' +

    '?access_token=' + this.accessToken +

    '&longUrl=' + EncodingUtil.urlEncode( url, 'UTF-8' ) +

    '&format=txt'

    );

    req.setMethod('GET');

    Http http = new Http();

    HttpResponse res = http.send(req);

    return res.getBody();

    }

    /**

    * Get the access token to make authenticated oauth calls.

    * The actual username/password credentials are stored in

    * Named Credentials so that the password is stored securely.

    *

    * This does require an extra http request when instantiating

    * the service which adds to latency. Alternatively, you could

    * store the generated access token in a custom setting and simply

    * reference it from your code, but then anyone who can view

    * custom settings can view the access token and use the API.

    * Trade-offs.

    */

    private String getAccessToken() {

    HttpRequest req = new HttpRequest();

    req.setEndpoint('callout:Bitly/oauth/access_token');

    req.setMethod('POST');

    req.setHeader('Content-Length', '0');

    Http http = new Http();

    HttpResponse res = http.send(req);

    return res.getBody();

    }

    }

     

    Apex Class #2. BitlyShortenURLInvocable.

     

     

    public class BitlyShortenURLInvocable {

    @InvocableMethod(

    label = 'shorten'

    description = 'Given case IDs then generates a bitly short url for them'

    )

    public static List<String> shorten( List<ID> oppsIds ) {

    // You can't invoke http callouts from Process Builder or Flow

    // because the database transaction has not committed yet, you will get error:

    // "System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out"

    // To get around this then we'll actually make the call in a @Future method asynchronously.

    shortenAsync( oppsIds );

    return new List<String>();

    }

    @Future( callout = true )

    private static void shortenAsync( List<ID> oppIds ) {

    // Fetch your data and a field where you want to store the generated short url

    List<Opportunity> opps = new List<Opportunity>([ SELECT Id, Long_URL__c, Short_URL__c FROM Opportunity WHERE id IN :oppIds ]);

    // Service to actually call out to bitly and get a shortened url

    BitlyService service = new BitlyService();

    // Bitly does not support bulk url shortening

    // so you must make each call individually.

    // Do be aware of Bitly's rate limiting if you try

    // to mass create a bunch of records in short succession

    // http://dev.bitly.com/rate_limiting.html

    for ( Opportunity opp : opps ) {

    // in this trivial example, we're just creating short urls to the record itself

    opp.Short_URL__c = service.shorten( opp.Long_URL__c );

    }

    // update the records with their short urls

    // use workflow or trigger to fire off the short url field being populated

    // to then send email alerts, etc. including the short url

    if ( opps.size() > 0 ) {

    update opps;

    }

    }

    }

     

    Process Builder on Opportunity.

     

    Hello Philipp, You need 2 Apex Classes and 1 Process Builder. Apex Class #1. BitlyService. /** * Simple service to make http callout to * Bitly url shortener service.

     

    pb_opp2

     

    pb_opp3

     

    This is just a quick and dirty solution, please take security and robustness into account :-)

     

    Enjoy your week-end!

     

    Best,

     

    Gauthier 
0/9000