About

Edit photo

Thursday, February 9, 2017

Scenario - Transaction (Child) to Customer (Parent) trigger


//Scenarios on triggers.

trigger tranToCustomer on transaction__c (before insert,before update,after delete,after insert, after update) 
{
 //Scenario-1): If a customer is already existed with a transaction, then need to throwh the error message. 
    if(trigger.isbefore)
    {
        set<id> setCustID = new set<id>();
        if(trigger.isinsert)
        {
            //set<id> setCustID = new set<id>();
            for(transaction__c tran: trigger.new)
            {
                setCustID.add(tran.customer__c);
            }
            
            map<id,customer__c> mapCustomer = new map<id,customer__c>([select id,name,(select id from transactions__r) from customer__c where id in:setCustID]);
            
            list<customer__c> custlist = new list<customer__c>();
            for(transaction__c tran:trigger.new)
            {
                for(customer__c cust:mapCustomer.values())
                {            
                    if(cust.transactions__r.size() > 0)
                    {
                        tran.adderror('cannot create a record under customer, because one transaction is already exist'); 
                    }
                    else
                    {
                        cust.transaction_check__c = true;
                        custlist.add(cust);
                    }
                }
                if(custlist.isempty()==false)
                update custlist;
            }
        }
  
  //scenario-2): when a new transaction is inserting, under a perticular customer, then confirm_transaction__c(checkbox) field value should be updated to checked automatically.
  //scenario-4): if transaction is moved from c1 to c2, uncheck c1 and update c2 checkbox true. if already exist transaction for c2, through an error. and if already 
  //deleted transaction is restored for c2, 
        if(trigger.isupdate)
        {
            //set<id> 
            
            for(transaction__c tran:trigger.new)
            {
                setCustID.add(tran.customer__c);
            }
            
            map<id,customer__c> mapcustomer = new map<id,customer__c>([select id,name,(select id from transactions__r) from customer__c where id in:setCustID]);
            
            list<customer__c> custlist = new list<customer__c>();
            list<customer__c> custOldlist = new list<customer__c>();
            
            for(transaction__c tran:trigger.new)
            {
                customer__c custOld = [select id,name,transaction_check__c from customer__c where id =:trigger.oldmap.get(tran.id).customer__c];
                if(tran.customer__c != trigger.oldmap.get(tran.id).customer__c)
                {
                    for(customer__c cust:mapcustomer.values())
                    {
                        if(cust.transactions__r.size()>0)
                            tran.adderror('Already the new customer is having a transaction with it, you can\'t allow to update another transaction again.');
                        else
                        {
                            cust.transaction_check__c = true;
                            custOld.transaction_check__c = false;
                            //cust.setCustID.transaction_check__c = true;
                            custlist.add(cust);
                            custOldlist.add(custOld);
                        }
                    }
                }
                if(custlist.isempty()==false )
                {
                    update custlist;
                }
                if(custOldlist.isempty()==false)
                    update custOldlist;
            }
        }
    }
 
 //scenario-3): if transaction__c is deleted under cusomer__c, the checkbox in customer should be uncheck.
    if(trigger.isafter)
    {
        if(trigger.isdelete)
        {
            set<id> setCustID = new set<id>();
            for(transaction__c tran:trigger.old)
            {
                setCustID.add(tran.customer__c);
            }
            
            map<id,customer__c> mapCust = new map<id,customer__c>([select id,name,(select id from transactions__r) from customer__c where id in:setCustID]);
            
            list<customer__c> custlist = new list<customer__c>();
            for(customer__c cust:mapCust.values())
            {
                if(cust.transactions__r.size() == 0)
                {
                    cust.transaction_check__c = false;
                    custlist.add(cust);
                }
            }
            update custlist;
        }
    }
}

0 comments:

Post a Comment