0

I have a created one Field on contact which is having checkbox data type name as "In use". I have a requirement like

1.One Contact can only be active at one place at a time. So we want to restrict the "In Use" field from being active if it is currently active on another Contact for the same account.

1
  • You can create a trigger on contact for that kind of validation............. Commented Jul 27, 2015 at 9:46

3 Answers 3

1

Can you try below trigger

trigger validate on Contact(before insert){
    SET<String> ids = new SET<String>();
        for(contact c : trigger.new){
            ids.add(c.AccountId);
        } 
        List<AggregateResult> tempAggr = [SELECT AccountId acc FROM Contact WHERE inUse__c= true AND Accontid IN : ids GROUP BY AccountId];

        Set<String> inUseset = new Set<String>();
        for(AggregateResult rs : tempAggr){
            inUseSet.add(String.valueOf(rs.get('acc')));
        }

        for(contact c : trigger.new){
            if(inUseSet.contains(c.AccountId)){
                c.inUse__c = false;
            }
            else{
                c.inUse__c = true;
                inUseSet.add(c.AccountId);
            }
        }
    }
13
  • GROUP BY Account or AccounID?? Commented Jul 28, 2015 at 13:48
  • There is slight Problem in this code when i checked in use for another child of same parent it will show me error this is good but when i unchecked it and then save it is also showing me error.. Commented Jul 29, 2015 at 6:27
  • Can you post the error stack trace?
    – San
    Commented Jul 29, 2015 at 6:58
  • It is a logical error ..... Commented Jul 29, 2015 at 11:01
  • Can you paste the code which you have tried.
    – San
    Commented Aug 3, 2015 at 7:03
1

I hope this would be helpful...

   trigger validate on Contact(before insert){

        set<id> rids = new set<id>();
        set<id> Uids = new set<id>();

        for(contact c : trigger.new){
              rids.add(c.AccountId);
        }
        list<contact> conts = [select accountid from contact where accountid IN :rids AND In_Use__c = true];
        for(contact c : conts){
           uids.add(c.accountid);
        }

        for(contact c : trigger.new){
             if(uids.contains(c.AccountId)){
                   c.addError('Account Related contact is already in Use');    
             }   

        } 
}
7
  • unexpected token: '==' at line 4 column 91 giving this error Commented Jul 27, 2015 at 10:13
  • see my updated ans, need to change == to = in query. Commented Jul 27, 2015 at 10:18
  • If this is the solution that you wanted..please Mark this ans as a it would be helpful.... Thanks.. Commented Jul 27, 2015 at 10:42
  • @AmitChaudhary : Your code has SOQL query inside for loop which is not a good practice.
    – San
    Commented Jul 27, 2015 at 10:43
  • @AmitChaudhary: I have a problem that when i am making a new Contact of same account and in use is already active of another contact of same account then also it is showing me error Commented Jul 28, 2015 at 6:19
0


Here is an option to fill the requirement without using code:
1. Create a unique field in contact.
2. Create a workflow on creation/update of contact when the in use check box is checked that fills the unique field created in section 1 with the account id.
3. Create a workflow on creation/update of contact when the in use check box is un-checked that clears the unique field created in section 1.
This way, when someone will try to check the in use check box under the same account he will get notification that there's a duplication and referral to the in use contact.

2
  • @Dagnish: But unique field will give me only once checked for all Contacts.For e.g XYZ contact is child of ABC and i checked In use and if i want to check in use of another Parent then it will throw me Workflow error. Commented Jul 27, 2015 at 10:11
  • Let's say that the id of account ABC is "ABC" and that the unique field is called "unique in use". On the contact under account ABC with the check box in use checked, the field "unique in use" will have "ABC" in it. If you'll try to check the check box in use for another contact under ABC account, the workflow will set the new contact field "unique in use" to have "ABC" and you will get notification that you have duplicates. If you will check another contact under another account, you will have a different value in the field "unique in use" and you won't have a problem.
    – Dagish
    Commented Jul 27, 2015 at 10:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .