Tuesday, December 17, 2019

Salesforce - Find the list of required fields for any Object


Sometimes we find ourselves in a situation where we need to identify all the required fields for an object.

This is useful  for feedback to business or when preparing for a migration.

Yes, I know we could have gone the route of using the developer console, Apex code and so much more but for this solution we are going with simplicity :-)



  1. Select your avatar/profile
  2. Then Select Switch to Salesforce Classic (Under Options)
  3. Navigate to Setup
  4. Look for Field Accessibility under Security Controls
  5. Pick an Object > View by Profile > Pick a Profile
  6. Required Fields display in Red




May the force be with you and happy trails.

Jerome Slinger


Monday, December 2, 2019

Salesforce Lightning - Using the "lightning:fileUpload"

Sometimes we find ourselves in a situation where the standard file upload process just doesn't meet the required grade.

This is when we have to dust off the old developer console and write some code :-)

Step 1: Login to your Salesforce and open developer console (I always recommend your Sandbox first). 

tep 2: Navigate to File | New | Apex Class and Create an Apex controller called UploadManager. Replace following code in apex controller.  

UploadManager.apxc


 public class UploadManager{
    
@AuraEnabled  
   public static List getFiles(string recordId){  
     List DocumentList = new List();  
     Set documentIds = new Set();  //store file ids
     List cdl=[select id,LinkedEntityId,ContentDocumentId from ContentDocumentLink where LinkedEntityId=:recordId];  
     for(ContentDocumentLink cdLink:cdl){  
       documentIds.add(cdLink.ContentDocumentId);  // Document ids
     }      
     DocumentList = [select Id,Title,FileType,ContentSize,Description from ContentDocument where id IN: documentIds];  
     return DocumentList;  
   }  
   @AuraEnabled  
   public static List UpdateFiles(string documentId,string title,string recordId){  
     system.debug('title: ' +title);  
     ContentDocument cd = [select id,title from ContentDocument where Id=:documentId]; // Getting files from Parent record 
     cd.Title = title;  // Changing file Title with user entered title
     try{  
       update cd;  // Update ContentDocument (File)
     }  
     catch(DMLException e){  
       system.debug('Exception has occurred! ' +e.getMessage());  
     }  
      List DocumentList = new List();  
     Set documentIds = new Set();  
     List cdl=[select id,LinkedEntityId,ContentDocumentId from ContentDocumentLink where LinkedEntityId=:recordId];  
     for(ContentDocumentLink cdLink:cdl){  
       documentIds.add(cdLink.ContentDocumentId);  
     }      
     DocumentList = [select Id,Title,FileType,ContentSize,Description from ContentDocument where id IN: documentIds];  
     return DocumentList;  // Return list of files on parent record
   }  
 }  
Step 3: Navigate to File | New | Lightning Component and create a Lightning Component called FileUpload. Replace the following markup in the Lightning Component.

LightningFileUpload.cmp





LightningFileUploadController.js
({  
   doInit:function(component,event,helper){  
     var action = component.get("c.getFiles");  
     action.setParams({  
       "recordId":component.get("v.recordId")  
     });      
     action.setCallback(this,function(response){  
       var state = response.getState();  
       if(state=='SUCCESS'){  
         var result = response.getReturnValue();  
         console.log('result: ' +result);  
         component.set("v.files",result);  
       }  
     });  
     $A.enqueueAction(action);  
   } ,  
   //Open File onclick event  
   OpenFile :function(component,event,helper){  
     var rec_id = event.currentTarget.id;  
     $A.get('e.lightning:openFiles').fire({ //Lightning Openfiles event  
       recordIds: [rec_id] //file id  
     });  
   },  
   UploadFinished : function(component, event, helper) {  
     var uploadedFiles = event.getParam("files");  
     var documentId = uploadedFiles[0].documentId;  
     var fileName = uploadedFiles[0].name;  
     helper.UpdateDocument(component,event,documentId);  
     var toastEvent = $A.get("e.force:showToast");  
     toastEvent.setParams({  
       "title": "Success!",  
       "message": "File "+fileName+" Uploaded successfully."  
     });  
     toastEvent.fire();  
     /* Open File after upload  
     $A.get('e.lightning:openFiles').fire({  
       recordIds: [documentId]  
     });*/  
   },  
 })  

LightningFileUploadHelper.js
({  
       UpdateDocument : function(component,event,Id) {  
     var action = component.get("c.UpdateFiles");  
     var fName = component.find("fileName").get("v.value");  
     //alert('File Name'+fName);  
     action.setParams({"documentId":Id,  
              "title": fName,  
              "recordId": component.get("v.recordId")  
              });  
     action.setCallback(this,function(response){  
       var state = response.getState();  
       if(state=='SUCCESS'){  
         var result = response.getReturnValue();  
         console.log('Result Returned: ' +result);  
         component.find("fileName").set("v.value", " ");  
         component.set("v.files",result);  
       }  
     });  
     $A.enqueueAction(action);  
   },  
 })  


Step 4: To Create a quick action Navigate to Setup | Object Manager | Account |Buttons, Links and Actions | New Action. Fill all required fields and hit the Save button.


Create an Action

Step 5: To add quick action in account page layout Navigate to Setup | Object Manager | Account | Page Layouts. Edit Account Layout and move to Mobile & Lightning Actions.



Step 6: Drag File Upload quick action in  Salesforce Mobile & Lightning Experience Actions section and save the Account page layout.



Step 7: Open an account record and click on the File upload quick action button.



Output:






And just like that you are all done :-)

Happy Trails!

Generate reports from Opportunities using a Visualforce Page in Salesforce

  Step 1: Create a Visualforce Page Go to the Setup menu in Salesforce. Search for "Visualforce Pages" in the Quick Find box and c...