+ Start a Discussion
Jay GatsbyJay Gatsby 

I am getting this error while upserting perticular field value from one custom object to other custom object.

User-added image
Two custom objects:
1.Building__c
2.Unit__c and Master-detail with Building__c

i wrote trigger on my unit object


/*trigger addUnitTrigger on Unit__c (after insert, after update) {
    
    List<Building__c>   building = [select id from Building__c];
    List<Id> id= new List<Id>();
    
    for(Building__c b: building){
        
        id.add(b.id);
    }
    
    List<Unit__c>  unit  = [select Add_Units__c, id,Building__c from Unit__c where Unit__c.Building__r.id IN :id];
    Building__c b;
    integer sum=0;
    for(Unit__c u: unit){
        sum= sum + Integer.valueOf(u.Add_Units__c);
        b =  [select id,Number_of_Units__c from Building__c where Id =: u.Building__c];
        b.Number_of_Units__c= sum;
    }
    
    upsert b;
    
}*/
I am not getting the error.
Best Answer chosen by Jay Gatsby
Raj VakatiRaj Vakati
Two Updates .. 

1.Change Number_of_Units__cLength to 18 from 3 
2 . Update the code as below 
 
public Class AvoidRecursion{
    private static boolean firstRun = true;
    public static boolean isFirstRun(){
    if(firstRun){
      firstRun = false;
      return true;
    }else{
        return firstRun;
    }
    }
}
 
trigger addUnitTrigger on Unit__c (after insert, after update) {
     if(AvoidRecursion.isFirstRun())
    {
    List<Building__c>   building = [select id from Building__c];
    List<Id> id= new List<Id>();
    for(Building__c b: building){
        id.add(b.id);
    }
    
    List<Unit__c>  unit  = [select Add_Units__c, id,Building__c from Unit__c where Unit__c.Building__r.id IN :id];
    Building__c b;
    integer sum=0;
    for(Unit__c u: unit){
        sum= sum + Integer.valueOf(u.Add_Units__c);
        b =  [select id,Number_of_Units__c from Building__c where Id =: u.Building__c];
        b.Number_of_Units__c= sum;
    }
    
    upsert b;
	}
    
}