Skip to content

Commit

Permalink
Autofill Mobile Device User Id
Browse files Browse the repository at this point in the history
  • Loading branch information
mbotos committed Jul 7, 2011
1 parent eab179f commit 48fdc5e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
8 changes: 8 additions & 0 deletions Salesforce/src/classes/MobileDevice_Test.cls
Expand Up @@ -33,5 +33,13 @@ public with sharing class MobileDevice_Test {
devices[0].Synced__c = true; devices[0].Synced__c = true;
update devices[0]; 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);
}
}


} }
@@ -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>(); set<Id> userIds = new set<Id>();
map<string, Mobile_Device__c> devicesByHash = new map<string, Mobile_Device__c>(); map<string, Mobile_Device__c> devicesByHash = new map<string, Mobile_Device__c>();
string error = 'A device may only be registered once per User'; 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) { for (Mobile_Device__c d : Trigger.new) {
string hash = d.User__c + d.Name; string hash = d.User__c + d.Name;
if (devicesByHash.containsKey(hash)) { if (devicesByHash.containsKey(hash)) {
Expand All @@ -17,7 +25,6 @@ trigger MobileDevice_Unqiue on Mobile_Device__c (before insert, before update) {
if (Trigger.isInsert) { if (Trigger.isInsert) {
for (Mobile_Device__c d : [select Name, User__c from Mobile_Device__c where User__c in :userIds]) { 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; string hash = d.User__c + d.Name;
//System.debug('Existing hash: ' + hash);
if (devicesByHash.containsKey(hash)) { if (devicesByHash.containsKey(hash)) {
devicesByHash.get(hash).addError(error); devicesByHash.get(hash).addError(error);
} }
Expand Down
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<ApexTrigger xmlns="http://soap.sforce.com/2006/04/metadata"> <ApexTrigger xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>18.0</apiVersion> <apiVersion>20.0</apiVersion>
<status>Active</status> <status>Active</status>
</ApexTrigger> </ApexTrigger>

0 comments on commit 48fdc5e

Please sign in to comment.