With this code , I successfully duplicate the object Template__c by creating a new one : => Apex :

  public static void getFormTemplateName(Id ,TemplateId, string sapValue){
   Template__c newFormTemplate = new Template__c();
    Template__c formTemplate = [SELECT Id ,Name,RecordTypeId,Template_ID__c 
                  FROM Template__c WHERE Id =:TemplateId];
   Organization__c salesOrg = [SELECT Id,Name FROM Organization__c WHERE CORE_SAP_Sales_Org_Code__c =: sapValue];                                              
    newFormTemplate.RecordTypeId = formTemplate.RecordTypeId;
    newFormTemplate.Name = formTemplate.Name;
    newFormTemplate.Template_ID__c = sapValue+'-'+formTemplate.Name;
    newFormTemplate.Organization__c = salesOrg.Id;
    insert newFormTemplate;


 getFormTemplateName({TemplateId: this.recordId, sapValue : this._selected.toString()})

Now I need to do the same thing for the object Template_Section__c .

I use the same button duplicate , and I want to have the new template which is created now with the sections , I want that when I click on the button duplicate template and I switch to the new template created , I find that also the sections of the template is created (section name, orderPosition ,Sales org, idSection(concatenation between the sap from the object Organization__c and the name of template from object Template__c and orderPosition from the section__c ..)

But this not worked for me :

=>Apex :

  public static void getTemplateSection(Id SectionId, string TemplateValue, string sapValue2){

   Template_Section__c newSection = new Template_Section__c();

   Template_Section__c TemplateSection = [SELECT Id ,Name,Template__c , Order_Position__c , Section_ID__c FROM Template_Section__c 
                                                  WHERE Id =:SectionId];

    Template__c formTemplate = [SELECT Id,Name FROM Template__c WHERE Name =: TemplateValue]; 

   Organization__c salesOrg = [SELECT Id,Name FROM COrganization__c WHERE SAP_Sales_Org_Code__c =: sapValue2];                                              

   newSection.Name = TemplateSection.Name;

   newSection.Order_Position__c = TemplateSection.Order_Position__c;

   newSection.Section_ID__c = sapValue2+'-'+ TemplateValue +'SECT'+newSection.Order_Position__c;
    insert newSection;
    System.debug('new section is'+newSection);


1 Answer 1


Your variables are not named consistently. Your query is storing the result in the variable "TemplateSection" instead of "formTemplateSection".

On another note, you probably don't need to have a separate query for Template__c if it's the parent of Template_Section__c, you can just reference the field you need (ex. formTemplate.Id ) when you get to that point - which I assume you're getting there and not just having an extra unreferenced query for no reason.

  • for the variable name I just committed an error when I copied the code from VS to stackExchange . I will trie to use just one query and make you now the result Jun 20, 2022 at 15:32

You must log in to answer this question.

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