28. Visualforce Page
01 <apex:page controller="RandomContactController">
02 <apex:outputText value="{!getRandomName}" />
03 </apex:page>
28
Apex Controller
01 public with sharing class RandomContactController {
02 public String getGetRandomName() {
03
04 Contact [] myList = [SELECT Name FROM Contact LIMIT 1000];
05 // Pick a list entry at random
06 Integer index = Math.mod(Math.abs(Crypto.getRandomInteger()),myList.size());
07 Contact selected = myList.get(index);
08 return selected.Name;
09 }
10 }
29. Visualforce Page
01 <apex:page controller="RandomContactController">
02 <apex:outputText value="{!getRandomName}" />
03 </apex:page>
29
Apex Controller 01 public with sharing class RandomContactController { 02 public String getGetRandomName() { 03 04 // Check if the user has read access on the Contact.Name field 05 if (!Schema.sObjectType.Contact.fields.Name.isAccessible()) { 06 return ''; 07 } 08 09 Contact [] myList = [SELECT Name FROM Contact LIMIT 1000]; 10 // Pick a list entry at random 11 Integer index = Math.mod(Math.abs(Crypto.getRandomInteger()),myList.size()); 12 Contact selected = myList.get(index); 13 return selected.Name; 14 } 15 }
32. 32
Apex Controller
01 public with sharing class LeadConverterExtension {
02 private Lead l;
03 public LeadConverterExtension(ApexPages.StandardController ctr) {
04 l = [SELECT FirstName,LastName,Phone,Company FROM Lead WHERE Id=:ctr.getRecord().05 }
06
07 public PageReference convertLead() {
08 Contact c = new Contact(FirstName = l.FirstName, LastName = l.LastName, Phone = l.Phone);
09 insert c;
10 return null;
11 }
12 }
33. 33
Apex Controller 01 public with sharing class LeadConverterExtension { 02 private Lead l; 03 public LeadConverterExtension(ApexPages.StandardController ctr) { 04 l = [SELECT FirstName,LastName,Phone FROM Lead WHERE Id=:ctr.getRecord().Id]; 05 } 06 07 public PageReference convertLead() { 08 String [] contactUpdateFields = new String [] {'FirstName', 'LastName', 'Phone'}; 09 10 Map<String,Schema.SObjectField> m = Schema.SObjectType.Contact.fields.getMap(); 11 for (String fieldToCheck : contactUpdateFields) { 12 if (!m.get(fieldToCheck).getDescribe().isCreateable()) { 13 ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 14 return null; 15 } 16 } 17 18 Contact c = new Contact(FirstName = l.FirstName, LastName = l.LastName, Phone = l.Phone); 19 insert c; 20 return null; 21 } 22 }
38. 38
Apex Controller 01 public with sharing class LeadDeleteExtension { 02 private Lead l; 03 public LeadDeleteExtension(ApexPages.StandardController ctr) { 04 l = [SELECT Id FROM Lead WHERE Id=:ctr.getRecord().Id]; 05 } 06 07 public PageReference deleteLead() { 09 delete l; 10 return null; 11 } 12 }
39. 39
Apex Controller 01 public with sharing class LeadDeleteExtension { 02 private Lead l; 03 public LeadDeleteExtension(ApexPages.StandardController ctr) { 04 l = [SELECT Id FROM Lead WHERE Id=:ctr.getRecord().Id]; 05 } 06 07 public PageReference deleteLead() { 08 if (!Lead.sObjectType.getDescribe().isDeletable()) { 09 ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 10 return null; 11 } 12 } 13 14 delete c; 15 return null; 16 } 17 }
isDeletable() は オブジェクトレベルで チェック!