Convert Datetime to (MM/dd/yyyy) Date Format in Apex
Datetime dt = (Datetime)Account.Start_Date__c; Date d = dt.format('MM/dd/yyyy');
Datetime dt = (Datetime)Account.Start_Date__c; Date d = dt.format('MM/dd/yyyy');
If you want any fields value then you need add the field API name in standard controller.
public with sharing class AccountControllerExt { public Account acc {get;set;} public AccountControllerExt(ApexPages.StandardController stdController) { stdController.addFields(new List<String>{'Name', 'Region__c', 'TAX_ID_Number__c'}); this.acc = (Account)stdController.getRecord(); } }
File Upload Controller:
public with sharing class FileUploadController { public Document document { get { if (document == null) document = new Document(); return document; } set; } public PageReference upload() { document.AuthorId = UserInfo.getUserId();//Current User Id document.FolderId = UserInfo.getUserId();//Add Folder Id try { insert document; } catch (DMLException e) { ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading file')); return null; } finally { document.body = null;//Clear the View State document = new Document(); } ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Document uploaded successfully')); return null; } }
File Upload Visualforce Page:
<apex:page controller="FileUploadController"> <apex:sectionHeader title="File Upload" subtitle="File Upload Example"/> <apex:form enctype="multipart/form-data"> <apex:pageMessages /> <apex:pageBlock title="Upload a File"> <apex:pageBlockButtons > <apex:commandButton action="{!upload}" value="Save"/> </apex:pageBlockButtons> <apex:pageBlockSection showHeader="false" columns="2"> <apex:pageBlockSectionItem > <apex:outputLabel value="File Name" for="fileName"/> <apex:inputText value="{!document.name}" id="fileName"/> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputLabel value="File" for="file"/> <apex:inputFile value="{!document.body}" filename="{!document.name}" id="file"/> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputLabel value="Description" for="description"/> <apex:inputTextarea value="{!document.description}" id="description"/> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputLabel value="Keywords" for="keywords"/> <apex:inputText value="{!document.keywords}" id="keywords"/> </apex:pageBlockSectionItem> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>
System Mode:
runAs()
method doesn’t enforce user permissions or field-level permissions, it only enforces record sharing.User Mode:
Mode of execution:
Basically Salesforce offers two types of relationship:
Sometimes we may need One To One relationship, But unfortunately Salesforce doesn’t allow any direct methodology to build One To one relationship.
Let’s consider the scenario that we want to establish a One to One relationship between two custom objects Employee__c and PAN_Card__c.
So, here are few ways to implement One To One relationship between two objects in Salesforce. We can achieve this by using configuration only and can also achieve this by using code to make it more scalable.
Option 1:
We have established a one to one relationship between PAN_Card__c and Employee__c. When we try to add a second PAN_Card__c to the Employee__c, the “unique” constraint would be violated and an error would be thrown. This approach will work on both standard and custom object.
Option 2:
In this way also, We have established a one to one relationship between PAN_Card__c and Employee__c. So it will throw an error if Employee__c has more than one PAN Card.
Option 3:
This is already having a one-to-onePassport relation.
Option 4:
trigger PANCardValidation on PAN_Card__c (before insert, before update) { Set<id> employeeIds = new Set<id>(); Map<id, Employee__c> mapEmployee = new Map<id, Employee__c>(); for (PAN_Card__c p : trigger.New) { employeeIds.add(p.Employee__c); } List<Employee__c> lstEmployee = [SELECT Id, Name FROM Employee__c WHERE Id IN : employeeIds]; if (!lstEmployee.isEmpty()) { for (Employee__c emp : lstEmployee) { mapEmployee.put(emp.Id, emp); } for (PAN_Card__c p : trigger.New) { if (mapEmployee.containsKey(p.Employee__c)) { p.addError('A PAN Card already exist for the employee - ' + mapEmployee.get(p.Employee__c).Name); } } } }