Integration of Salesforce.com and AutoCAD WS – Part 3

<?xml encoding=”UTF-8″>By Daniel Du 

In part 1 and part 2 we introduced how to setup the browses based development mode tool and desktop based Force.com IDE. In this post, let’s do some actual work.

We have some DWG attachments in a case, what I am trying to do is, to list these DWG attachments and add a button behind of each, when the button is clicked, the DWG attachment should be opened in AutoCAD WS. Let’s list the attachment first in this post.

There is a standard attachment list in case page layout, but I’d rather create my own attachment list so that I can add buttons behind, so I will create a visual force page. and we can put this visual force page to case page layout, we will discuss this in latter part. Let’s say we have 2 DWG attachments for one case. The visual force page is supposed to look like below:

image

Now we are going to implement this page. Firstly let’s create a visual force page, named as OpenInAutocadWS.

Developers can use Visualforce to create a Visualforce page definition. A page definition consists of two primary elements: Visualforce markup and  Visualforce controller. Visualforce markup consists of Visualforce tags, HTML, JavaScript, or any other Web-enabled code embedded within a single <apex:page> tag. The markup defines the user interface components that should be included on the page, and the way they should appear.A Visualforce controller is a set of instructions that specify what happens when a user interacts with the components specified in associated Visualforce markup, such as when a user clicks a button or link. Controllers also provide access to the data that should be displayed in a page, and can modify component behavior. A developer can either use a standard controller provided by the Force.com platform, or add custom controller logic with a class written in Apex.

In this page, we are trying to list attachment of case, so we can take advantage the “case” standard controller, we also need to create our custom controller extension to implement our custom logic.

<span class="kwrd"><</span><span class="html">apex:page</span> <span class="attr">standardController</span><span class="kwrd">="Case"</span>
<span class="attr">extensions</span><span class="kwrd">="OpenInAutocadWS_Controller"</span> <span class="kwrd">></span>
<span class="kwrd"><</span><span class="html">apex:form</span> <span class="attr">id</span><span class="kwrd">="frm"</span><span class="kwrd">></span>
<span class="kwrd"><</span><span class="html">apex:pageBlock</span> <span class="attr">id</span><span class="kwrd">="pb"</span><span class="kwrd">></span>
<span class="kwrd"><</span><span class="html">apex:variable</span> <span class="attr">value</span><span class="kwrd">="{!0}"</span> <span class="attr">var</span><span class="kwrd">="count"</span> <span class="kwrd">/></span>
<span class="kwrd"><</span><span class="html">apex:pageBlockTable</span>
<span class="attr">value</span><span class="kwrd">="{!listAttachment}"</span>
<span class="attr">var</span><span class="kwrd">="item"</span> <span class="attr">id</span><span class="kwrd">="pbt"</span><span class="kwrd">></span>
<span class="kwrd"><</span><span class="html">apex:column</span> <span class="attr">headerValue</span><span class="kwrd">="Name"</span><span class="kwrd">></span>
{!item.Name}
<span class="kwrd"></</span><span class="html">apex:column</span><span class="kwrd">></span>
<span class="kwrd"><</span><span class="html">apex:column</span> <span class="attr">headerValue</span><span class="kwrd">="Download"</span><span class="kwrd">></span>
<span class="rem"><!-- {!item.Id} –></span>
<span class="kwrd"><</span><span class="html">a</span> <span class="attr">href</span><span class="kwrd">="https://c.na9.content.force.com
/servlet/servlet.FileDownload?file
={!item.Id}"</span><span class="kwrd">></span>download<span class="kwrd"></</span><span class="html">a</span><span class="kwrd">></span>
<span class="kwrd"></</span><span class="html">apex:column</span><span class="kwrd">></span>
<span class="kwrd"><</span><span class="html">apex:column</span> <span class="attr">id</span><span class="kwrd">="colOpenInAcadWS"</span>
<span class="attr">rendered</span><span class="kwrd">="{!CONTAINS(item.ContentType, <br>                       'application/x-dwg')}"</span><span class="kwrd">></span>
<span class="kwrd"><</span><span class="html">p</span> <span class="attr">id</span><span class="kwrd">="{!count}.openInAcadWS"</span><span class="kwrd">></</span><span class="html">p</span><span class="kwrd">></span>
<span class="kwrd"><</span><span class="html">apex:commandLink</span> <br>             <span class="attr">title</span><span class="kwrd">="Open In AutoCAD WS"</span>
<span class="attr">id</span><span class="kwrd">="btnOpenInAutoCADWs"</span>
<span class="attr">value</span><span class="kwrd">="Open In AutoCAD WS"</span>
<span class="attr">styleClass</span><span class="kwrd">="btn"</span>  <br>             <span class="attr">style</span><span class="kwrd">="text-decoration:none"</span>
<span class="kwrd"></</span><span class="html">apex:commandLink</span><span class="kwrd">></span>
<span class="kwrd"><</span><span class="html">apex:pageBlock</span> <span class="attr">id</span><span class="kwrd">="hiddenBlock"</span> <br>              <span class="attr">rendered</span><span class="kwrd">="false"</span><span class="kwrd">></span>
<span class="kwrd"></</span><span class="html">apex:pageBlock</span><span class="kwrd">></span>
<span class="kwrd"><</span><span class="html">apex:variable</span> <span class="attr">value</span><span class="kwrd">="{!count+1}"</span> <br>                       <span class="attr">var</span><span class="kwrd">="count"</span> <span class="kwrd">/></span>
<span class="kwrd"></</span><span class="html">apex:column</span><span class="kwrd">></span>
<span class="kwrd"></</span><span class="html">apex:pageBlockTable</span><span class="kwrd">></span>
<span class="kwrd"></</span><span class="html">apex:pageBlock</span><span class="kwrd">></span>
<span class="kwrd"></</span><span class="html">apex:form</span><span class="kwrd">></span>
<span class="kwrd"></</span><span class="html">apex:page</span><span class="kwrd">></span>

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Here is the source code of OpenInAutocadWS_Controller, which is an Apex class.

public with sharing class 
                
OpenInAutocadWS_Controller {



    public List<Attachment> listAttachment

                                 
{get;set;}  
       

        //constructor

    public OpenInAutocadWS_Controller  
    
(ApexPages.StandardController controller) { 


     //get the controller instance

     currentCase = (Case)controller.getRecord(); 
       

     //initialize the attachments list

     listAttachment = new List<Attachment>(); 
       

     //get all the case related DWG attachments

     listAttachment =

        
[Select Id, ContentType, Name, Body  
         from Attachment  
         where ParentId = :currentCase.Id  
               and Name like
'%DWG'];

       

    }

   

}
 

With that code, we created a visualforce page, and bind the attachment information to this page.  In the controller, we use SOQL(Saleforce Object Query Language) to retrieve attachment from salesforce.com database and save it to a list. In Visualforce page, the list is banded to a page bloc table.

OK, are you following me? Did you create the visual force page and list out the attachment successfully? Now user can download the attachment file from following url https://c.na9.content.force.com /servlet/servlet.FileDownload?file ={!item.Id}. But for my case, is it not what I want, I would like to open it AutoCAD WS directly without downloading anything. We will discuss this in coming post.

Stay tuned and have fun!


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading