-1

i have a method in that there is a map

Map<String,String> formSubmissionData = new Map<String,String>();

formSubmissionData.put('contact__c',contact.Id);
formSubmissionData.put('Enrollment__c',enrollment.Id);
formSubmission = insertFormSubmission(formSubmissionData); 

from this map , loop on this map keyset and populate the form_submission record.

Form_Submission__c formSubmission = new Form_Submission__c( 
    Contact__c = formSubmissionData.get('contactId'),
    Enrollment__c = formSubmissionData.get('enrollmentId'),      
);*/
insert formSubmission;

i tried in this way i'm getting error , can any one help me?

Form_Submission__c formSubmission = new Form_Submission__c();
for( string fs:formSubmissionData.keyset())
{
  formSubmission.fs =formSubmissionData.get(fs);
 }
insert formSubmission;
2
  • 2
    Welcome to SFSE. Please edit your post to give the exact, verbatim error and the line on which it occurs.
    – David Reed
    Nov 20, 2019 at 21:19
  • @madhukar please provide the error message, also I noticed that in your second block of codes you try to retrieve contact with the key 'contactId' but it should be 'contact__c', should return the value
    – Hasantha
    Nov 21, 2019 at 3:53

4 Answers 4

4

You can't refer to properties dynamically like that in Apex.

formSubmission.fs =formSubmissionData.get(fs);

If you want to place a value in the field whose name corresponds to the content of the variable fs, you must use the put() method.

formSubmission.put(fs, formSubmissionData.get(fs));
2
  • Invalid field formSubmissionRecordTypeId for Form_Submission__c , i am getting this error. My concern here i need to use the keyset in the insertFormSubmission method from the above method which has map Nov 20, 2019 at 21:39
  • @MadhukarReddy Please edit your question as requested above to give a clear and complete summary of your issue. I believe the answer here explains your problem, however.
    – David Reed
    Nov 20, 2019 at 21:48
1

You can not use fs like this dynamically in apex. formSubmission.fs =formSubmissionData.get(fs); Use formSubmission.put(fs,formSubmissionData.get(fs)).

0

In your first solution, you had an extra comma right before your closing paren ,) for the constructor of you sObject Form_Submission__c. I have fixed it here:

Form_Submission__c formSubmission = new Form_Submission__c(Contact__c = formSubmissionData.get('contactId'),
                                                            Enrollment__c = formSubmissionData.get('enrollmentId'));
insert formSubmission;
0

formSubmission.fs =formSubmissionData.get(fs);

In above code you are setting formSubmission.fs , if fs is a field in object Form_Submission__c then your code will work fine.

This explanation based on the code you have given plz update the full code to understand your exact problem.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .