Posts

Showing posts with the label Trigger

Update a sObject record related with another sObject using Visualforce

Image
In this part, I am going to explain how to update a contact record related to an account object in the visualforce page. Scenario--  A VF Page showing a list of account record with a radio button and when you select an account from given list then it shows related contacts of selected account in the page and then you can update your record. Controller-- public with sharing class AllAccounts  {     public List<Account> acList {get;set;}     public List<Contact> conList {get;set;}        public string accId {get;set;}     public string eml {get;set;}          public AllAccounts()     {            acList = new List<Account>();         for(Account a : [select id,name,accountnumber from account limit 10])         {             acList.add(a);     ...

Display total number of contacts associated with an account.

In this part, I am going to explain how to calculate the total number of contacts associated with an account object in a very simple way. After calculating the total number of contacts you can display this totals in a field of an account object.  This trigger works in all scenario after insert, after delete , after Undelete and after update. Requirement -- Create a Number type field named NumberOfContact on account object. Trigger -- trigger NumberOfContactOnAccount on Contact (after insert,after update,after delete,after undelete)  {     set<Id> ids = new set<Id>();          if(Trigger.isInsert)     {         for(Contact c : Trigger.new)         {             if(c.AccountId != null)             {                 ids.add(c.AccountId);       ...

Trigger for display or update account field after creating a related contact record

Here I am going to create a trigger that update value of TotalAmount field on Account object after creating a contact record. Requirement-- 1. create a currency type field name TotalAmount on Account object. 2. create two currency type field name AmountX and AmountY on Contact object. Scenario-- When we create or update a contact record with providing value of above created field then total value of AmountX and AmountY displays on TotalAmount field on Account object. Trigger-- trigger TotalAmount on Contact (after insert,after update) {          set<Id> ids = new set<Id>();     for(Contact c : Trigger.new)     {         if(c.AccountId != null){             ids.add(c.AccountId);         }     }          List<Account> acList = [select id,TotalAmount__c,(select id,AmountX__c,AmountY__c from contacts) ...