+ Start a Discussion
SFDC DummySFDC Dummy 

Unable to create Formula Field

Hi All 
I am facing problem when creating formula field it showing

You have reached the maximum number of 10 object references 

How to update field value of one object from other object field with some calculation.is it possible using trigger
Mohit Bansal6Mohit Bansal6
Yes, you can use trigger to update value of one object from other object.

If your objects are linked to each via master child relationship then you use Workflow Field update else you need to write trigger code for it.

Simple Sample Code: 
trigger insertContact on Account (after insert)
Contact cont = new Contact();
cont.LastName = Trigger.new[0].name;
cont.AccountId = Trigger.new[0].ID; 
insert cont;

Complex Sample Code: 
trigger AccountTrigger on Account (after insert, after update) {
    // Create an instance of your helper class
    AccountTriggerHelper helper = new AccountTriggerHelper();

    // Even though this trigger only works after insert or after update currently,
    // there is nothing to stop you from adding to it in the future. Adding this
    // logic will allow you to expand in the future.
    // This says run if the trigger is after the insert OR after the update
    if((Trigger.isAfter && Trigger.isInsert) || (Trigger.isAfter && Trigger.isUpdate)){
        // Now call your helper method

public class AccountTriggerHelper{
   public void createCaseWhenNeeded(List<Account> accounts){
       // We need to store a List of Cases to create.
       List<Case> casesToCreate = new List<Case>();

       // Loop over the accounts. Remember, we don't know how many accounts we will have
       for(Account acc:accounts){
           // This is where you need to determine what your condition will be.
           // You will replicate this system with more if statements or else if
           // statements
           if(acc.Name == 'CreateCase'){
               // Your account meets the criteria, create the case you want and
               // add it to your List of cases
               Case caseToAdd = new Case();

               // Set up any fields you want
               caseToAdd.Name = 'Test';
               caseToAdd.Custom_Field__c = 'Something';

               // You could have also used the following...
               // new Case(Name = 'Test',Custom_Field__c = 'Something');
               // All you need to do is comma delimit all of the fields you want to add
               // Depending on how many fields you want to add, it can be cleaner to 
               // create it like above and then just add it all in one statement

       // You have your full List of cases to add, now just run the insert DML statement
       insert cases;

Nitin PaliwalNitin Paliwal
Hi ,
What is the Calculation which is to be done.Then we can write trigger for that.

SFDC DummySFDC Dummy
Hay I have two object object1 and object2. object1 have some field which i want in object 2 with calulation like in object1 have one field called tax__c which will be update in object2 with tax__c*5%.how it will be possible in trigger
Mohit Bansal6Mohit Bansal6
You can refer below code for reference.

In this code, on insert of new account, it will create a new contact and will set the TaxValue on the basis of Tax percent on account

trigger insertContact on Account (after insert)

    List<Contact> lCont = new List<Contact>();   
for(Account acc : Trigger.new){
    cont.LastName = acc.name;
    cont.AccountId = acc.ID; 
    cont.TaxValue __c  = acc.Tax__c * 5 *0.01;
   insert lcont;


SFDC DummySFDC Dummy
its throwing error.......

Is there any relationship between both object.
If yes please let me know about the relationship.

Gyanender Singh
I think you need to initialize the conatct list by adding line before 12th as
Contact cont = New Contact();
And also make sure you are not missing any required field on conatct.