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
sfdc development hintssfdc development hints 

hi, could you please help me out with this trigger

I am trying to write a test class for the trigger below as follows,

//Trigger on "enrollpeople" object which allows the field "Booked slots" to be incremented as the "EnrollPeople"is added and also verifying that "Booked slots" should not be greater than "Totalslots" //



trigger enrollcandidatetrigger on Enroll_Candidate__c(after delete, after insert, after update,after undelete)
{

Enroll_Candidate__c[] coursedetails;
if(Trigger.IsDelete)
coursedetails=Trigger.old;

else
coursedetails=Trigger.new;

Set<ID> CourseId= new set<ID>();

for(Enroll_Candidate__c candidates:coursedetails)
{
CourseId.add(candidates.Course_Details__c);
}

Map<ID,Course_Details__c> coursedetailsforcandidates=new Map<ID,Course_Details__c>([Select Id, Booked_Slots__c, Total_Slots__c, (Select Id from Enroll_Candidates__r ) from Course_Details__c where Id in:CourseId]);
for(Enroll_Candidate__c candidates:coursedetails)
{

Course_Details__c coursess = new Course_Details__c();
coursess = coursedetailsforcandidates.get(candidates.Course_Details__c);


if(coursess.Total_Slots__c >= coursess .Enroll_Candidates__r .size())
      coursess.Booked_Slots__c=coursess .Enroll_Candidates__r .size();
else
        candidates.addError('Booked seats should not exceed Total seats');
}


update coursedetailsforcandidates.values();
}

i have also written the test class as'
@isTest
public class EnrollCandidateTests
 {
 static testMethod void test()
 {

Enroll_Candidate__c ec = new Enroll_Candidate__c(Name ='Test');

insert ec;
}
}
but i am getting an error message "
  
ErrorError: Compile Error: Field is not writeable: Enroll_Candidate__c.Name at line 7 column 55
"because no such field exists also the object has only lookup relationship with other objects, could you plese help m e out with this issue





Best Answer chosen by Admin (Salesforce Developers) 
aruna11.3081223889641084E12aruna11.3081223889641084E12

Hi,

 

From the error, it seems, there is a autonumber field for the object Enroll_Candidate__c.

 

Remember,Whenever a new standard/custom object is created in Salesofrce, it always has the standard field "Name".

By default the datatype for this field is text.

 

In your case it is set as autonumber.

So it is giving you the error: Field not writeable as we cannot feed the data into autonumber field.