Friday 12 August 2016

How to Combine Documents through Docusign APIs

This time I got a chance to play with inner world of Docusign. Always I prefer REST API but this time SOAP saved my life.

I had a challenge to combine all signed document in single enevelope ( condition is you can not get it through Docusign configs settings because that is common for) so I have used soap api this time over rest ( was giving me Time out error).

Required Data:  Docusign Credential , Integrator Key & EnevelopId.

Firstly created a class 'DocusignSOAPfromWSDL_Cls' trough wsdl file then written our logic to get all document in combined format. Below is my code snippet:


// To Set endpoit URL
dsApiSend.endpoint_x = 'https://demo.docusign.net/api/3.0/dsapi.asmx';

// To Set Authentication
String auth = '<DocuSignCredentials>
                                <Username>'+ DocusignUserName+'</Username>
                                <Password>' + DocusignPassword + '</Password>
                                <IntegratorKey>' + Docusign_IntegratorKey + '</IntegratorKey>                                                 </DocuSignCredentials>';

// To Set Headers        
dsApiSend.inputHttpHeaders_x = new Map<String, String>();
dsApiSend.inputHttpHeaders_x.put('X-DocuSign-Authentication',auth);      

//Interating all incoming docusign status records
 DocusignSOAPfromWSDL_Cls.EnvelopePDF combinedPDF = dsApiSend.RequestPDFNoWaterMark(docusignEnvelopeID);
       Attachment combinedDocument =  new attachment(
                                           Body=EncodingUtil.base64Decode(combinedPDF.PDFBytes),
                                           Name= AttachmentName+'.pdf',
                                           parentId= ParentRecordId
                                    );
      insert combinedDocument ;

This is how you may combine all signed document through Docusign.


Thursday 4 August 2016

Finding nearest locations on google map within Salesforce

Locating anything is just part of our daily habit. Salesforce made is very easy by adding Geolocation to Address field types. So I have did one practice to locate nearest restaurants on google map.

Lets Say I have one Company Record and would l like to plot all restaurants that are located in x unit of distance to company on google map.

To achieve this I am going to use two features of salesforce one is Distance formula

DISTANCE(AddressField, GEOLOCATION(latitude,longitude), 'mi')

and another one is apex:map & apex:mapmarker tag

Visualforce page:

<apex:page controller="findrestaurantsController">  
      <apex:pageBlock >
         <apex:pageBlockSection title="Nearest Restaurants" >      
            <apex:map width="600px" height="500px" center="{!mylocation}" zoomLevel="5">
                <apex:repeat value="{!NearestRestaurantsList}" var="nearestPos" >
                    <apex:mapMarker position="{!nearestPos.location}" title="{!nearestPos.RestaurantName}"/>
                </apex:repeat>
            </apex:map>          
         </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>

Controller:
public class FindrestaurantsController {

  // Public Properties
  Public map < String, decimal > mylocation {
    get;
    private set;
  }
  Public List < NearestRestaurants > NearestRestaurantsList {
    get;
    set;
  }

  Public FindrestaurantsController() {
    Company__c Company = new Company__c();
String CompanyId = ApexPages.currentPage().getParameters().get('id');
    Company = [Select Id, Name, Location__latitude__s, Location__longitude__s From Company__c Where Id =: CompanyId];

    mylocation = new Map < String, Double > {
      'latitude' => Company.Location__latitude__s,
      'longitude' => Company.Location__longitude__s
    };

    String queryString =
      'SELECT Id, Name, Radius__c, Location__c, Location__longitude__s,' +
      'Location__latitude__s ' +
      'FROM Restaurant__c ' +
      'WHERE Id != \'' + Company.id + '\' AND DISTANCE(Location__c, GEOLOCATION(' + Company.Location__latitude__s +
      ',' + Company.Location__longitude__s + '), \'mi\') < ' +
      Company.Distance__c + ' ' +
      'ORDER BY DISTANCE(Location__c, GEOLOCATION(' + Company.Location__latitude__s + ',' + Company.Location__longitude__s +
      '), \'mi\') ';

// Getting nearest Restaurant records
    List < Restaurant > otherRestaurants = new List < Restaurant > ();
    otherRestaurants = Database.query(queryString);

    NearestRestaurantsList = new List < NearestRestaurants > ();
// Building wrapper List
    for (Restaurant otherRestaurant: otherRestaurants) {
NearestRestaurants wrapItem = new NearestRestaurants(
                             new Map < String, Double > {
                                    'latitude' => otherRestaurant.Location__latitude__s,
                                       'longitude' => otherRestaurant.Location__longitude__s              },
                                       otherRestaurant.Name);

NearestRestaurantsList.add(wrapItem);
    }

  }

  // Wrapper Class to store nearest restaurants detail
  Public Class NearestRestaurants {
    Public map < String, decimal > location {
      get;
      private set;
    }
    Public String RestaurantName {
      get;
      private set;
    }
    public NearestRestaurants(Map < String, Double > nearestlocation, String myResName) {
      location = nearestlocation;
      RestaurantName = myResName;
    }
  }
}

Thanks for reading this post.