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
Thulasi ReddysThulasi Reddys 

Save action and workorder lookup issues in lightning component?

JAYABALAJI TKM 1JAYABALAJI TKM 1
Hi Thulasi Reddys,

Could you give more explanation and share your code to solve?

Thanks 
Jayabalaji
Thulasi ReddysThulasi Reddys
Record couldn't saved and workorder lookup will not come

Apex Class:
public with sharing class OpportunitySaveCtrl
{
    /**
   * Create a new Opportunity
   *
   * @param Opportunity Opportunity Opportunity record to be inserted
   * 
   */
    @AuraEnabled
   public static String createRecord(Opportunity opportunity, string sta)
    {
        try
        {
            System.debug('OpportunitySaveCtrl::createRecord::opportunity'+opportunity);
           
            if(opportunity.StageName!= null)
                insert opportunity;
        } 
        catch (Exception ex)
        {
            
        }
        return null; 
      
    } 
   
  
    
     @AuraEnabled
     public static List <String> getselectOptions(sObject objObject, string fld) 
     {
          List <String> allOpts = new list <String>();
          Schema.sObjectType objType = objObject.getSObjectType();  // Get the object type of the SObject.
          Schema.DescribeSObjectResult objDescribe = objType.getDescribe();  // Describe the SObject using its object type.
          Map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap();  // Get a map of fields for the SObject
          List <Schema.PicklistEntry> values = fieldMap.get(fld).getDescribe().getPickListValues();  // Get the list of picklist values for this field.
        
          for(Schema.PicklistEntry a: values) 
           allOpts.add(a.getValue());  // Add these values to the selectoption list.
        
          allOpts.sort();
          return allOpts;
     }
           
}


Component:
<aura:component controller="OpportunitySaveCtrl" 
                implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes" 
                access="global" >
    
    <!-- Include Static Resource-->
    <ltng:require styles="/resource/bootstrap1/css/bootstrap.min.css" 
                  scripts="/resource/bootstrap1/js/jquery.js,/resource/bootstrap/js/bootstrap.min.js"/>
    
    <!-- Define Attribute-->
    <aura:attribute name="selectedLookUpRecord" type="sObject" default="{}"/>
    <aura:attribute name="selectedLookUpRecord1" type="sObject" default="{}"/>
    <aura:attribute name="selectedLookUpRecord2" type="sObject" default="{}"/>
    <aura:attribute name="number" type="integer"/>
    <aura:attribute name="myText" type="String"/>

    <aura:attribute name="opportunity" type="Opportunity" default="{'sobjectType': 'Opportunity',
                                                                  'Name': '',
                                                                  'Account__c': '',                 <!--- Custom field-->
                                                                  'Primary_Contact__c': '',     <!--- Custom field-->
                                                                  'Work_Order__c': '',                <!--- Custom field-->
                                                                  'LeadSource': '',
                                                                  'StageName' : '',
                                                                  'CloseDate': '',
                                                                  'Priority__c' : '',
                                                                  
                                                                  
                                                               }"/><br/>
        <div>
            <table>
                <tr>
                    <td>
                        <h2 style="font-weight:bold;font-size:12px;margin-left:5%">New Opportunity Edit</h2>
                        <h1 style="font-weight:bold;font-size:18px;margin-left:5%">New Opportunity</h1>
                    </td>
                </tr>
            </table>
    </div><br/>
    <center>
        <div class="col-md-4 text-center">
              <lightning:button variant="brand" label="Save" onclick="{!c.create}"/>
        </div>
    </center><br/>
    <div class="slds-box" style="background:rgb(0, 161, 223);font-weight:bold;font-size:15pt;">
        <p>Opportunity Information</p>
    </div>
    
    <div class="slds-grid slds-gutters">
        <div class="slds-col" style="width:500px;">
            <table class="slds-table slds-table--bordered slds-table--cell-buffer"> 
              <thead>
                 <tr>
                 <td>
                  <table>
                      <tr>
                        <td><p class="title">Opportunity Name</p>
                    <ui:inputText value="{!v.opportunity.name}"/></td>
                    </tr>
                      <tr>
                        <td><c:customLookup objectAPIName="account" IconName="standard:account" label="Account Name" selectedRecord="{!v.selectedLookUpRecord}"/></td>
                    </tr>
                    
                      <tr>
                        <td><c:customLookup objectAPIName="workorder" IconName="standard:workorder" label="Work Order" selectedRecord="{!v.selectedLookUpRecord2}"/></td>
                    </tr>
                      
                    <tr>
                        <td><c:customLookup objectAPIName="contact" IconName="standard:contact" label="Primary Contact" selectedRecord="{!v.selectedLookUpRecord1}"/></td>
                    </tr>
                        <div class="form-group">
                            <tr>
                            <td>
                        <p class="title">Lead Source</p>
                        <ui:inputSelect aura:id="InputSelectSingle" change="{!c.onSingleSelectChange}"  value="{!v.opportunity.LeadSource}">
                            <ui:inputSelectOption text="Web"/>
                            <ui:inputSelectOption text="External Referral"/>
                            <ui:inputSelectOption text="Trade Show"/>
                            <ui:inputSelectOption text="Public Relations"/>
                        </ui:inputSelect>
                     </td>
                     </tr>
                    </div>      
                     
                    </table>
                    </td>
                    <td> 
                        <table>
                        <tr>
                            <td>
                        <p class="title">Stage</p>
                        <ui:inputSelect class="single" aura:id="InputSelectSingle" change="{!c.onSingleSelectChange}"  value="{!v.opportunity.StageName}">
                            <ui:inputSelectOption text="Closed Won"/>
                            <ui:inputSelectOption text="Closed Lost"/>
                            <ui:inputSelectOption text="Patriot Suggestion"/>
                            <ui:inputSelectOption text="Agree to Proceed"/>
                        </ui:inputSelect>
                     </td>
                         </tr>
                            
                              <tr>
                            <td>
                        <label>Close Date</label>
                        <ui:inputDate aura:id="mySelectedDate" displayDatePicker="true" value="{!v.opportunity.CloseDate}"/>
                               </td>
                         </tr>      
                            
                    <div class="form-group">
                        <tr>
                         <td>
                        <p class="title">Priority</p>
                        <ui:inputSelect class="single" aura:id="InputSelectSingle" change="{!c.onSingleSelectChange}"  value="{!v.opportunity.Priority__c}">
                            <ui:inputSelectOption text="Low"/>
                            <ui:inputSelectOption text="Medium"/>                 
                            <ui:inputSelectOption text="High"/>
                            <ui:inputSelectOption text="Critical"/>
                          
                        </ui:inputSelect>
                        </td>
                      </tr>
                     </div>                                        
                </table>
                </td>               
                  </tr>
                </thead>
            </table>           
        </div>
                   
      </div>
</aura:component>

Controller:
({
    create : function(component, event, helper) {
        console.log('Create record');
        
        //getting the candidate information
        var opportunity = component.get("v.opportunity");
        opportunity.Account__c = null;
        opportunity.Primary_Contact__c = null;
        opportunity.Work_Order__c = null;
        
        /***************Validation
        if($A.util.isEmpty(opportunity.Account__c) || $A.util.isUndefined(opportunity.Account__c)){
            alert('Account Name is Required');
            return;           
        }*************************************************/
        if(component.get("v.selectedLookUpRecord").Id != undefined){
            opportunity.Account__c = component.get("v.selectedLookUpRecord").Id;
        }       
        if(component.get("v.selectedLookUpRecord2").Id != undefined){
            opportunity.Work_Order__c = component.get("v.selectedLookUpRecord2").Id;
        }
        if(component.get("v.selectedLookUpRecord1").Id != undefined){
            opportunity.Primary_Contact__c = component.get("v.selectedLookUpRecord1").Id;
        }
        
        //Calling the Apex Function
        var action = component.get("c.createRecord");
        
        //Setting the Apex Parameter
        action.setParams({
            opportunity : opportunity
        });
        
        //Setting the Callback
        action.setCallback(this,function(a){
            //get the response state
            var state = a.getState();
            
            //check if result is successfull
            if(state == "SUCCESS"){
                //Reset Form
                var newOpportunity = {'sobjectType': 'Opportunity',
                                    'Name': '', 
                                    'Account__c': '',
                                    'Primary_Contact__c': '',
                                    'Work_Order__c': '',
                                    'LeadSource': '',
                                    'StageName' : '',
                                    'CloseDate': '',
                                    'Priority__c' : '',
                                   
                                    
                                   };
                //resetting the Values in the form
                component.set("v.opportunity",newOpportunity);
                alert('Record is Created Successfully');
            } else if(state == "ERROR"){
                alert('Error in calling server side action');
            }
        });
        
        //adds the server-side action to the queue        
        $A.enqueueAction(action);
        
    },
    
    onSingleSelectChange: function(cmp) {
         var selectCmp = cmp.find("InputSelectSingle");
         var resultCmp = cmp.find("singleResult");
         resultCmp.set("v.value", selectCmp.get("v.value"));
     },
     onChange: function(cmp) {
         var dynamicCmp = cmp.find("InputSelectDynamic");
         var resultCmp = cmp.find("dynamicResult");
         resultCmp.set("v.value", dynamicCmp.get("v.value"));
     }
})