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
satakshisatakshi 

REQUIRED_FIELD_MISSING error

Hello, 
I am writing code to create calender records. I am facing this error.

Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [MTP]: [MTP]
Error is in expression '{!createdtp}' in component <apex:page> in page createdtp: Class.Calender.createdtp: line 40, column 1
An unexpected error has occurred. Your development organization has been notified.

Can anyone please help me to sort it out. MTP is master detail field

My code is:

Controller:
public with sharing class Calender {

    public Calender(ApexPages.StandardController controller)
     {
          tid = ApexPages.currentPage().getParameters().get('id');
         System.debug('***************dtp added*******************'+tid); 

     }

 public Decimal dy;
 public  id tid{get;set;}
    public PageReference createdtp() {
     
      System.debug('***************tid tid*******************'+tid);
    List<MTP__c> obj = [Select  Id, Month__c, Year__c,  February__c, NumberOfDays__c from MTP__c];
 
    for(MTP__c mt:obj)
    {
    if(mt.Month__c=='February')
    {
    String str=mt.February__c;
    dy=Decimal.ValueOf(str);
    }
    else
    {
     Decimal str= mt.NumberOfDays__c;
      dy=str;
    }
    }
  List<DTP__c> newdtp= new List<DTP__c>();
    for(Integer i = 1; i<=dy; i++)
    {
       DTP__c  dt = new DTP__c();
       dt.MTP__c=tid;
       newdtp.add(dt);
       System.debug('***************dtp added*******************'+tid);
       //insert newdtp;
       
    }
    insert newdtp;
    return null;
    }
        
    public List<DTP__c> getMydtpList(){
mydtpList =[select Name, Expenses__c, MTP__c, Food_Expenses__c, Date__c, Visit_type__c from DTP__c where id=:tid];

    return mydtpList ;
}
    List<DTP__c>  mydtpList = [select Expenses__c, Name, MTP__c, Food_Expenses__c, Visit_type__c from DTP__c where id=:tid];

    }

VF Page:

<apex:page standardController="MTP__c"  extensions="Calender" action="{!createdtp}">
 <apex:pageBlock title="All Dtp">
 <apex:pageblockTable value="{!mydtpList}" var="item">
 <apex:column value="{!item.Name}"/> 
 <apex:column value="{!item.Date__c}"/> 
 <apex:column value="{!item.Visit_type__c}"/> 
 <apex:column value="{!item.Food_Expenses__c}"/> 
</apex:pageblockTable>
 </apex:pageBlock> 
</apex:page>

Thanks & Regards,
Satakshi
Best Answer chosen by satakshi
Virendra ChouhanVirendra Chouhan
THen you need to provide MTP__C object's record Id. Because for your child object record you arew refering tid as an perentId (MTP__c record id) and you are capturing tid from url see your code:
tid = ApexPages.currentPage().getParameters().get('id');

So you must have to provide MTP__c object's record id in url. Just after your page name in URL add ?id=recordId.
(replace recordId with 15 digit id of that parent record)
 

All Answers

satakshisatakshi
no
 
Virendra ChouhanVirendra Chouhan
THen you need to provide MTP__C object's record Id. Because for your child object record you arew refering tid as an perentId (MTP__c record id) and you are capturing tid from url see your code:
tid = ApexPages.currentPage().getParameters().get('id');

So you must have to provide MTP__c object's record id in url. Just after your page name in URL add ?id=recordId.
(replace recordId with 15 digit id of that parent record)
 
This was selected as the best answer
satakshisatakshi
Thanks you virendra . its working