Permalink
Browse files

Autofill Mobile Device User Id

  • Loading branch information...
1 parent eab179f commit 48fdc5e4027d8f6dc460fdc1690087b4e045a146 @mbotos committed Jul 7, 2011
View
8 Salesforce/src/classes/MobileDevice_Test.cls
@@ -33,5 +33,13 @@ public with sharing class MobileDevice_Test {
devices[0].Synced__c = true;
update devices[0];
}
+
+ static testMethod void deviceWithoutUserHasCurrentUser() {
+ System.runAs(users[0]) {
+ Mobile_Device__c device = new Mobile_Device__c(Name = '2');
+ insert device;
+ System.assertEquals(users[0].Id, [select User__c from Mobile_Device__c where Id = :device.Id].User__c);
+ }
+ }
}
View
11 .../src/triggers/MobileDevice_Unqiue.trigger → Salesforce/src/triggers/MobileDevice.trigger
@@ -1,8 +1,16 @@
-trigger MobileDevice_Unqiue on Mobile_Device__c (before insert, before update) {
+trigger MobileDevice on Mobile_Device__c (before insert, before update) {
set<Id> userIds = new set<Id>();
map<string, Mobile_Device__c> devicesByHash = new map<string, Mobile_Device__c>();
string error = 'A device may only be registered once per User';
+ if (Trigger.isInsert) {
+ for (Mobile_Device__c d : Trigger.new) {
+ if (d.User__c == null) {
+ d.User__c = UserInfo.getUserId();
+ }
+ }
+ }
+
for (Mobile_Device__c d : Trigger.new) {
string hash = d.User__c + d.Name;
if (devicesByHash.containsKey(hash)) {
@@ -17,7 +25,6 @@ trigger MobileDevice_Unqiue on Mobile_Device__c (before insert, before update) {
if (Trigger.isInsert) {
for (Mobile_Device__c d : [select Name, User__c from Mobile_Device__c where User__c in :userIds]) {
string hash = d.User__c + d.Name;
- //System.debug('Existing hash: ' + hash);
if (devicesByHash.containsKey(hash)) {
devicesByHash.get(hash).addError(error);
}
View
2 ...gers/MobileDevice_Unqiue.trigger-meta.xml → ...rc/triggers/MobileDevice.trigger-meta.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexTrigger xmlns="http://soap.sforce.com/2006/04/metadata">
- <apiVersion>18.0</apiVersion>
+ <apiVersion>20.0</apiVersion>
<status>Active</status>
</ApexTrigger>

0 comments on commit 48fdc5e

Please sign in to comment.