+ Start a Discussion
akkkakkk 

I am getting Error when are click on the Next button for the Popup used the Contact Object

Hi All,
Please support i am getting the Recrod type of the Contact Object its find after that when am i clicking on the Next Button for Creating a Record facing the Error The Erro User-added image
the Component => <aura:component controller="recordtypeContact" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global">
    
    <aura:handler name="init" value="{!this}" action="{!c.fetchListOfRecordTypes}"/>
    
    <aura:attribute name="lstOfRecordType" type="String[]" />
    <aura:attribute name="isOpen" type="boolean" default="false" />
 
  <div class="slds-m-around--x-large">
    <lightning:button label="Create a contact" onclick="{!c.openModal}" />
  </div>    
   <!-- Model Box Start -->    
    <aura:if isTrue="{!v.isOpen}">
        <div role="dialog" tabindex="-1" aria-labelledby="header43" class="slds-modal slds-fade-in-open">
            <div class="slds-modal__container">
                <div class="slds-modal__header">
                    <button class="slds-button slds-modal__close slds-button--icon-inverse" title="Close" onclick="{!c.closeModal}">
                        X<span class="slds-assistive-text">Cancel</span>
                    </button>
                    <h2 id="header43" class="slds-text-heading--medium">New Contact</h2>
                </div>
                
                <div class="slds-modal__content slds-p-around--medium">
                    <div class="slds-grid slds-wrap">
                        <div class="slds-size--1-of-2 slds-large-size--1-of-2">
                             <div class="slds-align--absolute-center">Select a Record Type</div>                            
                        </div>
                        <div class="slds-size--1-of-2 slds-large-size--1-of-2">
                            <ui:inputSelect aura:id="selectid">
                                <aura:iteration items="{!v.lstOfRecordType}" var="contact">                            
                                    <ui:inputSelectOption text="{!contact}" label="{!contact}"  />
                                </aura:iteration>
                            </ui:inputSelect>
                        </div>&nbsp; &nbsp;
                    </div>                   
                </div>
                
                <div class="slds-modal__footer">
                    <lightning:button class="slds-button slds-button--neutral" onclick="{!c.closeModal}">Cancel</lightning:button>
                    <lightning:button class="slds-button slds-button--brand" onclick="{!c.createRecord}">Next</lightning:button>
                </div>
            </div>
        </div>
        <div class="slds-backdrop slds-backdrop--open"></div>
    </aura:if>
</aura:component>
 controller part=>
({
 
   
   fetchListOfRecordTypes: function(component, event, helper) {
      var action = component.get("c.fetchRecordTypeValues");
      action.setCallback(this, function(response) {
         component.set("v.lstOfRecordType", response.getReturnValue());
      });
      $A.enqueueAction(action);
   },
 
   
   createRecord: function(component, event, helper) {
      component.set("v.isOpen", true);
 
      var action = component.get("c.getRecTypeId");
      var recordTypeLabel = component.find("selectid").get("v.value");
      action.setParams({
         "recordTypeLabel": recordTypeLabel
      });
      action.setCallback(this, function(response) {
         var state = response.getState();
         if (state === "SUCCESS") {
            var createRecordEvent = $A.get("e.force:createRecord");
            var RecTypeID  = response.getReturnValue();
            createRecordEvent.setParams({
               "entityApiName": 'Contact',
               "recordTypeId": RecTypeID
            });
            createRecordEvent.fire();
             
         } else if (state == "INCOMPLETE") {
            var toastEvent = $A.get("e.force:showToast");
            toastEvent.setParams({
               "title": "Oops!",
               "message": "No Internet Connection"
            });
            toastEvent.fire();
             
         } else if (state == "ERROR") {
            var toastEvent = $A.get("e.force:showToast");
            toastEvent.setParams({
               "title": "Error!",
               "message": "Please contact your administrator"
            });
            toastEvent.fire();
         }
      });
      $A.enqueueAction(action);
   },
 
   closeModal: function(component, event, helper) {
      // set "isOpen" attribute to false for hide/close model box
      component.set("v.isOpen", false);
   },
 
   openModal: function(component, event, helper) {
      // set "isOpen" attribute to true to show model box
      component.set("v.isOpen", true);
   },
})
 apex code =>
public class recordtypeContact {
    
    public static Map<Id, String> recordtypemap {get;set;}
    
   @AuraEnabled        
    public static List<String> fetchRecordTypeValues(){
        List<Schema.RecordTypeInfo> recordtypes = Contact.SObjectType.getDescribe().getRecordTypeInfos();    
        recordtypemap = new Map<Id, String>();
        for(RecordTypeInfo rt : recordtypes){
            if(rt.getName() != 'Master')
            recordtypemap.put(rt.getRecordTypeId(), rt.getName());
        }        
        return recordtypemap.values();
    }
    
    @AuraEnabled
    public static Id getRecTypeId(String recordTypeLabel){
        Id recid = Schema.SObjectType.Contact.getRecordTypeInfosByName().get(recordTypeLabel).getRecordTypeId();        
        return recid;
    }      

}


Thanks
Akhan25
Danish HodaDanish Hoda
Hi there,
For firing an event, you need to register the same in your component as below:

<aura:registerEvent name="createRecord" type="c:createRecord"/>

Plz refer (https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/events_component_example.htm) for more details.
 
akkkakkk
Hi Danlsh Hoda

Acutally i used all thing but i am not able to slove the problem if you can test this code and reply me .
Danish HodaDanish Hoda
I would need access to your org, if possible please ping me on Skype <danish.hoda>
Danish HodaDanish Hoda
Hi Aklim,
Plz refer below code for Task creation.
Just to mention that, toast doesn't work in lightning Apps, so try using this after creating a button/quick Action on Task object.
cmp:
<aura:component controller="<ClassName>" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global">
    
    <aura:handler name="init" value="{!this}" action="{!c.fetchListOfRecordTypes}"/>
    
    <aura:attribute name="lstOfRecordType" type="String[]" />
    <aura:attribute name="isOpen" type="boolean" default="false" />
    <aura:attribute name="taskRec" type="Task" default="{'sobjectType' : 'Task'}" /> 
    <aura:attribute name="nxtPage" type="Boolean" default="false" />
    <aura:attribute name="recTypeId" type="String" />
    
    <div class="slds-m-around--x-large">
        <lightning:button label="Create a Task" onclick="{!c.openModal}" />
    </div>    
    <!-- Model Box Start -->    
    <aura:if isTrue="{!v.isOpen}">
        <div role="dialog" tabindex="-1" aria-labelledby="header43" class="slds-modal slds-fade-in-open">
            <div class="slds-modal__container">
                <div class="slds-modal__header">
                    <button class="slds-button slds-modal__close slds-button--icon-inverse" title="Close" onclick="{!c.closeModal}">
                        X<span class="slds-assistive-text">Cancel</span>
                    </button>
                    <h2 id="header43" class="slds-text-heading--medium">New Task</h2>
                </div>
                
                <div class="slds-modal__content slds-p-around--medium">
                    <div class="slds-grid slds-wrap">
                        <aura:if isTrue="{!v.nxtPage}" >
                            <lightning:input name="Subject" value="{!v.taskRec.Subject}" type="text" label = "Subject" 
                                             required="true"/> &nbsp;&nbsp;&nbsp;
                            <lightning:input name="Due Date" value="{!v.taskRec.ActivityDate}" type="Date" label = "Due Date" 
                                             required="true"/> &nbsp;&nbsp;&nbsp;
                            <lightning:select name="Priority" aura:id ="pr" label="Priority" required="true" value="{!v.taskRec.Priority}">
                                <option value="">---None---</option>
                                <option value="Normal">Normal</option>
                                <option value="High">High</option>
                                <option value="Low">Low</option>
                            </lightning:select>
                            <aura:set attribute="else">
                                <div class="slds-size--1-of-2 slds-large-size--1-of-2">
                                    <div class="slds-align--absolute-center">Select a Record Type</div>                            
                                </div>
                                <div class="slds-size--1-of-2 slds-large-size--1-of-2">
                                    <ui:inputSelect aura:id="selectid">
                                        <aura:iteration items="{!v.lstOfRecordType}" var="contact">                            
                                            <ui:inputSelectOption text="{!contact}" label="{!contact}"  />
                                        </aura:iteration>
                                    </ui:inputSelect>
                                </div>&nbsp; &nbsp;
                            </aura:set>
                        </aura:if>
                    </div>                   
                </div>
                
                <div class="slds-modal__footer">
                    <lightning:button class="slds-button slds-button--neutral" onclick="{!c.closeModal}">Cancel</lightning:button>
                    <aura:if isTrue="{!v.nxtPage}" >
                        <lightning:button class="slds-button slds-button--brand" onclick="{!c.createTask}">Submit</lightning:button>
                        <aura:set attribute="else">
                            <lightning:button class="slds-button slds-button--brand" onclick="{!c.createRecord}">Next</lightning:button>
                        </aura:set>
                    </aura:if>
                </div>
            </div>
        </div>
        <div class="slds-backdrop slds-backdrop--open"></div>
    </aura:if>
</aura:component>

-------------
controller.js
-------------
({
    fetchListOfRecordTypes: function(component, event, helper) {
        var action = component.get("c.fetchRecordTypeValues");
        action.setCallback(this, function(response) {
            component.set("v.lstOfRecordType", response.getReturnValue());
        });
        $A.enqueueAction(action);
    },
    
    
    createRecord: function(component, event, helper) {
        component.set("v.isOpen", true);
        
        var action = component.get("c.getRecTypeId");
        var recordTypeLabel = component.find("selectid").get("v.value");
        action.setParams({
            "recordTypeLabel": recordTypeLabel
        });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.nxtPage", true);
                component.set("v.recTypeId", response.getReturnValue());
            } else if (state == "INCOMPLETE") {
                var toastEvent = $A.get("e.force:showToast");
                toastEvent.setParams({
                    "title": "Oops!",
                    "message": "No Internet Connection"
                });
                toastEvent.fire();
                
            } else if (state == "ERROR") {
                var toastEvent = $A.get("e.force:showToast");
                toastEvent.setParams({
                    "title": "Error!",
                    "message": "Please contact your administrator"
                });
                toastEvent.fire();
            }
        });
        $A.enqueueAction(action);
    },
    
    closeModal: function(component, event, helper) {
        // set "isOpen" attribute to false for hide/close model box
        component.set("v.isOpen", false);
    },
    
    openModal: function(component, event, helper) {
        // set "isOpen" attribute to true to show model box
        component.set("v.isOpen", true);
    },
    
    createTask: function(component, event, helper) {
        let recordTypeId = component.get("v.recTypeId");
        let taskRec = JSON.stringify(component.get("v.taskRec"));
        var action = component.get("c.createTaskCtlr");
        action.setParams({
            "recordTypeId": recordTypeId,
            "taskRec" : taskRec
        });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var toastEvent = $A.get("e.force:showToast");
                toastEvent.setParams({
                    "title": "Success!",
                    "message": "Task created with Id : "+response.getReturnValue()
                });
                toastEvent.fire();
                component.set("v.isOpen", false);
            } else if (state == "INCOMPLETE") {
                var toastEvent = $A.get("e.force:showToast");
                toastEvent.setParams({
                    "title": "Oops!",
                    "message": "No Internet Connection"
                });
                toastEvent.fire();
            } else if (state == "ERROR") {
                var toastEvent = $A.get("e.force:showToast");
                toastEvent.setParams({
                    "title": "Error!",
                    "message": "Please contact your administrator"
                });
                toastEvent.fire();
            }
        });
        $A.enqueueAction(action);
    },
})

-----
Apex
-----
public class <ClassName> {

    public static Map<Id, String> recordtypemap {get;set;}
    
   @AuraEnabled        
    public static List<String> fetchRecordTypeValues(){
        List<Schema.RecordTypeInfo> recordtypes = Task.SObjectType.getDescribe().getRecordTypeInfos();    
        recordtypemap = new Map<Id, String>();
        for(RecordTypeInfo rt : recordtypes){
            if(rt.getName() != 'Master')
            recordtypemap.put(rt.getRecordTypeId(), rt.getName());
        }        
        return recordtypemap.values();
    }
    
    @AuraEnabled
    public static Id getRecTypeId(String recordTypeLabel){
        Id recid = Schema.SObjectType.Task.getRecordTypeInfosByName().get(recordTypeLabel).getRecordTypeId();        
        return recid;
    }
    
    @AuraEnabled
    public static String createTaskCtlr(String recordTypeId, String taskRec){
        Task taskObj = (Task) JSON.deserialize(taskRec, Task.class);
        taskObj.RecordTypeId = recordTypeId;
        taskObj.OwnerId = UserInfo.getUserId();
        //add other fields like WhoId WhatId etc
        INSERT taskObj;
        return taskObj.Id;
    }
}