0

Here i have a requirement and i am facing the issue in the trigger

  1. Fill Parent object (Parent Amount field) with Child Amount value when a child is created for the first time.
  2. Fill/Check Parent object (Greater value checkbox field) when a child is created and if the amount is greater than the previous/all child's amount.(Uncheck in case lesser value Checkbox is checked already) ,also Populate the same Child Amount value on Parent Amount Field .
  3. Fill/Check Parent object (lesser value checkbox field) when a child is created and if the amount is less than the previous/all child's amount.(Uncheck in case Greater value Checkbox is checked already).in this scenario Don't Update Child Amount value on Parent Amount Field .

I have written the code but its not working properly can anyone modify the trigger

trigger UpdateAmount on Contact (after insert, after update) 
{

    Set<Id> listIds = new Set<Id>();
    for (contact childObj : Trigger.new) 
    {
        listIds.add(childObj.AccountID);
    }

    Map<ID, Account> parentAccs  = new Map<Id, account>([SELECT id, Parent_amount__c,Greater_amount__c,Lesser_Amount__c,Name,(SELECT ID, Amount__c FROM contacts) FROM Account WHERE ID IN :listIds]);

    List<Account> updatedparentAccs = new List<Account>();

    for (contact con: Trigger.new)
    {

             Account  myParentOpp = parentAccs.get(con.AccountID);
             myParentOpp.Parent_amount__c= con.Amount__c;
             List<Contact> lstCont = myParentOpp.contacts;

             if(lstCont.size()>1 && con.Amount__c>lstCont[0].Amount__c)
             {
                myParentOpp.Greater_Amount__c = true;
                myParentOpp.Lesser_Amount__c = false;

             }
             if(lstCont.size()>1 && con.Amount__c<lstCont[0].Amount__c)
             {
               myParentOpp.Greater_Amount__c = false;
                myParentOpp.Lesser_Amount__c = true;

             }

                updatedparentAccs.add(myParentOpp);

    }


    if( !updatedparentAccs.isEmpty() )
    {
        update updatedparentAccs;
    }

}
  • 1
    Define "Not working properly". Are you getting errors? What is happening and what do you expect to happen. Giving multiple requirements and a code dump and just saying it is not working is to broad. See How to Ask or Help center – Eric Sep 10 '16 at 14:38
  • there's a possible conceptual issue here -- when child object is deleted - what should happen then? – cropredy Sep 10 '16 at 15:25
  • then amount and check boxes should become blank – Kali Sep 10 '16 at 16:25
1

I think you can easily solve you issue by focusing on the Parent Accounts fields when a child is created

   trigger UpdateAmount on Contact (after insert, after update) 
{

    Set<Id> listIds = new Set<Id>();
    for (contact childObj : Trigger.new) 
    {
        listIds.add(childObj.AccountID);
    }

    Map<ID, Account> parentAccs  = new Map<Id, account>([SELECT id, Parent_amount__c,Greater_amount__c,Lesser_Amount__c,Name FROM Account WHERE ID IN :listIds]);

    List<Account> updatedparentAccs = new List<Account>();

    for (contact con: Trigger.new)
    {

             Account  myParentOpp = parentAccs.get(con.AccountID);

             if(myParentOpp.Parent_amount__c == NULL) {
                  myParentOpp.Parent_amount__c= con.Amount__c;
             } else if (con.Amount__c > myParentOpp.Parent_amount__c){
                  myParentOpp.Parent_amount__c= con.Amount__c;
                  myParentOpp.Greater_Amount__c = true;
                  myParentOpp.Lesser_Amount__c = false;
             } else if (con.Amount__c < myParentOpp.Parent_amount__c) {
                  myParentOpp.Greater_Amount__c = false;
                  myParentOpp.Lesser_Amount__c = true;
             }
                updatedparentAccs.add(myParentOpp);

    }


    if( !updatedparentAccs.isEmpty() )
    {
        update updatedparentAccs;
    }

}
|improve this answer|||||
  • Thanq so much for ur reply but its not updating correctly for example for account it has 2 contacts with 10 and 20 amount if i have inserted next amount as 12 the greater amount checkbox of should be checked when compared with 10 and 20 when i give 5 as amount for the contact it should show lesser amount checkbox .Please reply me back – Kali Sep 10 '16 at 15:00
  • @Kali This does not work, with existing data. And writing code that would not be relevant after existing data scenarios are handled will not be efficient. Do a data patch to set the correct values for existing data, and then this trigger should be sufficient. – Vishal Shah Sep 11 '16 at 15:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.