With this code , I successfully duplicate the object Template__c by creating a new one : => Apex :
@AuraEnabled
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;
}
==>JS:
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 :
@AuraEnabled
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);
}