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
FinneyFinney 

Trigger to get the total of Transactions

Hi

 

I have an object named YTD (Year To Date) and another object which is Transactions.

 

I want to create 5 custom fields on the YTD object.

 

Field 1 is Achieved - This should be the total number of Transactions done by a particular user in 2012. For example if a user has done 10 transactions in 2012, the total value of all the transactions should appear in the field. Starts from Jan 1 and will end on 31 Dec.

 

Field 2 - Value of Property Sold - This should be the total value of Purchase Price (where purchase price is a currency field on transaction object) from the transaction object. This one is also for 2012

 

Field 3 - Value of Property Leased -  This should be the total value of Annual Rent(where Annual Rent is a currency field on transaction object) from the transaction object.  This one is also for 2012

 

Field 4 - Average Sale Fee - This should be Achieved (Field 1)divided by Value of Property Sold (Field 2)

 

Field 5 - Average Rental Fee - This should be Achieved(Field 1) divided by Value of Property Leased.(Field 3)

 

All the above fields are for the current financial year i.e. Jan 2012 - Dec 2012

 

Can anyone please help me in doing this.

 

Many Thanks

 

Finney

Best Answer chosen by Admin (Salesforce Developers) 
TheIntegratorTheIntegrator

code could be something like this, there might be some errors in the code as well is incomplete (straight forward computation part), so you have to modify as per your org. I also assume that YTD is a lookup or master-detail in Transactions

 

trigger aggregate on Transactions__c (after insert, after update) {

List <Transactions__c> transactions = new List <Transactions__c>();

List<YTD__c> ytd = new List<YTD__c>();

Double Achieved=0,VPS=0,VPL=0, ASF=0;ARF=0;

 

List <Id> ytdId = new List<Id>();

for(Transcations__c t: trigger.new){

if(t.YTD__c !=null)

ytdId.add(t.YTD__c);

}

 

ytd = [Select Id, Achieved__c, Value_Of_Property_Sold__c, Value_Of_Property_Leased__c, Average_Sale_Fee__c, Average_Rental_Fee__c From YTD__c where Id in :ytdId];

transactions = [Select Purchase_Price__c, Annual_Rent__c, YTD__c From Transactions__c Where YTD__c IN : ytdId AND CALENDAR_YEAR(CreatedDate) = THIS_YEAR];

 

for(YTD__c y: ytd){

Achieved=0;VPS=0;VPL=0;ASF=0;ARF=0;

for(Transations__c t: transactions){

if(t.YTD__c == y.Id){

//do the computations here, set the Double variables

}

}

//Assign the double variables to Y fields here

}

update(ytd);

}

 

Hope it helps

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

 

I think you can do this through the Master Details relation between these two objects. Create a master detail field on transaction object with YTD object. Now you can create the Rollup summary field on YTD object which will store the desire value of sum of transaction field (now you have filtering criteria for rollup field also, come in spring 2012). So by using the filtering criteria on rollup summary field you can easily achieve this. If you can’t make the master detail relation between these two objects then you have to write a trigger on Transaction object to do this.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

FinneyFinney

Dear Ankit

 

Thanks a lot for your help.

 

I have tried doing this but it din't come off as desired.

 

Can you please help me with the trigger.

 

Thanks and kind regards

 

Finney

MandyKoolMandyKool

Hi Finney,

 

I can help you with this one.

 

If possible you can drop an email to me. Also we can connect over skype and I will help you in writing the trigger.

 

 

My Skype id is - mandar19.kulkarni 

TheIntegratorTheIntegrator

code could be something like this, there might be some errors in the code as well is incomplete (straight forward computation part), so you have to modify as per your org. I also assume that YTD is a lookup or master-detail in Transactions

 

trigger aggregate on Transactions__c (after insert, after update) {

List <Transactions__c> transactions = new List <Transactions__c>();

List<YTD__c> ytd = new List<YTD__c>();

Double Achieved=0,VPS=0,VPL=0, ASF=0;ARF=0;

 

List <Id> ytdId = new List<Id>();

for(Transcations__c t: trigger.new){

if(t.YTD__c !=null)

ytdId.add(t.YTD__c);

}

 

ytd = [Select Id, Achieved__c, Value_Of_Property_Sold__c, Value_Of_Property_Leased__c, Average_Sale_Fee__c, Average_Rental_Fee__c From YTD__c where Id in :ytdId];

transactions = [Select Purchase_Price__c, Annual_Rent__c, YTD__c From Transactions__c Where YTD__c IN : ytdId AND CALENDAR_YEAR(CreatedDate) = THIS_YEAR];

 

for(YTD__c y: ytd){

Achieved=0;VPS=0;VPL=0;ASF=0;ARF=0;

for(Transations__c t: transactions){

if(t.YTD__c == y.Id){

//do the computations here, set the Double variables

}

}

//Assign the double variables to Y fields here

}

update(ytd);

}

 

Hope it helps

This was selected as the best answer
FinneyFinney

Dear Integrator

 

Can you give me the test class for the above trigger please.

 

Thanks and Kind Regards

 

Finney