Friday, 9 October 2015

update the field values from child - to - Parent Trigger

parent --> quote
child --->quote_product__c

when ever quote _Product__c  object update values related quote object fileds also get updated

trigger UpdateFieldsfromQuote on Quote_Product__c (after update)
{
 Map<id,Quote_Product__c> Mapquoteprod =new Map<Id,Quote_Product__c>();
 for(Quote_Product__c qtp :trigger.new)
 {
    Mapquoteprod.put(qtp.Quote__c,qtp);
 }
    List<Quote> qtList = new List<Quote>();

 for(Quote qt  : [Select id,Brightness__c ,Color_styles__c,Ash__c,Bulk_cc__c
                                                                            from Quote where id =:Mapquoteprod.keyset()])
 {
   qt.Brightness__c    = Mapquoteprod.get(qt.id).Brightness__c;
   qt.Color_styles__c  = Mapquoteprod.get(qt.id).of_Colors_Styles__c;
   qt.Ash__c           = Mapquoteprod.get(qt.id).Ash__c;
   qt.Bulk_cc__c       = Mapquoteprod.get(qt.id).Bulk_CC_gm__c;
   qtList.add(qt);
 }
   if(!qtList.isempty())
   {
     update qtList;
   }
}

update the Field values from parent to child Trigger

Where Account name, phone is update  related contact Lastname and phone also update.

trigger ContactUpdate on Account (after update)
 {
   set<Id> acctIds = new set<Id>();
    map<Id, Account> mapAccount = new map<Id, Account>();
    list<Contact> listContact = new list<Contact>();
    for(Account acct : trigger.new) 
    {
        acctIds.add(acct.Id);
        mapAccount.put(acct.Id, acct);
    }
    listContact = [SELECT LastName ,Phone , AccountId FROM Contact WHERE AccountId IN : acctIds];
    
   if(listContact.size() > 0) 
   {
        for(Contact con : listContact)
        {
            con.LastName= mapAccount.get(con.AccountId).Name;
            con.phone= mapAccount.get(con.AccountId).phone;
        }
        update listContact;
    }

}

display multiple object Records using wraper class

public with sharing class wrappercls {

public class accwrapper{
public Account acc{get; set;}
public List<Contact> lstcon{get; set;}
public List<Case> lstcases{get; set;}
public accwrapper(Account a, List<Contact> lst, List<case> lstcase){
acc = a;
lstcon = lst;
lstcases = lstcase;
}
}

public List<accwrapper> lstwrap{get; set;}

public wrappercls(){

lstwrap = new List<accwrapper>();
List<account> lst = [Select id, name,(Select id, name from Contacts),(Select id,CaseNumber  from cases) from account order by name];

for(Account acc : lst){
if(acc.contacts.size()>1)
lstwrap.add(new accwrapper(acc,acc.contacts,acc.cases));
}
}

}


<apex:page controller="wrappercls">
<apex:form >
<apex:pageblock >
<apex:pageblocktable value="{!lstwrap}" var="w">
<apex:column value="{!w.acc.name}"/>
<apex:column >
<apex:repeat value="{!w.lstcon}" var="c">
{!c.name}<br/>
</apex:repeat>
</apex:column>
<apex:column >
<apex:repeat value="{!w.lstcases}" var="c">
{!c.casenumber}<br/>
</apex:repeat>
</apex:column>
</apex:pageblocktable>
</apex:pageblock>
</apex:form>
</apex:page>