• Rupesh B
  • NEWBIE
  • 15 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 4
    Replies
Hi,
I am trying to display Account, Contact & Opportunity using lightning component.
I am having 2 component one is searching and other is display. Stuck with displaying the records on the component with different tables for account,contact, and opportunity

public class CustomSearchController {
    @AuraEnabled
    public static getdata searchForIds(String searchText) {
        getdata returndata = new getdata();
        List<List<SObject>> results = [FIND :searchText IN ALL FIELDS  RETURNING Account(Id,Name),Contact(Id,Name), Opportunity(Id,Name)];
        Account[] accList = (Account[])results[0];  
        Contact[] conList = (Contact[])results[1];  
        Opportunity[] oppList = (Opportunity[])results[2];
        returndata.acc = accList;
        returndata.con = conList;
        returndata.opp = oppList;
        return returndata;
    }
    Public class getdata{  
        @AuraEnabled Public List<Account> acc {get;set;}  
        @AuraEnabled Public List<Contact> con {get;set;}  
        @AuraEnabled Public List<Opportunity> Opp {get;set;}  
        Public getdata(){}    
    }    
}
----------------------------------------------------------------------------
SearchComponent
<aura:component implements="forceCommunity:searchInterface" >
    <aura:attribute name="searchText" type="String" default=""/>
    <aura:attribute name="disp" type="List"/>  
    <aura:registerEvent name="appEvent" type="c:appEvent"/>
    
    <lightning:layoutItem size="3" padding="around-small">    
        <lightning:input value="{!v.searchText}" variant="brand"  placeholder="Search" />
    </lightning:layoutItem>
    <lightning:layoutItem size="3" padding="around-small">
        <lightning:button iconName="utility:search" variant="bare" label="Search" onclick="{! c.handleClick }" />
    </lightning:layoutItem>
    
</aura:component>
--------------------------------------------------------------------
SearchComponentController

({
    handleClick : function(component, event, helper) {
        var searchbox = component.find("searchText").get("v.value");
        var appEvent = $A.get("e.c:appEvent");
        appEvent.setParams({"message": searchbox});
        appEvent.fire();
        
    }
});
----------------------------------------------------------------------------
appEvent

<aura:event type="APPLICATION" description="Event template" >
    <aura:attribute name="message" type="Object"/>
    
</aura:event>
------------------------------------------------------------------------------
DisplayComp

<aura:component controller="CustomSearchController" implements="force:hasRecordId" access="global">
    
    <aura:attribute name="disp" type="List" />
    <aura:attribute name="searchText"  type="String" />
    <aura:handler event="c:appEvent" action="{!c.handleEvent}"/>
    <aura:attribute name="accList" type="Account[]" />
    <aura:attribute name="conList" type="Contact[]" />
    <aura:attribute name="OppList" type="Opportunity[]" />
    <c:SearchComponent /><br/> 
    <H1>Hello Test</H1>
    <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" title="Id">Id</div>
                </th>
                <th scope="col">
                    <div class="slds-truncate" title="Name">Name</div>
                </th>
                
                
            </tr>
        </thead>
        <tbody>
            <aura:iteration items="{!v.disp.accList}" var="dis" >
                <tr>            
                    <td scope="row" data-label=" Id" >
                        <div class="slds-truncate" >{#dis.Id}</div>
                    </td>
                    <td data-label="Name">
                        <div class="slds-truncate" >{#dis.Name}</div>
                    </td>
                </tr>
            </aura:iteration>
        </tbody>
    </table>
    
</aura:component>
----------------------------------------------------------------------------
DisplayCompController

<aura:component controller="CustomSearchController" implements="force:hasRecordId" access="global">
    
    <aura:attribute name="disp" type="List" />
    <aura:attribute name="searchText"  type="String" />
    <aura:handler event="c:appEvent" action="{!c.handleEvent}"/>
    <aura:attribute name="accList" type="Account[]" />
    <aura:attribute name="conList" type="Contact[]" />
    <aura:attribute name="OppList" type="Opportunity[]" />
    <c:SearchComponent /><br/> 
    <H1>Hello Test</H1>
    <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" title="Id">Id</div>
                </th>
                <th scope="col">
                    <div class="slds-truncate" title="Name">Name</div>
                </th>
                
                
            </tr>
        </thead>
        <tbody>
            <aura:iteration items="{!v.disp.accList}" var="dis" >
                <tr>            
                    <td scope="row" data-label=" Id" >
                        <div class="slds-truncate" >{#dis.Id}</div>
                    </td>
                    <td data-label="Name">
                        <div class="slds-truncate" >{#dis.Name}</div>
                    </td>
                </tr>
            </aura:iteration>
        </tbody>
    </table>
    
</aura:component>
I am having some errors in the below code for the trigger to update contact address with Account Address using apex controller

public class ContactUpdateAccountAddress {
    public static void updatecontacts(List<Contact> contactlist){
        updateAddressToContacts(contactlist);
        
    }
    public static void updateAddressToContacts(List<Contact> contactlist){
        set<id> accIds = new set<id>();
        list<account> updAccaddr = new list<account>();
        for(contact con : contactlist){
            accIds.add(con.accountid);
        }
        system.debug('###'+accIds);
        list<account> acclist = [select id,BillingStreet,BillingCity,BillingState,BillingPostalCode,BillingCountry,(select id,MailingStreet,MailingCity, MailingState,MailingPostalCode,MailingCountry from contacts) from account where id in : accIds];
        for(Account acc : updAccaddr) {
        for(Contact con : contactlist){
           con.MailingStreet =  acc.BillingStreet ;
            contactlist.add(con);
        }
            }
        update contactlist;
    }

trigger ContactUpdateAccountAddressTrigger on Contact (before insert, after update, after delete) {
    if(trigger.isInsert && trigger.isbefore){
        ContactUpdateAccountAddress.updatecontacts(trigger.new);
    }
I want to create a validation rule on Phone field where Phone field can only accept numbers, brackets and dashes with numbers may or may not start with '+'. And '+' should always be leading character when it comes in the number.

I have written below validation rule but its still allowing more than one '+' character in the numbers and its not allowed. It should only be the leading character if at all its required in the number. 

Someone kindly assist with this asap.

Find below my validation rule:

NOT(OR(REGEX(  Phone  , "[[+][0-9]\\(\\+\\)-]*"),
REGEX(  Phone  , "[[0-9]\\(\\+\\)-]*")))


Also I want to bypass this for System Admin Profile.
Hi,
I am trying to display Account, Contact & Opportunity using lightning component.
I am having 2 component one is searching and other is display. Stuck with displaying the records on the component with different tables for account,contact, and opportunity

public class CustomSearchController {
    @AuraEnabled
    public static getdata searchForIds(String searchText) {
        getdata returndata = new getdata();
        List<List<SObject>> results = [FIND :searchText IN ALL FIELDS  RETURNING Account(Id,Name),Contact(Id,Name), Opportunity(Id,Name)];
        Account[] accList = (Account[])results[0];  
        Contact[] conList = (Contact[])results[1];  
        Opportunity[] oppList = (Opportunity[])results[2];
        returndata.acc = accList;
        returndata.con = conList;
        returndata.opp = oppList;
        return returndata;
    }
    Public class getdata{  
        @AuraEnabled Public List<Account> acc {get;set;}  
        @AuraEnabled Public List<Contact> con {get;set;}  
        @AuraEnabled Public List<Opportunity> Opp {get;set;}  
        Public getdata(){}    
    }    
}
----------------------------------------------------------------------------
SearchComponent
<aura:component implements="forceCommunity:searchInterface" >
    <aura:attribute name="searchText" type="String" default=""/>
    <aura:attribute name="disp" type="List"/>  
    <aura:registerEvent name="appEvent" type="c:appEvent"/>
    
    <lightning:layoutItem size="3" padding="around-small">    
        <lightning:input value="{!v.searchText}" variant="brand"  placeholder="Search" />
    </lightning:layoutItem>
    <lightning:layoutItem size="3" padding="around-small">
        <lightning:button iconName="utility:search" variant="bare" label="Search" onclick="{! c.handleClick }" />
    </lightning:layoutItem>
    
</aura:component>
--------------------------------------------------------------------
SearchComponentController

({
    handleClick : function(component, event, helper) {
        var searchbox = component.find("searchText").get("v.value");
        var appEvent = $A.get("e.c:appEvent");
        appEvent.setParams({"message": searchbox});
        appEvent.fire();
        
    }
});
----------------------------------------------------------------------------
appEvent

<aura:event type="APPLICATION" description="Event template" >
    <aura:attribute name="message" type="Object"/>
    
</aura:event>
------------------------------------------------------------------------------
DisplayComp

<aura:component controller="CustomSearchController" implements="force:hasRecordId" access="global">
    
    <aura:attribute name="disp" type="List" />
    <aura:attribute name="searchText"  type="String" />
    <aura:handler event="c:appEvent" action="{!c.handleEvent}"/>
    <aura:attribute name="accList" type="Account[]" />
    <aura:attribute name="conList" type="Contact[]" />
    <aura:attribute name="OppList" type="Opportunity[]" />
    <c:SearchComponent /><br/> 
    <H1>Hello Test</H1>
    <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" title="Id">Id</div>
                </th>
                <th scope="col">
                    <div class="slds-truncate" title="Name">Name</div>
                </th>
                
                
            </tr>
        </thead>
        <tbody>
            <aura:iteration items="{!v.disp.accList}" var="dis" >
                <tr>            
                    <td scope="row" data-label=" Id" >
                        <div class="slds-truncate" >{#dis.Id}</div>
                    </td>
                    <td data-label="Name">
                        <div class="slds-truncate" >{#dis.Name}</div>
                    </td>
                </tr>
            </aura:iteration>
        </tbody>
    </table>
    
</aura:component>
----------------------------------------------------------------------------
DisplayCompController

<aura:component controller="CustomSearchController" implements="force:hasRecordId" access="global">
    
    <aura:attribute name="disp" type="List" />
    <aura:attribute name="searchText"  type="String" />
    <aura:handler event="c:appEvent" action="{!c.handleEvent}"/>
    <aura:attribute name="accList" type="Account[]" />
    <aura:attribute name="conList" type="Contact[]" />
    <aura:attribute name="OppList" type="Opportunity[]" />
    <c:SearchComponent /><br/> 
    <H1>Hello Test</H1>
    <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" title="Id">Id</div>
                </th>
                <th scope="col">
                    <div class="slds-truncate" title="Name">Name</div>
                </th>
                
                
            </tr>
        </thead>
        <tbody>
            <aura:iteration items="{!v.disp.accList}" var="dis" >
                <tr>            
                    <td scope="row" data-label=" Id" >
                        <div class="slds-truncate" >{#dis.Id}</div>
                    </td>
                    <td data-label="Name">
                        <div class="slds-truncate" >{#dis.Name}</div>
                    </td>
                </tr>
            </aura:iteration>
        </tbody>
    </table>
    
</aura:component>
I am having some errors in the below code for the trigger to update contact address with Account Address using apex controller

public class ContactUpdateAccountAddress {
    public static void updatecontacts(List<Contact> contactlist){
        updateAddressToContacts(contactlist);
        
    }
    public static void updateAddressToContacts(List<Contact> contactlist){
        set<id> accIds = new set<id>();
        list<account> updAccaddr = new list<account>();
        for(contact con : contactlist){
            accIds.add(con.accountid);
        }
        system.debug('###'+accIds);
        list<account> acclist = [select id,BillingStreet,BillingCity,BillingState,BillingPostalCode,BillingCountry,(select id,MailingStreet,MailingCity, MailingState,MailingPostalCode,MailingCountry from contacts) from account where id in : accIds];
        for(Account acc : updAccaddr) {
        for(Contact con : contactlist){
           con.MailingStreet =  acc.BillingStreet ;
            contactlist.add(con);
        }
            }
        update contactlist;
    }

trigger ContactUpdateAccountAddressTrigger on Contact (before insert, after update, after delete) {
    if(trigger.isInsert && trigger.isbefore){
        ContactUpdateAccountAddress.updatecontacts(trigger.new);
    }
Hi All,

I want to display number of contacts associated with an account using triggers.

for this I had created a lookup field like noofcontacts__c in account  object. and Wrote code as

trigger numberofcontacts on contact(after insert, after update, after delete) {
    Map<Id, List<Contact>> AcctContactList = new Map<Id, List<Contact>>();
    Set<Id> AcctIds = new Set<Id>();   
    List<schema.Account> AcctList = new List<schema.Account>();
    List<schema.Contact> ConList = new List<schema.Contact>();
   
    if(trigger.isInsert || trigger.isUPdate) {
        for(Contact Con : trigger.New) {
            if(String.isNotBlank(Con.AccountId)){
                AcctIds.add(Con.AccountId); 
            }  
        } 
    }
   
    if(trigger.isDelete) {
        for(Contact Con : trigger.Old) {
            AcctIds.add(Con.AccountId);    
        } 
    }          
   
    if(AcctIds.size() > 0){
        ConList = [SELECT Id, AccountId FROM Contact WHERE AccountId IN : AcctIds];
       
        for(Contact Con : ConList) {
            if(!AcctContactList.containsKey(Con.AccountId)){
                AcctContactList.put(Con.AccountId, new List<Contact>());
            }
            AcctContactList.get(Con.AccountId).add(Con);     
        }                          
      
           
        AcctList = [SELECT noofContacts__c FROM Account WHERE Id IN : AcctIds];
        for(Account Acc : AcctList) {
            List<schema.Contact> ContList = new List<schema.Contact>();
            ContList = AcctContactList.get(Acc.Id);
            Acc.Number_of_Contacts__c = ContList.size();
        }   
       
      
        update AcctList;   
    }

}
 I am   getting an error as "Variable doesnot exist:id".

Kindly support and suggest.

Thanks