0
trigger prioritycheck on Case (before insert, after update) {
    Set<ID> setacc = new  Set<ID>();
    for(case c : trigger.new){
        setacc.add(c.accountid);
    }
    Map<Id,List<Contact>> bottlemap = new Map<Id,List<Contact>>();
    List<Contact> tempList;

    for(case c : trigger.new){
        if(bottlemap != null && bottlemap.containskey(c.accountid)){
            tempList = bottlemap.get(c.accountid);
        }

        for( contact con: tempList){
            List<Contact> conList;
            conlist = new List<Contact>();
            conlist = bottlemap.get(c.accountid);
            if(c.priority == con.priority__c){
                c.contactid = con.id;
                break;
            }
        }
    }
}
Programmable Medley
  • 2,825
  • 1
  • 30
  • 47
parth
  • 1
  • 2
  • What is the problem with the trigger? Are you getting a compile time error? – BarCotter Oct 28 '14 at 11:49
  • trigger is not working no error are coming but the values are same as previously – parth Oct 28 '14 at 11:50
  • There is a error on line "c.priority=conlist.priority__c", You cannot get the priority value from the list. And also can u elabporate your question I am not able to understand it. – tandonprateek Oct 28 '14 at 11:51
  • i am making a trigger on case... case.priority should be equal to contact.priority? – parth Oct 28 '14 at 11:52
  • As an Account can have many child Contact records, you first need to decide how to determine a single priority value for an Account. For example should it be the maximum priority value from all the child Contacts? Also the code and title don't seem to match. – Keith C Oct 28 '14 at 12:00
  • if case.priority changes so all Contact records should be changed,kindle help in code @KeithC – parth Oct 28 '14 at 12:07
  • @tandonprateek can u help – parth Oct 28 '14 at 12:22
  • Are you trying to update all Contact records when a Cases priority changes? – BarCotter Oct 28 '14 at 12:29
  • yeaa @BarCotter – parth Oct 28 '14 at 12:44

1 Answers1

2

Your question is a bit confusing but based on the comments you are trying to update all Contact records when a Cases priority changes.

The (untested) code below should achieve this or at least give you an idea of how to achieve it.

trigger prioritycheck on Case (after update) {
    if (Trigger.isUpdate) {
        Map<Id, String> accountIdToPriority = new Map<Id, String>();
        for (Case c : Trigger.new) {
            // Only fire when priority changes
            if (c.priority__c != Trigger.oldMap.get(c.Id).priority__c) { 
                accountIdToPriority.put(c.accountId, c.priority__c);
            }
        }
        List<Contact> contacts = [select AccountId 
                                  from Contact
                                  where AccountId in :accountIdToPriority.keySet()];

        for (Contact con : contacts) {
            con.priority__c = accountIdToPriority.get(con.AccountId);
        }
        if (contacts.size() > 0) {
            update contacts;
        }
    }
 }
BarCotter
  • 12,331
  • 4
  • 37
  • 58
  • Apex trigger prioritycheck caused an unexpected exception, contact your administrator: prioritycheck: execution of AfterUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Contact.AccountId: Trigger.prioritycheck: line 15, column 1.. @barcotter – parth Oct 28 '14 at 13:13
  • I have updated my answer to fix that issue – BarCotter Oct 28 '14 at 13:22
  • its not making any change as after updating the value the values are same .@barcotter – parth Oct 28 '14 at 13:30
  • 1
    This trigger is untested so I am not sure why its not fully working. It could possibly be that priority__c is not being changed on Case. The code is to give you an idea of what you need to do to achieve your requirements. I suggest you have a look at How do I start to debug my own Apex code? and then see which bits of the code are being executed. – BarCotter Oct 28 '14 at 13:34
  • 4
    @parth you cant expect people to write code for you. Try putting in a few debug statements in the if condition to see if priority is getting changed at first place. Also check if there are any workflows which is resetting the priority in contact. The code provided in the answer by barCotter should work, unless you have made any additional changes to it – Prady Oct 28 '14 at 14:17