How to get this code to allow updates?

This trigger checks to see if a record that is being inserted or updated is using a role that is already in use and gives the user a notification if it already is in use. The issue is that if one updates the record and tries to save it, it won't allow them because it'll see that the record already exists with that role (like a recursive lookup). How to modify the code that allows saving modifications to the record but not allow one to change the role to a role that already exists?

I hope that this makes sense.

trigger trgr_Check_Duplicate_Roles on Key_Staff_People__c (before insert,before update) {       for (Key_Staff_People__c rsp: Trigger.new)          {             List <Key_Staff_People__c> rcrd = [SELECT Contact__c, Role__c FROM Key_Staff_People__c                                                  WHERE Contact__c = :rsp.Contact__c AND Role__c = :rsp.Role__c];               if (rcrd.size() > 0)             {                 if (rsp.Role__c <> Null && rsp.Role__c <> 'Other')                 {                     trigger.new[0].addError('A contact with the role "'+rsp.Role__c+'" already Exist. Only the role "Other" may be assigned to multiple Key Staff People.');                                     }             }         } } 

Replay

Apex N-u-b. I guess what you are saying the problem is you are updating a record with role__c != null or 'Other' and it gives error.

For that,

try the below code.

Set<String> contacts = new Set<String>();
Set<String> roles = new Set<String>();
Map<String, List<Key_Staff_People__c>> roleToKSP = new Map<String, List<Key_Staff_People__c>>();

for(Key_Staff_People__c ksp: Trigger.new) {
    if(!String.isBlank(ksp.Contact__c != null)) {
        contacts.add(ksp.Contact__c);
    }
    if(!String.isBlank(ksp.Role__c)) {
        roles.add(ksp.Role__c);
    }
}
for(Key_Staff_People__c ksp : [SELECT Contact__c, Role__c FROM Key_Staff_People__c
    WHERE Contact__c IN (contacts) AND Role__c IN (roles)]) {
    if(roleToKSP.containsKey(ksp.Role__c)) {
        roleToKSP.get(ksp.Role__c).add(ksp);
    } else {
        roleToKSP.put(ksp.Role__c, new List<Key_Staff_People__c> {ksp});
    }
}

for(Key_Staff_People__c ksp: Trigger.new) {
    if(ksp.Role__c <> Null && ksp.Role__c <> 'Other' && roleToKSP.get(ksp.Role__c).size() > 1) {
        //Display error message.
    }
}

This checks if the record being updated is the second record with the same role. If it is, it will display message. I have not run the code so there may be a minor syntactical error.

Please tell me whether the issue got resolved or not.

Regards,
Jigar

Category: trigger Time: 2016-07-31 Views: 4
Tags: trigger

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.169 (s). 12 q(s)