You need to sign in to do that
Don't have an account?
SFDC Panther 92
How to Keep 2 field as a Key in Map
Hi All
I am having a scenario where through a batch i am creating new survey forms in the application and have to copy response from previous survey form to new survey form.
Survey question and Survey answer are 2 different object and survey answer is using survey question ( question Id) as lookup.
Now , When I am creating new survey through batch , since there are multiple survey and each survey has multiple answers records , i am not able to understand how to copy the response from previous survey to new survey.
We will get survey answer record through survey Id and question id , so I want to know how can I keep these 2 field as a Key in Map.
Below is my code where I copy the response from one survey to another but the issue is it work only for 1 survey , not for List of Survey.
List<Survey__answer__C> OldSurvey = [Select questionid , answer__c from survey__c where Survey__c = :oldsurveyId];
List<Survey__answer__C> NewSurvey = [Select questionid , answer__c from survey__c where Survey__c = :newsurveyId];
Map<Id,String> mapofsurvey = new Map Map<Id,String>;
List<Survey__answer__C> SurveyListtoUpdate = new List<Survey__answer__C>;
for(Survey__answer__C oldsurveyAnswer :OldSurvey)
{
mapofsurvey.put(OldSurvey.questionid ,OldSurvey.answer__c);
}
for(Survey__answer__C newsurveyAnswer :NewSurvey)
{
newsurveyAnswer.answer__c = mapofsurvey.get(newsurveyAnswer.questionid);
SurveyListtoUpdate.add(newsurveyAnswer);
}
I am having a scenario where through a batch i am creating new survey forms in the application and have to copy response from previous survey form to new survey form.
Survey question and Survey answer are 2 different object and survey answer is using survey question ( question Id) as lookup.
Now , When I am creating new survey through batch , since there are multiple survey and each survey has multiple answers records , i am not able to understand how to copy the response from previous survey to new survey.
We will get survey answer record through survey Id and question id , so I want to know how can I keep these 2 field as a Key in Map.
Below is my code where I copy the response from one survey to another but the issue is it work only for 1 survey , not for List of Survey.
List<Survey__answer__C> OldSurvey = [Select questionid , answer__c from survey__c where Survey__c = :oldsurveyId];
List<Survey__answer__C> NewSurvey = [Select questionid , answer__c from survey__c where Survey__c = :newsurveyId];
Map<Id,String> mapofsurvey = new Map Map<Id,String>;
List<Survey__answer__C> SurveyListtoUpdate = new List<Survey__answer__C>;
for(Survey__answer__C oldsurveyAnswer :OldSurvey)
{
mapofsurvey.put(OldSurvey.questionid ,OldSurvey.answer__c);
}
for(Survey__answer__C newsurveyAnswer :NewSurvey)
{
newsurveyAnswer.answer__c = mapofsurvey.get(newsurveyAnswer.questionid);
SurveyListtoUpdate.add(newsurveyAnswer);
}
Hi,
Try to make key in this format "surveyId+'~'+questionID". This will give you unique answer related to particular survey and question and use below code. If this solves your query kindly mark as best answer.
for(Survey__answer__C oldsurveyAnswer :OldSurvey)
{
for(Survey__answer__C newsurveyAnswer :NewSurvey)
{
newsurveyAnswer.answer__c = mapofsurvey.get(oldsurveyAnswer.Id+'~'+newsurveyAnswer.questionid);
SurveyListtoUpdate.add(newsurveyAnswer);
}
}
Regards,
Shiraz