+ Start a Discussion

linking apex code to a field

I want to display a field that does calculations with fields from several other objects.  Can I do it programmatically so that I don't have to have a lot of relationships?

Like maybe...

Account.ThisField = Task.ThatField + Cases.AnotherField;

I have some code that compiles, but how do I get that code attached to that field?  I don't think I need a trigger, I just need that field defined with code, instead of with a formula.

public class ValueScore
        public Decimal Val;

        public Decimal CalculateThis()
          Val = Integer.ValueOf(Account.NumberOfEmployees) - Double.ValueOf (Task.ConvertServicePoints__c);
          return Val;



Thank you.

Best Answer chosen by Admin (Salesforce Developers) 

You can do EXACTLY what you asked:


Account.ThisField = Task.ThatField + Cases.AnotherField;


There is a major issue with this.  Never name your variable the same name as the Object that it is.


Account someAccount = something;

someAccount.AField = myTask.ThatField + myCase.AnotherField;