0

This code worked for me for the creation of a new template as well as a section, but when I added the creation of a new question. I found a problem in the deployment, I will put all the code in order to better understand and I will indicated the exact line where I have the problem, if anyone of you can help me .

 @AuraEnabled 
  public static void getFormTemplateName(Id TemplateId, string sapValue){

    Form_Template__c newFormTemplate = new Form_Template__c();
   Form_Template__c formTemplate = [SELECT Id ,Name,RecordTypeId,Form_Template_ID__c FROM Form_Template__c 
                                                  WHERE Id =:TemplateId];
    Sales_Organization__c salesOrg = [SELECT Id,Name FROM Sales_Organization__c WHERE SAP_Sales_Org_Code__c =: sapValue]; 
    
    newFormTemplate.RecordTypeId = formTemplate.RecordTypeId;
    newFormTemplate.Name = formTemplate.Name;
    newFormTemplate.Form_Template_ID__c = sapValue+'-'+formTemplate.Name;
    newFormTemplate.Sales_Organization__c = salesOrg.Id;
     
    insert newFormTemplate;

    Id formTemp = newFormTemplate.Id;
    List<Form_Template_Section__c> formTemplateSection = [SELECT Section_ID__c,  Name,Form_Template__c ,Order_Position__c  FROM Form_Template_Section__c 
    WHERE Form_Template__c =: TemplateId ];
  
    List<Form_Template_Section__c> newSection = new List<Form_Template_Section__c>();
    
    for (Form_Template_Section__c section : formTemplateSection){
    Form_Template_Section__c newSectionW = new Form_Template_Section__c();
      newSectionW.Form_Template__c = formTemp;
    newSectionW.Name = section.Name;
    newSectionW.Order_Position__c = section.Order_Position__c;
    newSectionW.Section_ID__c = newFormTemplate.Form_Template_ID__c+'-' +'SECT'+section.Order_Position__c +'teste';
    newSection.add(newSectionW);
    }
    system.debug('sizzze' + newSection.size());
    insert newSection;

    

=>I have problem in this line ,I can't deploy my code and I get as error that : Variable does not exist: Id . I use this method to get the Id of the New Section ( it is a lookup with the object Form_Template_Question__c )

    Id sectionFormTemp = newSection.Id;
   List<Form_Template_Question__c> formTemplateQuestion =
          [SELECT Question__c,Order_Position__c  ,Question_ID__c ,
   Form_Template_Section__c ,Form_Template__c 
          FROM Form_Template_Question__c WHERE Form_Template__c=: TemplateId ];

   List<Form_Template_Question__c> newQuestion = new List<Form_Template_Question__c>();


   for (Form_Template_Question__c question : formTemplateQuestion){

    Form_Template_Question__c newQuestionW= new Form_Template_Question__c();
    
     newQuestionW.Question__c = question.Question__c;
     newQuestionW.Order_Position__c = question.Order_Position__c;
    

     newQuestionW.Form_Template__c = formTemp;
     newQuestionW.Form_Template_Section__c = sectionFormTemp;
     
   
     newQuestionW.Question_ID__c = newFormTemplate.Form_Template_ID__c+'-' +'QUEST'+question.Order_Position__c +'teste';

     newQuestion.add(newquestionW);
     }
     
     insert newQuestion;

  

  }

1 Answer 1

0

newSection is a List<Form_Template_Section__c>, so the error is correct that newSection.Id does not exist. You'll need to figure out which section the question belongs to and use the appropriate ID from there.

That logic should look something like this:

Map<Id, Form_Template_Section__c> newSection = new Map<Id, Form_Template_Section__c>();

for (Form_Template_Section__c section : formTemplateSection){
    Form_Template_Section__c newSectionW = new Form_Template_Section__c();
    newSectionW.Form_Template__c = formTemp;
    newSectionW.Name = section.Name;
    newSectionW.Order_Position__c = section.Order_Position__c;
    newSectionW.Section_ID__c = newFormTemplate.Form_Template_ID__c+'-' +'SECT'+section.Order_Position__c +'teste';
    newSection.put(section.Id, newSectionW);
}
insert newSection.values();

for (Form_Template_Question__c question : formTemplateQuestion){
  Id sectionFormTemp = newSection.get(question.Form_Template_Section__c);
  // ... rest of logic
3
  • how can I determin which section the question belongs to for using the appropriate ID Jul 4, 2022 at 14:36
  • I suppose you probably need a Map<Id, Form_Template_Section__c> to link the old section Id to the new one; let me write up a demo.
    – sfdcfox
    Jul 4, 2022 at 15:00
  • ok thank's for your help Jul 4, 2022 at 15:02

You must log in to answer this question.

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