1

I Have a trigger on IT_Asset__c object. And trigger runs on update. The problem is in the error:

Required fields are missing: [Account]: [Account]

I stuck on this error, because i don't understand why it missed. Also Field Account__c is a masterdetail to account object. This is my trigger:

igger ITAssetCashFlowCreate on IT_Asset__c (before update){

 List<Cash_Flow__c> sub=new List<Cash_Flow__c>();
 for(IT_Asset__c it : Trigger.new){

       if(it.FrstDayOfMnth__c == true){

               it.FrstDayOfMnth__c=false;
               Cash_Flow__c cf=new Cash_Flow__c();
               cf.CurrencyIsoCode=it.CurrencyIsoCode;
               cf.Date__c=System.today();
               cf.IT_Asset__c=it.Id;
               cf.RecordTypeId='012b0000000UOay';
               cf.Account__c=it.Account__c;
               cf.Amount__c=-1*it.Total_Asset_Cost_mth__c;
               cf.Cost_Category__c='IT Cost';
               cf.Business_Line_picklist__c=it.Business_Line__c;
               cf.Cost_Center__c=it.Cost_Center_new__c;
               cf.Cost_Line__c=it.Cost_Line_new__c;
               cf.IT_Code__c=it.IT_Code_lookup__c;
               cf.Vendor__c=it.Vendor__c;
               cf.BCS_Entity__c=it.BCS_Entity__c;
               cf.Comment__c='Monthly cost autogenerated';                   

               sub.add(cf);
        }
 }
        if(sub.size()>0)
        insert sub; ***//Error Line***     
}

Updated Trigger code:

Trigger ITAssetCashFlowCreate on IT_Asset__c (after update){

 List<Cash_Flow__c> sub=new List<Cash_Flow__c>();
 for(IT_Asset__c it : Trigger.new){

       if(it.FrstDayOfMnth__c == true){


               Cash_Flow__c cf=new Cash_Flow__c();
               cf.CurrencyIsoCode=it.CurrencyIsoCode;
               cf.Date__c=System.today();
               cf.IT_Asset__c=it.Id;
               cf.RecordTypeId='012b0000000UOay';
               cf.Account__c=it.Account__c;
               cf.Amount__c=-1*it.Total_Asset_Cost_mth__c;
               cf.Cost_Category__c='IT Cost';
               cf.Business_Line_picklist__c=it.Business_Line__c;
               cf.Cost_Center__c=it.Cost_Center_new__c;
               cf.Cost_Line__c=it.Cost_Line_new__c;
               cf.IT_Code__c=it.IT_Code_lookup__c;
               cf.Vendor__c=it.Vendor__c;
               cf.BCS_Entity__c=it.BCS_Entity__c;
               cf.Comment__c='Monthly cost autogenerated';                   
               System.debug(it.Account__c);
               sub.add(cf);
        }
 }
        if(sub.size()>0)
        insert sub;      
}

Full error message:

Trigger.ITAssetCashFlowCreate: line 29, column 1
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Account]: [Account]

Thanks in advance!

  • You need to make changes in trigger event. This event should be "after update" – Devendra Aug 3 '16 at 10:27
  • is it.Account__c null ? – Ratan Paul Aug 3 '16 at 10:27
  • Debug for it.Account__c: 14:09:39:172 USER_DEBUG [26]|DEBUG|0017E00000FZzLRQA1 – Viktor Aug 3 '16 at 11:12
  • Why do you have a hardcoded Id here: EXL.Account__c = '0017E000009SJN1'; ? – smukov Aug 3 '16 at 12:31
  • @smukov I don't really know (This is not my code). I added corrections. See Trigger code. Still geting error on editing IT Asset record. – Viktor Aug 3 '16 at 12:38
0

I think it.Account__c does not have value in it and that is why cf.Account__c is not getting populated and hence you are getting the required field missing error.Check whether it_Account__c has the value or not by using system.debug().

  • Hi! The it.Account__c have a value because it also required field on object IT_Asset__c. – Viktor Aug 3 '16 at 10:37
  • Can we try for "after update" scenario? – Abhijeet Aug 3 '16 at 10:38
  • It still show the error – Viktor Aug 3 '16 at 10:45
  • please put system.debug and check if you are actually getting the value in it_Account__c or not – Abhijeet Aug 3 '16 at 10:55
  • Can you add the updated code? – Devendra Aug 3 '16 at 10:57
0

Please update event as after update.if you use before update the trigger will fire before committing into database so "it.Account__c" value will not available for use().Due to Account__c is required field in Cash_Flow__c object you are getting error.

  • Hi! Just try to use the after update event. I'm still getting the error – Viktor Aug 3 '16 at 10:42
  • Try cf.Account__c=it.Account__c.Id; – ncmouli Aug 3 '16 at 10:45
  • It shows error: Invalid foreign key relationship: IT_Asset__c.Account__c. Even if i use cf.Account__c=it.Account__r.Id; it shows the error – Viktor Aug 3 '16 at 10:47
  • check is there any other trigger Activated on that object – ncmouli Aug 3 '16 at 10:52
  • yes i have another trigger but this trigger do nothing with IT_Asset record. – Viktor Aug 3 '16 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.