function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Atul Shendge 2Atul Shendge 2 

onclick of save button record should save in object

Hi,
I have tried inserting records and when I try to save it, A record should get created in Custom object. But when I try with below code it is not.

Any help would be appreciated.
Class:
public class appleIDFctrl {
    @auraEnabled
    public static myWrapper doInitial()
    {
        myWrapper wrp = NEW myWrapper();
        wrp.inv = NEW SymphonyIPM__Inventor__c();
        wrp.countriesOptions = getPicklistValues('SymphonyIPM__Inventor__c','SymphonyIPM__Location__c');
        wrp.citizenshipOptions = getPicklistValues('SymphonyIPM__Inventor__c','SymphonyIPM__CitizenshipPicklist__c');
        wrp.clientOptions = getPicklistValues('SymphonyIPM__Inventor__c','SymphonyIPM__EmploymentCategory__c');
        //system.debug(wrp);
        return wrp;
    }
    @auraEnabled
    public static void saveData(myWrapper myData)
    {
        system.debug(myData.inv);
        insert myData.inv;
        
    }
    public class myWrapper{
        @auraEnabled Public SymphonyIPM__Inventor__c inv{set;get;}
        @auraEnabled Public List<optionsWrapper> countriesOptions{set;get;}
        @auraEnabled Public List<optionsWrapper> citizenshipOptions{set;get;}
        @auraEnabled Public List<optionsWrapper> clientOptions{set;get;}
    }
    public class optionsWrapper{
        @auraEnabled public string value{set;get;}
        @auraEnabled public string label{set;get;}
    }
    public static list<optionsWrapper> getPicklistValues(string objectName, string fieldName)
    {
        List<optionsWrapper> options = NEW List<optionsWrapper>();
        
        Schema.SObjectType s = Schema.getGlobalDescribe().get(objectName) ;
        Schema.DescribeSObjectResult r = s.getDescribe() ;
        Map<String,Schema.SObjectField> fields = r.fields.getMap() ;
        Schema.DescribeFieldResult fieldResult = fields.get(fieldName).getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        for( Schema.PicklistEntry pickListVal : ple)
        {
            optionsWrapper newOpt = NEW optionsWrapper();
            newOpt.label = pickListVal.getLabel();
            newOpt.value = pickListVal.getValue();
            options.add(newOpt);
        }
        if(options.size()>0)
        {
            return options;            
        }
        else{
            optionsWrapper newOpt = NEW optionsWrapper();
            newOpt.label = '--No Data--';
            newOpt.value = '';
            options.add(newOpt);
            return options;
        }
    }    
}
Component:
<aura:component controller="appleIDFctrl" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction,lightning:availableForFlowScreens" access="global" >
    
    <aura:attribute name="Inventor" type="Boolean" default="false" />
    <aura:attribute name="Add" type="String" default=" " />
    <aura:attribute name="myData" type="Object"/>
    <aura:attribute name="Add1" type="Boolean" default="false"/>
    <aura:handler name="init" value="{!this}" action ="{!c.doInIt}"/>
    <h3><b>Inventors:*</b></h3>
    <br/>
    <p>Please click on the Primary Inventor Contact below to search and select a name.</p>
    <br/>
    <div> <b>IC1 </b><lightning:button class="customButton" variant="brand-outline" aura:id="prim" label="Primary Inventor Contact" onclick="{! c.handleClick }" value="{!v.Inventor}"/>
    </div>
    <!--<aura:if isTrue="{!v.Inventor}" />-->
    <br/>
    
    <div> <b>Other Potential Inventors </b><lightning:button class="customButton" variant="brand-outline" aura:id="ad" label="Add" onclick="{! c.OnAdd }" value="{!v.Add}"/>
        &nbsp;&nbsp;
        <lightning:button class="customButton" variant="brand-outline" aura:id="ade" label="Add Non-Employees" onclick="{! c.OnAdd1 }" value="{!v.Add1}"/>
    </div>
    
    <aura:if isTrue="{!v.Add1}">
        
<lightning:select name="Country" label="Country" value="{!v.myData.inv.SymphonyIPM__Location__c}">
                <aura:iteration var="opt" items="{!v.myData.countriesOptions}">
                    <option value="{!opt.value}" text="{!opt.label}"/>
                </aura:iteration>
            </lightning:select>            
                </div>
                </div>
            <br/>
            <p>Other Information</p>
            <hr/>
              <div class= "slds-grid slds-gutters">
             <div class="slds-col">
            <lightning:select name="Citizenship" label="Citizenship" value="{!v.myData.inv.SymphonyIPM__CitizenshipPicklist__c}">
                <aura:iteration var="opt" items="{!v.myData.citizenshipsOptions}">
                    <option value="{!opt.value}" text="{!opt.label}"/>
                </aura:iteration>
            </lightning:select>
</div>
                    </div>
            <br/>
            <lightning:button label="Save" onclick="{!c.save}"/>
        
        </div>
        </aura:if>
    
</aura:component>

Controller:
save : function(cmp, event, helper) {
        var allData = cmp.get("v.myData");
        alert("From server: " + JSON.stringify(allData.inv));
        var action = cmp.get("c.saveData");
        action.setParams({ myData : allData });
        action.setCallback(this, function(response) {
            alert(response.getState());
            var state = response.getState();
            if (state === "SUCCESS") {
                alert(state);
            }
            else if (state === "INCOMPLETE") {
                alert(state);
            }
                else if (state === "ERROR") {
                    alert(state);
                    var errors = response.getError();
                    if (errors) {
                        if (errors[0] && errors[0].message) {
                            console.log("Error message: " + 
                                        errors[0].message);
                        }
                    } else {
                        console.log("Unknown error");
                    }
                }
        });
    },
    
})

Helper:
({
    helperdoInIt : function(cmp, event, helper) {
        // create a one-time use instance of the serverEcho action
        // in the server-side controller
        var action = cmp.get("c.doInitial");
        //action.setParams({ firstName : cmp.get("v.firstName") });

        // Create a callback that is executed after 
        // the server-side action returns
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                // Alert the user with the value returned 
                // from the server
                //alert("From server: " + JSON.stringify(response.getReturnValue()));
                cmp.set("v.myData",response.getReturnValue());
                // You would typically fire a event here to trigger 
                // client-side notification that the server-side 
                // action is complete
            }
            else if (state === "INCOMPLETE") {
                // do something
            }
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + 
                                 errors[0].message);
                    }
                } else {
                    console.log("Unknown error");
                }
            }
        });

        // optionally set storable, abortable, background flag here

        // A client-side action could cause multiple events, 
        // which could trigger other events and 
        // other server-side action calls.
        // $A.enqueueAction adds the server-side action to the queue.
        $A.enqueueAction(action);
    }
})

Thanks
Atul Shendge
MagulanDuraipandianMagulanDuraipandian
Atul,
Put debug log to find the issue.
Debug Log will show whether it failed due to any validations.
--
Magulan Duraipandian
www.infallibletechie.com
Atul Shendge 2Atul Shendge 2
Hi Magulan,

I have two cmp--> Parent and child and I want to call child in parent cmp.
Above is the component and now I have tried in calling in controller

Please see the below code:
Controller:
OnAdd: function(component, event, helper) {
        var evt = $A.get("e.force:navigateToComponent");
        //alert(component.get("v.HomePage")+'HomePage');
        //component.set('v.HomePage',false);
        //alert(component.get("v.HomePage")+'comp1');
        evt.setParams({
            componentDef: "c:cmpAdditionalInventorsSelection",
            componentAttributes: {
                 //HomePage : component.get("v.HomePage")
                //Attributes here.
            }
        });
        evt.fire();
    },

User-added image On click  of this button, this cmp--> cmpAdditionalInventorsSelection should be called.

Thanks in Advance,
Atul Shendge