Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Autofill Mobile Device User Id

  • Loading branch information...
commit 48fdc5e4027d8f6dc460fdc1690087b4e045a146 1 parent eab179f
Matthew Botos authored
8 Salesforce/src/classes/MobileDevice_Test.cls
@@ -33,5 +33,13 @@ public with sharing class MobileDevice_Test {
33 33 devices[0].Synced__c = true;
34 34 update devices[0];
35 35 }
  36 +
  37 + static testMethod void deviceWithoutUserHasCurrentUser() {
  38 + System.runAs(users[0]) {
  39 + Mobile_Device__c device = new Mobile_Device__c(Name = '2');
  40 + insert device;
  41 + System.assertEquals(users[0].Id, [select User__c from Mobile_Device__c where Id = :device.Id].User__c);
  42 + }
  43 + }
36 44
37 45 }
11 Salesforce/src/triggers/MobileDevice_Unqiue.trigger → Salesforce/src/triggers/MobileDevice.trigger
... ... @@ -1,8 +1,16 @@
1   -trigger MobileDevice_Unqiue on Mobile_Device__c (before insert, before update) {
  1 +trigger MobileDevice on Mobile_Device__c (before insert, before update) {
2 2 set<Id> userIds = new set<Id>();
3 3 map<string, Mobile_Device__c> devicesByHash = new map<string, Mobile_Device__c>();
4 4 string error = 'A device may only be registered once per User';
5 5
  6 + if (Trigger.isInsert) {
  7 + for (Mobile_Device__c d : Trigger.new) {
  8 + if (d.User__c == null) {
  9 + d.User__c = UserInfo.getUserId();
  10 + }
  11 + }
  12 + }
  13 +
6 14 for (Mobile_Device__c d : Trigger.new) {
7 15 string hash = d.User__c + d.Name;
8 16 if (devicesByHash.containsKey(hash)) {
@@ -17,7 +25,6 @@ trigger MobileDevice_Unqiue on Mobile_Device__c (before insert, before update) {
17 25 if (Trigger.isInsert) {
18 26 for (Mobile_Device__c d : [select Name, User__c from Mobile_Device__c where User__c in :userIds]) {
19 27 string hash = d.User__c + d.Name;
20   - //System.debug('Existing hash: ' + hash);
21 28 if (devicesByHash.containsKey(hash)) {
22 29 devicesByHash.get(hash).addError(error);
23 30 }
2  ...src/triggers/MobileDevice_Unqiue.trigger-meta.xml → ...sforce/src/triggers/MobileDevice.trigger-meta.xml
... ... @@ -1,5 +1,5 @@
1 1 <?xml version="1.0" encoding="UTF-8"?>
2 2 <ApexTrigger xmlns="http://soap.sforce.com/2006/04/metadata">
3   - <apiVersion>18.0</apiVersion>
  3 + <apiVersion>20.0</apiVersion>
4 4 <status>Active</status>
5 5 </ApexTrigger>

0 comments on commit 48fdc5e

Please sign in to comment.
Something went wrong with that request. Please try again.