You need to sign in to do that
Don't have an account?
Chitral Chadda
Unique value of picklist in contacts related to single account
I have a picklist Priority__c on contact whith values 1 ,2,3...upto 10
i want that for single account , there shud be no duplicate picklist value of priority in contacts (limit 10 )
if user tries to enter a duplicate value of priority , then display error.
I am stuck at this map concept.
since in MAP there shoould be single id of account and list of contacts storing priority
i m quering it from account list but something is wrong.
trigger UniquePriority on Contact (before insert,before update) {
set<id> accId = new set<id>();
for(contact c: trigger.new)
{
if (c.accountid!=null)
{
accId.add(c.accountId);
}
}
//Here i used account list to maintain unique id (single account id) with limit of 10 contacts
list<account> acc = [select id ,(select Priority__c from contacts where Priority__c!=null) from account where id In: accid ];
map<id,list<contact>> accMap = new map<id,list<contact>>();
//[Error] Error: Compile Error: Loop variable must be of type SOBJECT:Account at line 14 column 17
//But if i change this to for(account a : acc.contacts) i will have to chasnge map to map<id,list<account>> and everything goes wrong then.
for(contact a : acc)
{
if(!accMap.containskey(a.accountid))
{
accMap.put(a.accountid,new List<contact>());
}
else
{
accMap.get(a.accountid).add(a);
}
}
for(contact c: trigger.new)
{
if(accMap.containsKey(c.accountid) && c.Priority__C!=null)
{
list<contact> dupPriority =accMap.get(c.Accountid);
{
for( contact dp: dupPriority)
{
if( c.Priority__c == dp.Priority__c)
{
c.addError('Same priority exists');
}
}
}
}
}
}
i want that for single account , there shud be no duplicate picklist value of priority in contacts (limit 10 )
if user tries to enter a duplicate value of priority , then display error.
I am stuck at this map concept.
since in MAP there shoould be single id of account and list of contacts storing priority
i m quering it from account list but something is wrong.
trigger UniquePriority on Contact (before insert,before update) {
set<id> accId = new set<id>();
for(contact c: trigger.new)
{
if (c.accountid!=null)
{
accId.add(c.accountId);
}
}
//Here i used account list to maintain unique id (single account id) with limit of 10 contacts
list<account> acc = [select id ,(select Priority__c from contacts where Priority__c!=null) from account where id In: accid ];
map<id,list<contact>> accMap = new map<id,list<contact>>();
//[Error] Error: Compile Error: Loop variable must be of type SOBJECT:Account at line 14 column 17
//But if i change this to for(account a : acc.contacts) i will have to chasnge map to map<id,list<account>> and everything goes wrong then.
for(contact a : acc)
{
if(!accMap.containskey(a.accountid))
{
accMap.put(a.accountid,new List<contact>());
}
else
{
accMap.get(a.accountid).add(a);
}
}
for(contact c: trigger.new)
{
if(accMap.containsKey(c.accountid) && c.Priority__C!=null)
{
list<contact> dupPriority =accMap.get(c.Accountid);
{
for( contact dp: dupPriority)
{
if( c.Priority__c == dp.Priority__c)
{
c.addError('Same priority exists');
}
}
}
}
}
}
Please let me know if there is something that is unclear to you about this trigger. If this does not meet your needs, please let me know what about it does not meet your needs.
NOTE: When you add code, please use the "Add a code sample" button (the icon is <>) to increase readability
All Answers
This method will save you time instead of doing the comparison in the loop. If Priority__c is a Text field instead of an Integer, just change line 10 to be the following insted.
NOTE: This code has not been tested and may contain typographical or logical errors.
@pcon thnx buddy
1 thing
if my list is on account ,cant i make for loop like //for(contact c: acc .contacts) ??
************
list<account> acc = [select id ,(select Priority__c from contacts where Priority__c!=null) from account where id In: accid ];
map<id,list<contact>> accMap = new map<id,list<contact>>();
for(contact c: acc .contacts)
{
if(!accMap.containskey(a.accountid))
{
accMap.put(a.accountid,new List<contact>());
}
else
{
accMap.get(a.accountid).add(a);
}
}
i tried bt i got error [Error] Error: Compile Error: Initial term of field expression must be a concrete SObject: LIST<Account> at line 17 column 21
i made little modification to the code
trigger UniquePriority1 on Contact (before insert, before update) {
Set<Id> accountIds = new Set<Id>();
for (Contact contact: Trigger.new) {
if (contact.AccountId != null) {
accountIds.add(contact.accountId);
}
}
Map<Id, list<Integer>> prioMap = new Map<Id, List<Integer>>();
for (Id id: accountIds) {
prioMap.put(id, new List<Contact>()); // Error: Compile Error: Incompatible value type LIST<Contact> for MAP<Id,LIST<Integer>> at line 13 column 9
}
for (Contact contact: [
select Priority__c
from Contact
where Priority__c != null and
AccountId in :accountIds
]) {
prioMap.get(contact.AccountId).add(contact.Priority__c);
}
for (Contact contact: Trigger.new) {
if (
prioMap.containsKey(contact.AccountId) &&
contact.Priority__c != null &&
prioMap.get(contact.AccountId).contains(contact.Priority__c)
) {
contact.addError('Same priority exists');
}
}
}
1 more thing // can u help me in my code cz i realyy cnt figure out that little thing , my head banged !
in picklist Priority__c ( on contact) i have 10 picklist values only so generally i have to put limit 10 on contact so that it checks only for 10 contacts
if i put limit 10 here (the code you shared)
for (Contact contact: [ select Priority__c from Contact where Priority__c != null andAccountId in :accountIds limit 10 ])
// it will put limit on account also get 10 account
but for single account i want it to check for 10 contacts
so as in my code i provided i used a subquery thats why
trigger UniquePriority on Contact (before insert,before update) {
set<id> accId = new set<id>();
for(contact c: trigger.new)
{
if (c.accountid!=null)
{
accId.add(c.accountId);
}
}
list<account> acc = [select id ,(select Priority__c from contacts where Priority__c!=null) from account where id In: accid ];
map<id,list<contact>> accMap = new map<id,list<contact>>();
for(account a : acc)
{
if(!accMap.containskey(a.aid))
{
accMap.put(a.id,new List<contact>());
}
else
{
accMap.get(a.id).add(a); //Error: Compile Error: Incompatible element type SOBJECT:Account for collection of SOBJECT:Contact at line 25 column 24 : i get this error wen i save this code // cz i need to use this list <contact> in the ****** marked area
}
}
for(contact c: trigger.new)
{
if(accMap.containsKey(c.accountid) && c.Priority__C!=null)
{
list<contact> dupPriority =accMap.get(c.Accountid); // ****** here i need to get use that list
{
for( contact dp: dupPriority)
{
if( c.Priority__c == dp.Priority__c)
{
c.addError('Same priority exists');
}
}
}
}
}
}
You cannot add the limit restrictions to the query because if you insert / update a bulk number of contacts across multiple you will not get all of the results back.
There are several reason why your code above would fail (mainly because of your variable types do not match)
NOTE: When you add code, please use the "Add a code sample" button (the icon is <>) to increase readability
Please let me know if there is something that is unclear to you about this trigger. If this does not meet your needs, please let me know what about it does not meet your needs.
NOTE: When you add code, please use the "Add a code sample" button (the icon is <>) to increase readability
Thnku for u spontaneous and helpful reply .
and yup here is the formatted code previously.
for my basic understanding , i m not clear with this part of code ..wen i save this code it gives error
although my reruirement is solved but for understanding level if u can explain why id this issue .
In your code you were doing a contains on a List which does not have the method contains. Also, you were adding prorities to a List that you were not doing anything with. Finally, you were trying to do a contains on a list of Contacts and checking to see if it contains a Priority (which is a string).
Is there any reason why the code I have supplied to you serveral times does not meet your needs? It seems like you could have taken that code and already be done with this issue.
GOT IT !
THANKS MAN !!!!
* KUDOS *