Tuesday, June 21, 2011

Convert 15 digit SFDC Id in 18 Digit



Convert 15 digit SFDC Id in 18 Digit


Visualforce Page Code :
<apex:page sidebar="false" controller="FileUploader" showHeader="false" contentType="{!ctype}">
   <apex:form >
      <apex:sectionHeader title="Upload data from CSV file (15 Digit Id in csv)"/>
      <apex:pagemessages />
      <apex:pageBlock >
             <center>
              <apex:inputFile value="{!contentFile}" filename="{!nameFile}" />
                  <apex:commandButton action="{!ReadFile}" value="Upload File" id="theButton" style="width:70px;"/>
             </center> 
        

      <apex:pageblocktable value="{!convertedId1}" var="acc" rendered="{!NOT(ISNULL(uploadedAccounts))}">
          <apex:column headerValue="15 Digit Id">
              <apex:outputText value="{!acc.Digit15Id}"/>
          </apex:column>

          <apex:column headerValue="18 Digit Id">
              <apex:outputText value="{!acc.Digit18Id}"/>
          </apex:column>

        <!--  <apex:column headerValue="Shipping Street">
              <apex:outputField value="{!acc.ShippingStreet}"/>
          </apex:column>

          <apex:column headerValue="Shipping City">
              <apex:outputField value="{!acc.ShippingCity}"/>
          </apex:column>

          <apex:column headerValue="Shipping State">
              <apex:outputField value="{!acc.ShippingState}"/>
          </apex:column>

          <apex:column headerValue="Shipping Postal Code">
              <apex:outputField value="{!acc.ShippingPostalCode}"/>
          </apex:column>

          <apex:column headerValue="Shipping Country">
              <apex:outputField value="{!acc.ShippingCountry}"/>
          </apex:column>-->

      </apex:pageblocktable>
    
      </apex:pageBlock>     
   </apex:form> 
</apex:page>





Controller Code :


public class FileUploader
{  
    public string ctype {get;set;}
    public List<wrapper15to18Id> convertedId1{get;set;}
    public class wrapper15to18Id
    {
        public String Digit15Id{get;set;} 
        public String Digit18Id{get;set;} 
    }   

    public string nameFile{get;set;}
    public Blob contentFile{get;set;}
    String[] filelines = new String[]{};
    public FileUploader2 ()
    {
        convertedId1 = new  List<wrapper15to18Id> ();      
    }
    public Pagereference ReadFile()
    {
        nameFile=contentFile.toString();
        filelines = nameFile.split('\n');
        for (Integer i=1;i<filelines.size();i++)
        {
            wrapper15to18Id ForconvertedId = new  wrapper15to18Id ();
            String[] inputvalues = new String[]{};
            inputvalues = filelines[i].split(',');
            ForconvertedId.Digit15Id = inputvalues[0];
            convertedId1.add(ForconvertedId);
        }
        try
        {
            for(wrapper15to18Id idcon : convertedId1)
            {
                System.debug('***************'+idcon.Digit15Id);
                idcon.Digit18Id = getCinvertedId(idcon.Digit15Id.trim());
            }
            ctype = 'application/vnd.ms-excel';
        }

        catch (Exception e)
        {
            ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. Please check the template or try again later');
            ApexPages.addMessage(errormsg);
        }   
        return null;
    }

  //  public String sids {get;set;}//= '001O0000004AKBd' ;
    public String ConvertedId {get;set;}
    string suffix = '';
    Integer flags;
    public List<wrapper15to18Id> getuploadedAccounts()
    {
        return convertedId1; 
    }     

    public string getCinvertedId(String sids)
    {
        string suffix = '';
        for (Integer i = 0; i < 3; i++)
        {
            flags = 0;          
            for (integer j = 0; j < 5; j++)
            {
               string c = sids.substring(i * 5 + j,i * 5 + j + 1);
               //Only add to flags if c is an uppercase letter:
               if (c.toUpperCase().equals(c) && c >= 'A' && c <= 'Z')
               {
                    flags = flags + (1 << j);
               }
           }
          if (flags <= 25)
          {
               suffix = suffix + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.substring(flags,flags+1);
          }
          else
          {
               suffix = suffix + '012345'.substring(flags-25,flags-24);
          }
        }
      
        //18 Digit Id with checksum
        //System.debug(' ::::::: ' + sids + suffix) ;
        ConvertedId = sids + suffix;
        return ConvertedId ;
    }        

}


No comments:

Post a Comment