Tuesday, 13 January 2015

Add a Helpbutton in pageblock using javascript.

    Helptext is preety much important for new application user and sometimes we can have requirement to add helptext to pageblock title/header...



  • We have helptext attribute in pageBlockSection but not in pageBlock as of now.
  • So we can append a helpbutton using Jquery .See the script below.


<apex:page standardController="Case">
<head>
    <script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
    <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
</head>  
    <apex:form >
      <apex:pageBlock mode="edit"  tabStyle="Case" >                                    
          <apex:pageBlockSection title="Subject" columns="1" id="subject" collapsible="false" >
              <!-- Configure your help text from Field Help text-->                                                                        
              <apex:pageBlockSectionItem helpText="{!$ObjectType.Case.fields.subject.inlineHelpText}">
                  <apex:outputLabel value="{!$ObjectType.Case.fields.subject.label}"/>                                      
                  <apex:outputPanel >
                      <apex:inputField value="{!case.subject}" />              
                  </apex:outputPanel>                                        
              </apex:pageBlockSectionItem>
           </apex:pageBlockSection>
       </apex:pageBlock>
     
       <script>
                 var elementToAppend = '<span class="helpButton" id="example-title-_help"><img src="/s.gif" class="helpOrb" style="width: 20px!important;height:15px!important;"/></span>';
                 $("[id$='subject']").find('h3').after(elementToAppend);
                 sfdcPage.setHelp('example-title', 'Demo help Text');
        </script>
    </apex:form>
 
</apex:page>


Hope this helps..:-)

Sunday, 11 January 2015

Stuck with multiple attachments upload in your visualforce page..Ajax file uploader may help you :-)


    Uploading multiple attachment using AJAX will help you to process all file uploading asynchronously. Check  code in github :

  Here you see function that fetch all files/attachments and upload them asynchronously using AJAX

  function uploadFile(parentId){
       
         
            $("input[type=file]").each(function(){
             
              filesToUpload.push($(this)[0].files[0]);
            });
            //console.log(filesToUpload);
             for(var i = 0, f; f = filesToUpload[i]; i++)
            {
                var reader = new FileReader();

                // Keep a reference to the File in the FileReader so it can be accessed in callbacks
                reader.file = f;

                reader.onload = function(e)
                {
                    var att = new sforce.SObject("Attachment");
                    att.Name = this.file.name;
                    att.ContentType = this.file.type;
                    att.ParentId = parentId;

                    var binary = "";
                    var bytes = new Uint8Array(e.target.result);
                    var length = bytes.byteLength;

                    for (var i = 0; i < length; i++)
                    {
                        binary += String.fromCharCode(bytes[i]);
                    }

                    att.Body = (new sforce.Base64Binary(binary)).toString();

                    sforce.connection.create([att],
                    {
                        onSuccess : function(result, source)
                        {
                            if (result[0].getBoolean("success"))
                            {
                                console.log("new attachment created with id " + result[0].id);
                            }
                            else
                            {
                                console.log("failed to create attachment " + result[0]);
                            }
                        },
                        onFailure : function(error, source)
                        {
                            console.log("an error has occurred " + error);
                        }
                    });
                };

                reader.readAsArrayBuffer(f);
            }

         
        }    


Hope this will help you,,stay coding..:-)        

Add a Helpbutton in pageblock using javascript.

    Helptext is preety much important for new application user and sometimes we can have requirement to add helptext to pageblock title/hea...