Skip to main content
kali charan ha fatto una domanda in #Apex
Hi everyone,

   I am newbie to salesforce and i want help to update the parent account of amount field when ever the contact of amount field is inserted or updated.and i have the scenario like when ever the contact is created or updated the amount field of account should be auto populated this is the first scenario. The second scenario is when the 2nd contact is created if the amount field is greater the first amount the greater amount field should be populated on the account amount field. The 3rd scenario is there are two check boxes greater amount and lesser amount in the account object this should be populated with the latest contact whether it should be greater or lesser than the previous account.

The fields are

Account object:

Parent_amount__c = Text field

Greater_amount__c = checkbox

Lesser_Amount__c = checkbox

Contact object:

Amount__c 

I have written some code only for update thing and please help me for second and third scenarios.

The 1st scenario is also giving error for me.

trigger UpdateAmount on Contact (after insert, after update) { //You want it on update too, right?

  Map<ID, Account> parentOpps = new Map<ID, Account>(); //Making it a map instead of list for easier lookup

  List<Id> listIds = new List<Id>();

  for (contact childObj : Trigger.new) {

    listIds.add(childObj.Account.id);

  }

  //Populate the map. Also make sure you select the field you want to update, amount

  //The child relationship is more likely called Quotes__r (not Quote__r) but check

  //You only need to select the child quotes if you are going to do something for example checking whether the quote in the trigger is the latest

  parentOpps = 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]);

  for (contact con: Trigger.new){

     Account myParentOpp = parentOpps.get(con.Account.id);

     myParentOpp.Parent_amount__c = con.Amount__c;

  }

  update parentOpps.values();

}

The error is 

Error: Invalid Data. 

Review all error messages below to correct your data.

Apex trigger UpdateAmount caused an unexpected exception, contact your administrator: UpdateAmount: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.UpdateAmount: line 16, column 1

Please help in fixing the bug and please suggest me for 2nd and 3rd scenarios

Thanks.
16 risposte
  1. 8 set 2016, 17:53

    trigger UpdateAmount on Contact (after insert, after update) { //You want it on update too, right?

    Map<ID, Account> parentOpps = new Map<ID, Account>(); //Making it a map instead of list for easier lookup

    List<Id> listIds = new List<Id>();

    for (contact childObj : Trigger.new) {

    listIds.add(childObj.Account_ID__c);

    }

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

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

    for (contact con: Trigger.new){

    if(parentOpps.containsKey(con.Account_ID__c)) {

    Account myAccount = parentOpps.get(con.Account_ID__c);

    if(myParentOpp.Parent_Amount__c < con.Amount__c) {

    myParentOpp.Parent_Amount__c = con.Amount__c;

    myParentOpp.Greater_Amount__c = true;

    updatedParentOpps.add(myParentOpp);

    } else if(myParentOpp.Parent_Amount__c > con.Amount__c) {

    myParentOpp.Lesser_Amount__c = true;

    updatedParentOpps.add(myParentOpp); }

    }

    }

    if(parentOpps.size()>0){

    update parentOpps.values();

    }

    }

    Looks like I added too many brackets and used .contains instead of .containsKey.