+ Start a Discussion
RiyajRiyaj 

Insert record is not working?

 

When I am going to insert a record using following apex code, but it is not working. In my table in two fields are i m using lookup table. one is studentname, class name..

 

APEX CODE

global class InsertOneAttendanceDate{
WebService static void InsertOneDateAttendance(string studentname,string classname,datetime dtattendance){
Attendance__c i=new Attendance__c(student_Name__r.Name= studentname,class_Name__r.Name= classname,attendance_Date__c= dtattendance);
Insert i;
}
}

 Error: Compile Error: Invalid field initializer: student_Name__r.Name at line 3 column 35

 

 

 

global class InsertOneAttendanceDate{
WebService static void InsertOneDateAttendance(string studentname,string classname,datetime dtattendance){
Attendance__c i=new Attendance__c(student_Name__r.Name= studentname,class_Name__r.Name= classname,attendance_Date__c= dtattendance);
Insert i;
}
}

Best Answer chosen by Admin (Salesforce Developers) 
kiranmutturukiranmutturu

u r trying to insert Attendance__c  object so you can have the provision to use those fields in the insertion and student name and class names are different objects right ? here u need to get the ids to assing them in Attendance__c  object not the names or u need to query those objects to get that ids and assign them...

 

like

 

Attendance__c i=new Attendance__c(student_Name__c= studentname(this should be id),class_Name__c= classname(this should be id),attendance_Date__c= dtattendance);
Insert i;


All Answers

kiranmutturukiranmutturu

u r trying to insert Attendance__c  object so you can have the provision to use those fields in the insertion and student name and class names are different objects right ? here u need to get the ids to assing them in Attendance__c  object not the names or u need to query those objects to get that ids and assign them...

 

like

 

Attendance__c i=new Attendance__c(student_Name__c= studentname(this should be id),class_Name__c= classname(this should be id),attendance_Date__c= dtattendance);
Insert i;


This was selected as the best answer
sanjdevsanjdev

Hi,

 

The assignment of the following type is not allowed or is invalid:-

student_Name__r.Name= studentname    //wrong

rather you should try assigning the id of student_Name__c and change the Name of the Student.

You are try to change or assign name of student_Name__r.Name= studentname which is generating the error.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

 

Cheers

Sanj

RiyajRiyaj

Hi..

 

Thanks for ur help. When Passing the corresponding id which parameter, record is insetered. but i could not view in saledforce Tab area.... because i m using asp.net grid view load the table values.. that time i can view the inserted data's but not in live salesforce site..even i got debuging.... What is the reason?

 

if any additional parameter i need to passing?