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
whatever513whatever513 

Calculate Sum from One Tab to Another

I have 2 different Tabs set up - One for Accounts and the other for Management Companies. I have a field on the Accounts Tab called Number of Units. This number is to represent the number of apartments at an apartment complex. The Management Companies Tab contains the names of the Companies that manage the Accounts. A Management Company, at times, can have more than 50 Accounts. I would like to know if there is a way to link these two Tabs so that when a new Account is created and listed with their Number of Units a field can be automatically updated on the Management Company tab so we know how many Units a company might have with us. Does anyone know how to create a formula that will do this?

 

Thanks in advance for any help.

SurekaSureka

Hi,

 

You can create a look up field in Account object. During the Account insert/update,  write a trigger in the Account  to update the field in Management Company.

 

Thanks

whatever513whatever513

Thank you for your help but I'm not sure that is resolving the issue I am talking about. When I add a new Account on the Accounts Tab and input the number of units for this Account, and I have already selected the correct Management Company from a lookup field on the Accounts Tab, I want a field on the Management Company Tab that will automatically update the total number of units this Managemtn Company has.

 

Here's another way to explain it. On the Accounts Tab I add the following Accounts with their Units for a Management Company (called Example 1):

 

Location 1 - 50 Units

Location 2 - 100 Units

Location 3 - 75 Units

Location 4 - 125 Units

 

Now when I created each of these in the Accounts Tab I have already selected Example 1 from a Lookup field called Management Company. What I need is a field on the Management Company Tab that would automatically update with a total of 350 Units once those 4 Accounts above have been added. Is this possible? If so, how can I create this?

 

Thanks in advance.

SurekaSureka

Hi,

 

You can try with the sample code below:

 

Trigger updatemanagement on Account(before insert, before upsert)

{

for(Account a:trigger.new)
{
List<CustomObject__c> conlist = [select SumofUnits__c from CustomObject__c where Id =: a.CustomObject__c];
for(Integer i=0;i<conlist.size();i++)
{
conlist[i].SumofUnits__c = conlist[i].SumofUnits__c +a.Units__c;
}
update conlist;
}

 

 

You can try somethings like the above code.

 

Hope this solves.

 

Thanks

whatever513whatever513

I am not sure this works for what I am looking for; to be honest I do not really understand the language you used in the response you gave so I am not sure that it would work. Let me try explaining again.

 

I have a Custom Tab called Management Companies. When new Accounts are created they are created on the Account Tabs. Each new Account is going to have a specific number of Units (field is titled Number of Units). What I am looking to do is have a field on my Management Company Tab (titled Total Units) that would automatically update every time a new Account is added that is linked to that specific company. I thought I would be able to say this field on the Management Company Tab would contain information using a Roll-Up Summary from the Accounts Tab but it's telling me I can't because this is not the master in a master-detail relationship.

SurekaSureka

Hi,

 

Create a lookup field in Account to the corresponding custom object(Management Companies).

 

Whenever a new Account record is inserted/updated, the below trigger will add the Units value to the corresponding Customobject.

 

CustomObject__c - Lookup field to the custom object

Units__c - custom field in Account.

SumofUnits__c - custom field in Custom object which sums up all the units of Accounts related to it.

 

Trigger updateCustobj on Account(before insert, before upsert)

{

for(Account a:trigger.new)

{

CustomObject__c objlist = [select SumofUnits__c from CustomObject__c where Id =: a.CustomObject__c];   // this will fetch the corresponding Management Company record

if(objlist.SumofUnits__c == null)

{

objlist.SumofUnits__c = 0;

}

else

{

conlist.SumofUnits__c = conlist.SumofUnits__c +a.Units__c;

}

update conlist;

}

}

 

The above is just a sample code.

 

 Thanks