• Russell Farmer 9
  • NEWBIE
  • 25 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 3
    Replies
I have an Apex Class that basically queries data and it takes opportunity ID as an input. I am trying to write test class that creates the opportunity in test data and then I need to get the ID and pass it to the method. 

Here's my code:

Main Class:
public class RFPDateController{

    @AuraEnabled
    public static List <Milestone__c> fetchDates(String oppId) {
        //Qyery 10 
        List<Milestone__c> dateList = [SELECT ID, Name, Milestone_Type__c, Milestone_Date__c, Opportunity_Related__c from Milestone__c where Opportunity_Related__c != null and Opportunity_Related__c =: oppId];
        //return lis
        return dateList;
    }
    
}

Test Class
 
@isTest
private class RFPDateControllerTEST {
    
    static private map<String, Schema.RecordTypeInfo> accRTmap = Account.SObjectType.getDescribe().getRecordTypeInfosByName();
    static private map<String, Schema.RecordTypeInfo> conRTmap = Contact.SObjectType.getDescribe().getRecordTypeInfosByName();
    //static private Opportunity opp = new Opportunity();
    
    
        @isTest
    	static void validateGetMst(){
        CreateTestData();
        Test.startTest();
        List<Opportunity> opId = [SELECT Id FROM Opportunity WHERE Name='Test Opportunity' LIMIT 1];
        List<Milestone__c> mstResult = new List<Milestone__c>();
        mstResult = RFPDateController.fetchDates(opId);
        Test.stopTest();
        System.assertEquals(1,mstResult.size());
    	}
    
    static private void CreateTestData(){
        
        //Customer ACCOUNT
        Account acc = new Account();
        acc.Name = 'Test Customer Account';
        acc.RecordTypeId = accRTmap.get('Customer').getRecordTypeId();
        acc.Physical_Location__c='True';
        acc.Distributor_Types__c = 'Broker';
        acc.ShippingStreet='100 Market Street';
        acc.ShippingState='Texas';
        acc.ShippingPostalCode='75428';
        acc.ShippingCity='Houston';
        insert acc;
        
        //Distributor ACCOUNT (3)
        Account acc1 = new Account();
        acc1.Name = 'Test Distributor Account 1';
        acc1.RecordTypeId = accRTmap.get('Distributor').getRecordTypeId();
        acc1.Physical_Location__c='True';
        acc1.Distributor_Types__c = 'Broker';
        acc1.ShippingStreet='10 Chester Rd';
        acc1.ShippingState='Pennsylvania';
        acc1.ShippingPostalCode='19103';
        acc1.ShippingCity='Philadelphia';
        insert acc1;
        
        Account acc2 = new Account();
        acc2.Name = 'Test Distributor Account 2';
        acc2.RecordTypeId = accRTmap.get('Distributor').getRecordTypeId();
        acc2.Physical_Location__c='True';
        acc2.Distributor_Types__c = 'Broker';
        acc2.ShippingStreet='12 Jump Street';
        acc2.ShippingState='New Jersey';
        acc2.ShippingPostalCode='07041';
        acc2.ShippingCity='Parsippany';
        insert acc2;
        
        Account acc3 = new Account();
        acc3.Name = 'Test Distributor Account 3';
        acc3.RecordTypeId = accRTmap.get('Distributor').getRecordTypeId();
        acc3.Physical_Location__c='True';
        acc3.Distributor_Types__c = 'Broker';
        acc3.ShippingStreet='Wayne';
        acc3.ShippingState='Texas';
        acc3.ShippingPostalCode='75428';
        acc3.ShippingCity='commerce';
        insert acc3;
        
        //Opportunity
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opportunity';
        opp.StageName = 'Pending';
        opp.CloseDate = System.today() + 30;
        opp.AccountId = acc.id;
        opp.Distributor__c=acc3.id;
        opp.Effective_Date__c=system.today();
        opp.Date_Due_To_Sales_Rep__c=system.today();
        opp.Questionnaire__c =true;
        insert opp;
        
        //Milestone
        Milestone__c mstn = new Milestone__c();
        mstn.Opportunity_Related__c = opp.Id;
        mstn.Name = opp.Name;
        mstn.Milestone_Date__c = System.today() + 30;
        mstn.Milestone_Type__c = 'Due Date';
        insert mstn;
        
    }

}

​​​​​​​
Hi have a lightning component datatable that is populated by a query with a subquery in it. I need the second column to be poulated with a value from the subquery but I can't seem to figure out how to set the field name for the the subquery field. 

Screen shot: (RPF Con should map to sub query field.
User-added imageApex Class
public class RFPOppListController {

        @AuraEnabled
    public static List <Opportunity> fetchOpps (String oppId) {
        //Qyery 10 
        List<Opportunity> oppList = [SELECT Opportunity.Name, (select Contact__r.Name from Opportunity_Contact_Roles__r where Contact_roles__c = 'RFP consultant') FROM Opportunity];
        //return lis
        return oppList;
    }
}

Component:
<aura:component controller="RFPOppListController" access="global" implements="force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId">

    <aura:attribute type="Opportunity[]" name="oppList"/>
    <aura:attribute name="mycolumns" type="List"/>
    <aura:attribute name="updatedRecord" type="Object[]" />


     
    <aura:handler name="init" value="{!this}" action="{!c.fetchOPPS}"/>
     
    <lightning:datatable aura:id="oppsDataTable"
                         data="{! v.oppList }"
                         columns="{! v.mycolumns }"
                         keyField="Id"
                         hideCheckboxColumn="true"
                         onsave ="{!c.onSave}"
                         />
     
</aura:component>

Controller
({
    fetchOPPS : function(component, event, helper) {
        helper.fetchOPPSHelper(component, event, helper);    

    }
})
Helper
({
    fetchOPPSHelper : function(component, event, helper) {
        component.set('v.mycolumns', [
            {label: 'Opp Name', fieldName: 'Name', editable:'true', sortable:'true', type: 'text'},          
            {label: 'RFP Con', fieldName: 'Opportunity_Contact_Roles__r.Contact__r.Name', editable:'true', sortable:'true', type: 'date'}   
            ]);
        debugger;
        var action = component.get("c.fetchOpps");      
        var operID = component.get("v.recordId");
        console.log("operID" + operID);
        action.setParams({
            oppId:operID
        });
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.oppList", response.getReturnValue());
            }
        }); 
        $A.enqueueAction(action);
    }
})


BTW, here is a debugger screenshot of of the value I am trying to display:
User-added image
I am trying to get a datatable to reload after it saves. All of my code is working correctly and the I have added console.logs to my reloadDataTable function so I know it is going through the code. However, it is not refresshing the component.
Here is my screen: 
User-added image

Here is my code:
Class
public class RFPDateController{

    @AuraEnabled
    public static List <Work_Date__c> fetchDates(String oppId) {
        //Qyery 10 
        List<Work_Date__c> dateList = [SELECT ID, Name, Date_Type__c, Work_Date__c, Opportunity_Related__c from Work_Date__c where Opportunity_Related__c != null and Opportunity_Related__c =: oppId];
        //return lis
        return dateList;
    }
    
        @AuraEnabled
    public static boolean updateDates(List<Work_Date__c> editedDatesList){
        try{
            update editedDatesList;
            return true;
        } catch(Exception e){
            return false;
        }
    }
}
Component
<aura:component controller="RFPDateController" access="global" implements="force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId">

    <aura:attribute type="Work_Date__c[]" name="dateList"/>
    <aura:attribute name="mycolumns" type="List"/>
    <aura:attribute name="updatedRecord" type="Object[]" />
     
    <aura:handler name="init" value="{!this}" action="{!c.fetchDTS}"/>
     
    <lightning:datatable aura:id="datesDataTable"
                         data="{! v.dateList }"
                         columns="{! v.mycolumns }"
                         keyField="Id"
                         hideCheckboxColumn="true"
                         onsave ="{!c.onSave}"
                         />
     
</aura:component>

Controller
({
    fetchDTS : function(component, event, helper) {
        helper.fetchDTSHelper(component, event, helper);    

    },
     /*
     * This function is calling saveDataTable helper function
     * to save modified records
     * */
    onSave : function (component, event, helper) {
        helper.saveDataTable(component, event, helper);
    }
})

Helper: This has my save function and the reload code. 
({
    fetchDTSHelper : function(component, event, helper) {
        component.set('v.mycolumns', [
            {label: 'Date Type', fieldName: 'Date_Type__c', editable:'true', sortable:'true', type: 'text'},
            {label: 'Date Name', fieldName: 'Name', editable:'true', sortable:'true', type: 'text'},            
            {label: 'Complete Date', fieldName: 'Work_Date__c', editable:'true', sortable:'true', type: 'date'}   
            ]);
        var action = component.get("c.fetchDates");
        console.log(component.get("v.dateList"));
        var operID = component.get("v.recordId");
        console.log("operID" + operID);
        action.setParams({
            oppId:operID
        });
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.dateList", response.getReturnValue());
            }
        });
        $A.enqueueAction(action);
    },
        /*
     * This function get called when user clicks on Save button
     * user can get all modified records
     * and pass them back to server side controller
     * */
    saveDataTable : function(component, event, helper) {
        var editedRecords =  component.find("datesDataTable").get("v.draftValues");      
        var totalRecordEdited = editedRecords.length;
        var action = component.get("c.updateDates");
        action.setParams({
            'editedDatesList' : editedRecords
        });     
        action.setCallback(this,function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                //if update is successful
                if(response.getReturnValue() === true){
                    helper.showToast({
                        "title": "Record Update",
                        "type": "success",
                        "message": totalRecordEdited+" Account Records Updated"
                    });
                    helper.reloadDataTable();

                } else{ //if update failed
                    helper.showToast({
                        "title": "Error!!",
                        "type": "error",
                        "message": "Error in update"
                    });
                }
            }
        });
        $A.enqueueAction(action);
    },
    /*
     * Show toast with provided params
     * */
    showToast : function(params){
        var toastEvent = $A.get("e.force:showToast");
        if(toastEvent){
            toastEvent.setParams(params);
            toastEvent.fire();
        } else{
            alert(params.message);
        }
    },

    /*
     * reload data table
     * */
    reloadDataTable : function(){
    var refreshEvent = $A.get("e.force:refreshView");
        console.log("reload1");
        if(refreshEvent){
            console.log("reload2");
            refreshEvent.fire();
            console.log("reload3");
        }
    },
})

Credit where due: I'm using code I foudn here: https://sfdcfacts.com/lightning/editable-lightningdatatable-summer18-feature/
 
I have an Apex Class that basically queries data and it takes opportunity ID as an input. I am trying to write test class that creates the opportunity in test data and then I need to get the ID and pass it to the method. 

Here's my code:

Main Class:
public class RFPDateController{

    @AuraEnabled
    public static List <Milestone__c> fetchDates(String oppId) {
        //Qyery 10 
        List<Milestone__c> dateList = [SELECT ID, Name, Milestone_Type__c, Milestone_Date__c, Opportunity_Related__c from Milestone__c where Opportunity_Related__c != null and Opportunity_Related__c =: oppId];
        //return lis
        return dateList;
    }
    
}

Test Class
 
@isTest
private class RFPDateControllerTEST {
    
    static private map<String, Schema.RecordTypeInfo> accRTmap = Account.SObjectType.getDescribe().getRecordTypeInfosByName();
    static private map<String, Schema.RecordTypeInfo> conRTmap = Contact.SObjectType.getDescribe().getRecordTypeInfosByName();
    //static private Opportunity opp = new Opportunity();
    
    
        @isTest
    	static void validateGetMst(){
        CreateTestData();
        Test.startTest();
        List<Opportunity> opId = [SELECT Id FROM Opportunity WHERE Name='Test Opportunity' LIMIT 1];
        List<Milestone__c> mstResult = new List<Milestone__c>();
        mstResult = RFPDateController.fetchDates(opId);
        Test.stopTest();
        System.assertEquals(1,mstResult.size());
    	}
    
    static private void CreateTestData(){
        
        //Customer ACCOUNT
        Account acc = new Account();
        acc.Name = 'Test Customer Account';
        acc.RecordTypeId = accRTmap.get('Customer').getRecordTypeId();
        acc.Physical_Location__c='True';
        acc.Distributor_Types__c = 'Broker';
        acc.ShippingStreet='100 Market Street';
        acc.ShippingState='Texas';
        acc.ShippingPostalCode='75428';
        acc.ShippingCity='Houston';
        insert acc;
        
        //Distributor ACCOUNT (3)
        Account acc1 = new Account();
        acc1.Name = 'Test Distributor Account 1';
        acc1.RecordTypeId = accRTmap.get('Distributor').getRecordTypeId();
        acc1.Physical_Location__c='True';
        acc1.Distributor_Types__c = 'Broker';
        acc1.ShippingStreet='10 Chester Rd';
        acc1.ShippingState='Pennsylvania';
        acc1.ShippingPostalCode='19103';
        acc1.ShippingCity='Philadelphia';
        insert acc1;
        
        Account acc2 = new Account();
        acc2.Name = 'Test Distributor Account 2';
        acc2.RecordTypeId = accRTmap.get('Distributor').getRecordTypeId();
        acc2.Physical_Location__c='True';
        acc2.Distributor_Types__c = 'Broker';
        acc2.ShippingStreet='12 Jump Street';
        acc2.ShippingState='New Jersey';
        acc2.ShippingPostalCode='07041';
        acc2.ShippingCity='Parsippany';
        insert acc2;
        
        Account acc3 = new Account();
        acc3.Name = 'Test Distributor Account 3';
        acc3.RecordTypeId = accRTmap.get('Distributor').getRecordTypeId();
        acc3.Physical_Location__c='True';
        acc3.Distributor_Types__c = 'Broker';
        acc3.ShippingStreet='Wayne';
        acc3.ShippingState='Texas';
        acc3.ShippingPostalCode='75428';
        acc3.ShippingCity='commerce';
        insert acc3;
        
        //Opportunity
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opportunity';
        opp.StageName = 'Pending';
        opp.CloseDate = System.today() + 30;
        opp.AccountId = acc.id;
        opp.Distributor__c=acc3.id;
        opp.Effective_Date__c=system.today();
        opp.Date_Due_To_Sales_Rep__c=system.today();
        opp.Questionnaire__c =true;
        insert opp;
        
        //Milestone
        Milestone__c mstn = new Milestone__c();
        mstn.Opportunity_Related__c = opp.Id;
        mstn.Name = opp.Name;
        mstn.Milestone_Date__c = System.today() + 30;
        mstn.Milestone_Type__c = 'Due Date';
        insert mstn;
        
    }

}

​​​​​​​
I am trying to get a datatable to reload after it saves. All of my code is working correctly and the I have added console.logs to my reloadDataTable function so I know it is going through the code. However, it is not refresshing the component.
Here is my screen: 
User-added image

Here is my code:
Class
public class RFPDateController{

    @AuraEnabled
    public static List <Work_Date__c> fetchDates(String oppId) {
        //Qyery 10 
        List<Work_Date__c> dateList = [SELECT ID, Name, Date_Type__c, Work_Date__c, Opportunity_Related__c from Work_Date__c where Opportunity_Related__c != null and Opportunity_Related__c =: oppId];
        //return lis
        return dateList;
    }
    
        @AuraEnabled
    public static boolean updateDates(List<Work_Date__c> editedDatesList){
        try{
            update editedDatesList;
            return true;
        } catch(Exception e){
            return false;
        }
    }
}
Component
<aura:component controller="RFPDateController" access="global" implements="force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId">

    <aura:attribute type="Work_Date__c[]" name="dateList"/>
    <aura:attribute name="mycolumns" type="List"/>
    <aura:attribute name="updatedRecord" type="Object[]" />
     
    <aura:handler name="init" value="{!this}" action="{!c.fetchDTS}"/>
     
    <lightning:datatable aura:id="datesDataTable"
                         data="{! v.dateList }"
                         columns="{! v.mycolumns }"
                         keyField="Id"
                         hideCheckboxColumn="true"
                         onsave ="{!c.onSave}"
                         />
     
</aura:component>

Controller
({
    fetchDTS : function(component, event, helper) {
        helper.fetchDTSHelper(component, event, helper);    

    },
     /*
     * This function is calling saveDataTable helper function
     * to save modified records
     * */
    onSave : function (component, event, helper) {
        helper.saveDataTable(component, event, helper);
    }
})

Helper: This has my save function and the reload code. 
({
    fetchDTSHelper : function(component, event, helper) {
        component.set('v.mycolumns', [
            {label: 'Date Type', fieldName: 'Date_Type__c', editable:'true', sortable:'true', type: 'text'},
            {label: 'Date Name', fieldName: 'Name', editable:'true', sortable:'true', type: 'text'},            
            {label: 'Complete Date', fieldName: 'Work_Date__c', editable:'true', sortable:'true', type: 'date'}   
            ]);
        var action = component.get("c.fetchDates");
        console.log(component.get("v.dateList"));
        var operID = component.get("v.recordId");
        console.log("operID" + operID);
        action.setParams({
            oppId:operID
        });
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.dateList", response.getReturnValue());
            }
        });
        $A.enqueueAction(action);
    },
        /*
     * This function get called when user clicks on Save button
     * user can get all modified records
     * and pass them back to server side controller
     * */
    saveDataTable : function(component, event, helper) {
        var editedRecords =  component.find("datesDataTable").get("v.draftValues");      
        var totalRecordEdited = editedRecords.length;
        var action = component.get("c.updateDates");
        action.setParams({
            'editedDatesList' : editedRecords
        });     
        action.setCallback(this,function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                //if update is successful
                if(response.getReturnValue() === true){
                    helper.showToast({
                        "title": "Record Update",
                        "type": "success",
                        "message": totalRecordEdited+" Account Records Updated"
                    });
                    helper.reloadDataTable();

                } else{ //if update failed
                    helper.showToast({
                        "title": "Error!!",
                        "type": "error",
                        "message": "Error in update"
                    });
                }
            }
        });
        $A.enqueueAction(action);
    },
    /*
     * Show toast with provided params
     * */
    showToast : function(params){
        var toastEvent = $A.get("e.force:showToast");
        if(toastEvent){
            toastEvent.setParams(params);
            toastEvent.fire();
        } else{
            alert(params.message);
        }
    },

    /*
     * reload data table
     * */
    reloadDataTable : function(){
    var refreshEvent = $A.get("e.force:refreshView");
        console.log("reload1");
        if(refreshEvent){
            console.log("reload2");
            refreshEvent.fire();
            console.log("reload3");
        }
    },
})

Credit where due: I'm using code I foudn here: https://sfdcfacts.com/lightning/editable-lightningdatatable-summer18-feature/
 
Hello Devs,

I have a component that is going to be using a <lightning:datatable /> object I am attempting to set the cols with:
var cols = [
    {label: 'Vendor', fieldName: 'a02_Vendor__r.Name', type: 'text'},
    {label: 'Type', fieldName: 'RecordType.Name', type: 'text'},
    {label: 'Created', fieldName: 'CreatedDate', type: 'Date'}
];
component.set("v.tableCols", cols);
Where the a02_Vendor__r.Name is a related Account Name and the RecordType is the RecordType of the Object being returned by my controller.

When I console.log(response.getReturnValue()) from my call back function I do see that I am getting the apropreate and expected resluts from my controller, however my lighting datatabel IS displaying the CreatedDate field, but the two realted fields are empty. Can we not use related or nested objects in the datatable component? (that would be silly)...

Any help would be much apreciated!