function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
khaja Mohiddin Shaikkhaja Mohiddin Shaik 

I have a Field Amount__c in contact and TotalAmount__c in Account, Where ever a new contact record is created with some value like100 in Amount and insert another contact record for the same account now i need to show 200 in related account(i need to sum)

vishal kumar 315vishal kumar 315
This is an Arabic language keyboard driver that supports most brands of Arabic language keyboards, and it's now available for download. This driver makes it easy to type Arabic using a QWERTY keyboard. The driver comes in handy when you need to type text consisting mostly of Arabic alphabetical characters into documents, browsers, email apps, and more. ( http://clavierrarabe.com/ (http://clavierrarabe.com/))
CharuDuttCharuDutt
Hii Khaja
Try Below Trigger
trigger NumberOfChild2 on Contact (After Insert,After Update,After Delete) {
List<Account> accList=new List<Account>();

    Set<Id> setAccIds = new Set<Id>();
    if(Trigger.isInsert){
         if(trigger.isAfter){
        for(Contact con : Trigger.new){
            if(con.AccountId != null){
            setAccIds.add(con.AccountId);
            	}
			}
		}
    } 
    system.debug('setAccIds ==> '+setAccIds);
    if(Trigger.isUpdate){
         if(trigger.isAfter){
        for(Contact con : Trigger.new){ 
            if(con.AccountId!=Trigger.oldMap.get(con.Id).AccountId){
               	setAccIds.add(con.AccountId);
                setAccIds.add(Trigger.oldMap.get(con.Id).AccountId);
            	}
          
			}        
        }
    }
    if(Trigger.isDelete){
        if(trigger.isAfter){
        for(Contact con : Trigger.old) { 
            if(con.AccountId != null){
            setAccIds.add(con.AccountId);
            	}
        	}
        }
    }    
    for(Account acc :[Select id,TotalAmount__c,(Select id,Name,Amount__c from contacts) from Account where Id in : setAccIds]){
			integer val = 0;
        for(Contact con : acc.Contacts){
            
            val += integer.valueOf(con.Amount__c );
            system.debug('====> ' +val);
        }
        system.debug(val);
        acc.TotalAmount__c = string.valueOf(val);
        acclist.add(acc);
        
    }
    if(acclist.size()>0){
        update accList;     
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
Suraj Tripathi 47Suraj Tripathi 47
Hi Khaja,
Greetings!

You can implement this without any code.
Create a Master-Detail in Contact and select Account as Parent.
Now, create a Roll-up summary field in Account and Select SUM and select the child field.

If you find your Solution then mark this as the best answer. 

Thank you!

Regards,
Suraj Tripathi
PriyaPriya (Salesforce Developers) 

Hi Khaja,

Salesforce provide standard relationship between Account & Contact. But for this you can create another relationship of Master- Detail. And then create a Rollup Summary on Account to calculate the Sum for Contact Object.

Link to create Master Detail Relationship :- 

https://trailhead.salesforce.com/content/learn/modules/data_modeling/object_relationships

 

Regards,

Priya Ranjan