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
swain 10swain 10 

How to show error on Click contact New button Conditionally?

Hi 
I have to show error message on clicking contact new button on contact page and I can create without showing error message on new contact button in Account Related List.
Means I need to create contact only from account related list not from contact.


 
RakeshRakesh (Salesforce Developers) 
Hi Swan,

You can achieve this functionality through Javascript, there are few ideas and discussions on the similar requirement. Please refer the below links to get some clues or way to get the solution. Hope this helps you!
Please mark it as solved, if this reply was helpful.

Thanks,
​Rakesh
swain 10swain 10
Thank you Rakesh,
we have to do it in lightning component
 
swain 10swain 10
In contact creation lightning component we need check whether accountId is null or not.
if it is null then we need to show error. 
swain 10swain 10
I written a code but I am not able to get parent record Id when I click contact new button in account related list.
Code:
  • <aura:component implements="force:lightningQuickAction,lightning:actionOverride,flexipage:availableForRecordHome,force:hasRecordId,force:hasSObjectName" access="global" >
    <aura:attribute name="record" type="Object" description="The record object to be displayed"/>
    <aura:attribute name="accountRecord" type="Object" description="A simplified view record object to be displayed"/>
    <aura:attribute name="sObjectName" type="String" />
        
    <aura:handler name="init" value="{!this}" action="{!c.init}" />  
        
    <force:recordData aura:id="account" layoutType="FULL" recordId="{!v.recordId}"
                      targetRecord="{!v.record}" targetFields="{!v.accountRecord}"
                         recordUpdated="{!c.recordUpdate}"
                      mode="EDIT"/>
        
      
     <div aura:id="editDialog" role="dialog" tabindex="-1" aria-labelledby="header43" class="slds-modal slds-fade-in-open">
        <div class="slds-modal__container">
            
            <aura:renderif isTrue="{!v.sObjectName == 'Account' }" >
                <div class="slds-modal__header">
                        <button class="slds-button slds-button_icon slds-modal__close slds-button_icon-inverse icolor" title="Close">
                         <lightning:buttonIcon iconName="utility:close"  alternativeText="Close" iconClass="icolor"   onclick="{!c.cancelDialog}"/>                          
                        </button>
                        <h2 class="slds-text-heading--medium">New Contact</h2>
                </div>
                <div class="slds-modal__content slds-p-around--medium slds-grid slds-wrap ">              
                    <lightning:flow aura:id="Create_New_Contact" />              
                </div>  
             </aura:renderif>  
                
           <aura:renderif isTrue="{!v.sObjectName != 'Account' }" >
                 <div class="slds-modal__header">
                    
                     <button class="slds-button slds-button_icon slds-modal__close slds-button_icon-inverse icolor" title="Close">
                         <lightning:buttonIcon iconName="utility:close"  alternativeText="Close" iconClass="icolor"   onclick="{!c.cancelDialog}"/>                          
                     </button>   
                        <h2 class="slds-text-heading--medium">Attention!</h2>
                </div>
                 
                <div class="slds-modal__content slds-p-around--medium slds-grid slds-wrap ">              
                    <p>  Contacts can only be created from an Account page. 
                         Please go to the Account page that this Contact is associated with, 
                         or create a new Account
                    </p>             
                </div>  
             </aura:renderif>     
        </div>  
       
    </div>  

         <div aura:id="overlay" class="slds-backdrop slds-backdrop--open"></div>

    </aura:component>



Js Controller:

({
      init : function (component) { 
      var flow = component.find("Create_New_Contact");     
      flow.startFlow("New_Contact_Creation");
      alert(component.get("v.recordId"));
      alert(component.get("v.sObjectName")); 
       
      
   },
     cancelDialog : function(component, helper) {
        var homeEvt = $A.get("e.force:navigateToObjectHome");
        homeEvt.setParams({
            "scope": "Contact"
        });
        homeEvt.fire();              
    },
       recordUpdate: function(component, event, helper) {
       alert(component.get("v.record"));
       alert(component.get("v.recordId"));
       alert(component.get("v.sObjectName"));
        
         if(component.get("v.recordId").substring(0,3) == '001'){
                component.set("v.sObjectName",'Account');
            }
            else{
                component.set("v.sObjectName",' ')
            }  
    } 
})