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
Harsha vardhan 135Harsha vardhan 135 

1) create a field on Account named "number of contacts" . populate this filed with the number of contacts related to an account.

2) Build a basic lighting component that can query a list of 10 most recently created accounts and display it using in lightning app.
3) Make a basic http call out and print result in system.debug 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Harsha,

I trust you are doing very well.

1. Create a field on Account named "Number Of Contacts". Populate this field with the number of contacts related to an account.
 
trigger CountConOnAcc on Contact (after insert, after update, after delete) {
    
    Set <Id> accountIds = new Set <Id>();
    List <Account> lstAccountsToUpdate = new List <Account>();
    
    if(Trigger.isInsert){
        for(Contact con:trigger.new){
            accountIds.add(con.accountID);
        }
    }
    
    if(Trigger.isUpdate|| Trigger.isDelete){
        for(Contact con:trigger.old){
            accountIds.add(con.accountID);
        }
    }
    
    for(Account acc:[SELECT Id,Name,Number_Of_Contacts__c,(Select Id from Contacts) from Account where Id IN: accountIds]){
        Account accObj = new Account ();
        accObj.Id = acc.Id;
        accObj.Number_Of_Contacts__c = acc.Contacts.size();
        lstAccountsToUpdate.add(accObj);
    }
    
    UPDATE lstAccountsToUpdate;
}


2) Build a basic lighting component that can query a list of 10 most recently created accounts and display it using in a lightning app.

Application:
<aura:application extends="force:slds">
    <c:RecentAcc/>
</aura:application>

Component:
<aura:component controller="RecentAcc"
                implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    
    <aura:attribute name="mydata" type="Object"/>
    <aura:attribute name="mycolumns" type="List"/>
    
    
    <aura:handler name="init" value="{!this}" action="{!c.init}" />
    
    <lightning:datatable data="{! v.mydata }" 
                         columns="{! v.mycolumns }" 
                         keyField="Id" />
</aura:component>

Controller:
({
    init : function(component, event, helper) {
        component.set('v.mycolumns', [         
            {label: 'Account Name', fieldName: 'Name', type: 'text'},
            {label: 'Phone', fieldName: 'Phone', type: 'phone'},
            {label: 'Rating', fieldName: 'Rating', type: 'text'}
        ]);       
        
        var action = component.get("c.search");
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                console.log(response.getReturnValue());
                component.set("v.mydata", response.getReturnValue());
            }
            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");
                }
            }
        });
        $A.enqueueAction(action);
    },
})

Apex:
public class RecentAcc {
    
    @AuraEnabled
    public static List<Account> search(){
        List<Account> sr = [SELECT Name, Phone, Rating FROM Account ORDER BY createdDate DESC LIMIT 10];
        return sr;
    }
}


3) Make a basic HTTP call out.

Please refer to the below links which might help you.

http://sfdcmonkey.com/2017/01/02/http-callout-lightning-component/

https://trailhead.salesforce.com/en/content/learn/modules/apex_integration_services/apex_integration_rest_callouts

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas