0

Could anyone please tell me apex code for server side contoller about how to create a record on Junction Object(Detail side of Master Detail Relationship). Say There are two objects Products__c and ProductGroup__c which are having many to many Master Detail Relationship using a Junction Object( Junction__c ). From my Client Side Controller I am getting a list of Id's(listToAdd)from Products. The Id of Group should be captured from URL of lightning component.(please tell how to achieve this too) What I want to do is that Create a record on Junction__c populating the Id's from Group and Products (M-D relationship).

Thanks!! My Client Side Contoller :-

   checkboxSelect: function(component, event, helper) {
  // get the selected checkbox value  
  var selectedRec = event.getSource().get("v.value");
  // get the selectedCount attrbute value(default is 0) for add/less numbers. 
  var getSelectedNumber = component.get("v.selectedCount");
  // check, if selected checkbox value is true then increment getSelectedNumber with 1 
  // else Decrement the getSelectedNumber with 1     
  if (selectedRec == true) {
   getSelectedNumber++;
  } else {
   getSelectedNumber--;
  }
  // set the actual value on selectedCount attribute to show on header part. 
  component.set("v.selectedCount", getSelectedNumber);
 },


    // For select all Checkboxes 
    selectAll: function(component, event, helper) {
        //get the header checkbox value  
        var selectedHeaderCheck = event.getSource().get("v.value");
        // get all checkbox on table with "boxPack" aura id (all iterate value have same Id)
        // return the List of all checkboxs element 
        var getAllId = component.find("boxPack");
        // If the local ID is unique[in single record case], find() returns the component. not array   
        if(! Array.isArray(getAllId)){
            if(selectedHeaderCheck == true){ 
                component.find("boxPack").set("v.value", true);
                component.set("v.selectedCount", 1);
            }else{
                component.find("boxPack").set("v.value", false);
                component.set("v.selectedCount", 0);
            }
        }else{
            // check if select all (header checkbox) is true then true all checkboxes on table in a for loop  
            // and set the all selected checkbox length in selectedCount attribute.
            // if value is false then make all checkboxes false in else part with play for loop 
            // and select count as 0 
            if (selectedHeaderCheck == true) {
                for (var i = 0; i < getAllId.length; i++) {
                    component.find("boxPack")[i].set("v.value", true);
                    component.set("v.selectedCount", getAllId.length);
                }
            } else {
                for (var i = 0; i < getAllId.length; i++) {
                    component.find("boxPack")[i].set("v.value", false);
                    component.set("v.selectedCount", 0);
                }
            } 
        }  

    },


 //For Adding selected records 
 addSelected: function(component, event, helper) {
  // create var for store record id's for selected checkboxes  
  var addId = [];
  // get all checkboxes 
  var getAllId = component.find("boxPack");
  // If the local ID is unique[in single record case], find() returns the component. not array
     if(! Array.isArray(getAllId)){
         if (getAllId.get("v.value") == true) {
           addId.push(getAllId.get("v.text"));
         }
     }else{
     // play a for loop and check every checkbox values 
     // if value is checked(true) then add those Id (store in Text attribute on checkbox) in addId var.
     for (var i = 0; i < getAllId.length; i++) {
       if (getAllId[i].get("v.value") == true) {
         addId.push(getAllId[i].get("v.text"));
       }
      }
     } 

     // call the helper function and pass all selected record id's.    
      helper.addSelectedHelper(component, event, delId);

 },

What I tried :-

public with sharing class UpdateWithCheckboxController {

 @AuraEnabled
 public static list < Products__c > fetchPDCN() {
  list < Products__c > returnPDList = new List < Products__c > ();

  List < Products__c > lstP = [SELECT Id,Name,Brand_Trademark_Name__c FROM Products__c LIMIT 50];
  // play for loop on lstP and add each contact to returnPDList List.
  for (Products__c c: lstP) {
   returnPDList.add(c);
  }
 // return the List of PDCN
  return returnPDList;
 }


 @AuraEnabled
 public static List < String > updateRecords(List < String > lstRecordId) {
// for store Error Messages  
    List < String > oErrorMsg = new List < String > ();
// Query Records to add where id in lstRecordId [which is pass from client side controller] 
    List < Products__c > lstUpdateRec = [select Id from Products__c where id IN: lstRecordId];
    return lstRecordId;
    /*List< Junction__c > recToAdd = new List< Junction__c >();
     recToAdd.Product__c = lstUpdateRec;
     recToAdd.Group__c = ;*/

 }
}
  • It is not clear from your code, but the list of ids that you are passing to the apex controller contains just the ids of Product__c or ProductGroup__c or both? – Mohit Arora Aug 17 '18 at 6:52
  • ProductGroup__c id should be from URL . Sorry, I edited the question for this. – Thomas Pete Aug 17 '18 at 6:58
1

To capture the ProductGroup__c Id from the url, include the following attribute in your Lightning component

<aura:attribute name="recordId" type="String"/>

which will by default have the value of the record Id on whose detail page the component is placed.

To pass values from lightning component to apex controller:

var action = component.get( 'c.createJunction' ); action.setParams({ groupId: component.get( 'v.recordId' ), prodIdList: component.get( 'v.prodIdList' ) }); $A.enqueueAction( action );

For the apex controller, you could try something like

@AuraEnabled public static createJunction( String groupId, List<String> prodIdList ) { List<Junction__c> junctionList = new List<Junction__c>(); for( String prodId : prodIdList ) { junctionList.add( new Junction__c( Product__c = prodId, ProductGroup__c = groupId )); } insert junctionList; }

Please make sure the master-detail parent field API names are correct.

Let me know if this works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.