1

I'm adjusting this, so that it's creating multiple contacts associated with the Account. I can't figure out two things.

  1. How to pre-populate the Contact.LastName that is associated with Account.npe01__One2OneContact__c. Is there a way to do that in the Helper.js?
    --Update: 7/19/2021 I was able to use SOQL in the dev console, but am still not sure where to place it. The SOQL is: SELECT LastName FROM Contact WHERE Id IN (SELECT npe01__One2OneContact__c FROM Account)
  2. How to have a picklist on some of lightning:input fields that pulls from the picklist. None of these options in the documentation fit.

Class = addRemoveContactsDynamically

public class addRemoveContactsDynamically {
    @AuraEnabled
    public static void saveContacts(List<Contact> contList){
        Insert contList;
    }    
}

Lightning Component = addRemoveContactsDynamically

<aura:component controller="addRemoveContactsDynamically" Implements="flexipage:availableForRecordHome,force:hasRecordId">
    
    <aura:attribute name="contactList" type="Contact[]"/> 
    
    <div class="slds-m-around--xx-large">
        <div class="slds-text-heading_medium">Create Contact Records</div>
        <div class="slds-float_right slds-p-bottom_small">
            <h1 class="slds-page-header__title">Add Row 
                <lightning:buttonIcon iconName="utility:add"  size="large" variant="brand" alternativeText="Add" onclick="{!c.addRow}"/>
            </h1>
        </div>
        <div class="container-fluid">        
            <table class="slds-table slds-table_bordered slds-table_cell-buffer"> 
                <thead>
                    <tr class="slds-text-title_caps">
                        <th scope="col">
                            <div class="slds-truncate">#</div>
                        </th>
                        <th scope="col">
                            <div class="slds-truncate" title="Species">Species</div>
                        </th>
                        <th scope="col">
                            <div class="slds-truncate" title="First Name">First Name</div>
                        </th>
                        <th scope="col">
                            <div class="slds-truncate" title="Status">Status</div>
                        </th>
                        <th scope="col">
                            <div class="slds-truncate" title="Sex">Sex</div>
                        </th>
                        <th scope="col">
                            <div class="slds-truncate" title="Weight">Weight</div>
                        </th>
                        <th scope="col">
                            <div class="slds-truncate" title="Age">Age</div>
                        </th>
                        <th scope="col">
                            <div class="slds-truncate" title="Description">Description</div>
                        </th>
                    </tr>
                </thead>   
                <tbody>      
                    <aura:iteration items="{!v.contactList}" var="cont" indexVar="index">
                        <tr>
                            <td> 
                                {!index + 1}
                            </td>
                            <td>
                                <lightning:input name="contSpecies" type="text" required="false" maxlength="20" label="Species" value="{!cont.fido_Species__c}" />
                            </td>
                            <td>
                                <lightning:input name="contFirstName" type="text" required="false" maxlength="50" label="First Name" value="{!cont.FirstName}" />
                            </td>
                            <td>
                                <lightning:input name="contStatus" type="text" required="true" maxlength="50" label="Status" value="{!cont.fido_spay_neuter_Status__c}" />
                            </td>
                            <td>
                                <lightning:input name="contSex" type="text" required="true" maxlength="50" label="Sex" value="{!cont.Gender__c}" />
                            </td>
                            <td>
                                <lightning:input name="contWeight" type="text" required="false" maxlength="50" label="Weight" value="{!cont.fido_Weight__c}" />
                            </td>
                            <td>
                                <lightning:input name="contAge" type="date" required="true" maxlength="50" label="Age" value="{!cont.Birthdate}" />
                            </td>
                            <td>
                                <lightning:input name="contDescripton" type="text" required="false" maxlength="2000" label="Description" value="{!cont.Description}" />
                            </td>
                            <td>
                                <a onclick="{!c.removeRow}" data-record="{!index}">
                                    <lightning:icon iconName="utility:delete" size="small" alternativeText="Delete"/>
                                    <span class="slds-assistive-text">Delete</span>
                                </a>
                            </td> 
                        </tr>
                    </aura:iteration>
                </tbody>
            </table>
            <div class="slds-align_absolute-center slds-p-top_small">
                <lightning:button variant="brand" label="Submit" title="Brand action" onclick="{!c.save}" />
            </div>
        </div>
    </div>
</aura:component>

Controller = addRemoveContactsDynamicallyController.js

({
    addRow: function(component, event, helper) {
        helper.addContactRecord(component, event);
    },
    
    removeRow: function(component, event, helper) {
        //Get the contact list
        var contactList = component.get("v.contactList");
        //Get the target object
        var selectedItem = event.currentTarget;
        //Get the selected item index
        var index = selectedItem.dataset.record;
        contactList.splice(index, 1);
        component.set("v.contactList", contactList);
    },
    
    save: function(component, event, helper) {
        if (helper.validateContactList(component, event)) {
            helper.saveContactList(component, event);
        }
    },
})

Helper = addRemoveContactsDynamicallyHelper.js

({
    addContactRecord: function(component, event) {
        //get the contact List from component  
        var contactList = component.get("v.contactList");
        var accountId = component.get("v.recordId");
        //Add New Contact Record
        contactList.push({
            'sobjectType': 'Contact',
            'AccountId': accountId,
            'RecordTypeId':'0124P000000uNWFQA2',
            'fido_Species__c': '',
            'FirstName': '',
            'LastName': '',
            'fido_spay_neuter_Status__c': '',
            'Gender__c': '',
            'fido_Weight__c': '',
            'Birthdate': '',
            'Description': ''
        });
        component.set("v.contactList", contactList);
    },
    
    validateContactList: function(component, event) {
        //Validate all contact records
        var isValid = true;
        var contactList = component.get("v.contactList");
        for (var i = 0; i < contactList.length; i++) {
            if (contactList[i].LastName == '') {
                isValid = false;
                alert('Last Name cannot be blank on row number ' + (i + 1));
            }
        }
        return isValid;
    },
    
    saveContactList: function(component, event, helper) {
        //Call Apex class and pass contact list parameters
        var action = component.get("c.saveContacts");
        action.setParams({
            "contList": component.get("v.contactList")
        });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.contactList", []);
                alert('Contact records saved successfully');
            }
        }); 
        $A.enqueueAction(action);
    },
})

enter image description here enter image description here

0

Browse other questions tagged or ask your own question.