1

I have 2 custom object(Master Contract and Contact Person), i have lookup relationship between them. I have related list Contact Person in Master Contract Object with button named Recalculation. In Contact Person i have Amount field and in Master contract i have Total Amount field. If i create new contact person(with new amount) and then i click Recalculation button, it will sum the new amount and the old one. The total amount will be shown at Total Amount field in Master Contract. I already have trigger which is executed without using button. But i want this trigger to execute when i click the button. Can anyone help me to solve this problem? Here i type the trigger and image. Thanks.

trigger AddContractMasterTotalAmount on Contact_Person__c (after insert, after delete, after update) {

    if(Trigger.isInsert){
            try {
                for (Contact_Person__c cp : Trigger.new){
                    Contract_Master__c cm = [SELECT Id, Total_Amount__c  FROM Contract_Master__c WHERE Id = :cp.Contact_ID__c];
                    Decimal amount = 0.0;
                    List<Contact_Person__c> l_cp = [SELECT Id, Amount__c FROM Contact_Person__c WHERE Contact_Id__c = :cm.Id];
                    for(Contact_Person__c am_cp : l_cp) {
                        amount += am_cp.Amount__c;  
                    }
                    cm.Total_Amount__c = amount;

                    update cm;
                }
            } catch (Exception e) {
                System.debug(e);
            }
        }

    if(Trigger.isAfter) {
        if(Trigger.isUpdate){
            try {
                for (Contact_Person__c cp : Trigger.old){
                    Contract_Master__c cm = [SELECT Id, Total_Amount__c FROM Contract_Master__c WHERE Id = :cp.Contact_ID__c];
                    Decimal amount = 0.0;
                    List<Contact_Person__c> l_cp = [SELECT Id, Amount__c FROM Contact_Person__c WHERE Contact_Id__c = :cm.Id];
                    for(Contact_Person__c am_cp : l_cp) {
                        amount += am_cp.Amount__c;  
                    }
                    cm.Total_Amount__c = amount;

                    update cm;
                }
            } catch (Exception e) {
                System.debug(e);
            }
        }

        if(Trigger.isDelete){
            try {
                for (Contact_Person__c cp : Trigger.old){
                    Contract_Master__c cm = [SELECT Id, Total_Amount__c FROM Contract_Master__c WHERE Id = :cp.Contact_ID__c];
                    Decimal amount = 0.0;
                    List<Contact_Person__c> l_cp = [SELECT Id, Amount__c FROM Contact_Person__c WHERE Contact_Id__c = :cm.Id];
                    for(Contact_Person__c am_cp : l_cp) {
                        amount += am_cp.Amount__c;  
                    }
                    cm.Total_Amount__c = amount;

                    update cm;
                }
            } catch (Exception e) {
                System.debug(e);
            }
        }
    }
}

enter image description here

3
  • 2
    Can you not use a roll-up summary field? help.salesforce.com/… Jun 2, 2015 at 15:45
  • I agree with @MihaiNeagoe Roll-up Summary is the way to go.
    – dBeltowski
    Jun 2, 2015 at 16:08
  • Roll-up summary is used in master-detail relationship only right? I use lookup relationship. Do you have any idea? Jun 3, 2015 at 1:06

2 Answers 2

1

So you can't run a trigger by clicking a button, unless that button causes a DML operation like insert, update, delete etc.

Your button can call out to an Apex controller method however, where you can do whatever processing you want.

Even better though is this Andrew Fawcett's Declarative Lookup Rollup Summary tool.

I'd still recommend makind this a master-detail relationship and using a rollup, but Andy's tool is a cool option if you really have to have a lookup relationship instead.

0

If Roll up summary is an answer than go for it. If not, create a simple VF page and and override this Recalculate button with this page. The page should be like this:

<apex:page StandardController="Contact_person__c" extensions="Recalculation" action = "{!recalculate}"/> 

Then, create a class like this:

Public class Recalculation{
private string mId;
//ContactMaterLookUpField - This is the field which related to master object.
public Recalculation(Apexpages.standardcontroller sc){
mId = ((Contact_person__c)sc.getrecord()).ContactMaterLookUpField;
}

public Pagereference recalculate(){
//Use mId to query all the child records and update them like you doing in your trigger.
return new PageReference('/'+mId);
}

Let me know if this works for you.

2
  • Hi, Thanks for your help. Is there possibility to use this button? If it possible, may you let me know how to code this button? Jun 3, 2015 at 3:48
  • Yes there is a possibility. The above code will help you achieve that. You should start coding and let me know if you face any error. Jun 3, 2015 at 4:09

You must log in to answer this question.

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