Friday, 30 October 2020

Bind Record Using Wrapper Class in Component | Opportunity, Account and Contact List

WELCOME TO SFDC4STUDENTS.COM 

Step 1 - Create Apex Class named OpportunityAccountContactController.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class OpportunityAccountContactController {
    @AuraEnabled
    public static string getOppWithAccAndCon(String oppId){
        OpportunityAccountContactListWrapper oppAccConWrapper = new OpportunityAccountContactListWrapper();
        Opportunity oppRecord = [SELECT Id, Name, AccountId FROM Opportunity WHERE Id =: oppId limit 1];
        if(oppRecord.Id != null){
			string strAccId = oppRecord.AccountId;
			Account accList = [SELECT Id, Name, BillingState, Website, Phone,
                                 (SELECT Id, FirstName, LastName, Email From Contacts)
                                 FROM Account WHERE Id =: strAccId];			
			
			oppAccConWrapper.opportunityRecord = oppRecord;
            oppAccConWrapper.accRecord = accList;
            oppAccConWrapper.contactList = accList.Contacts;
            oppAccConWrapper.contactCount = accList.Contacts.size();
        }
        return json.serialize(oppAccConWrapper);
    }
    
    // wrapper class with @AuraEnabled and {get;set;} properties 
    public class OpportunityAccountContactListWrapper{
		@AuraEnabled
        public Opportunity opportunityRecord{get;set;}
        @AuraEnabled
        public Account accRecord{get;set;}
        @AuraEnabled
        public List<Contact> contactList{get;set;}
        @AuraEnabled
        public Integer contactCount{get;set;}
    }
}

Step 2 - Create Aura Component named OpportunityAccountContactComponent.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<aura:component controller="OpportunityAccountContactController"  implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global">  
    <aura:handler name="init" value="{!this}" action="{!c.initData}"/>
    <!-- Attributes declaration--> 
    <aura:attribute name="opportunityAccountContactWrapper" type="object"/>
    <aura:attribute name="recordId" type="Id" />
    
    <div class="slds-p-around--large">
        <h1 style="font-size:25px;">{!v.opportunityAccountContactWrapper.opportunityRecord.Name}</h1> 
        <h1 style="font-size:15px;">{!v.opportunityAccountContactWrapper.accRecord.Name}</h1> 
        <br/>
        <p style="color:red">Total Contacts = {!v.opportunityAccountContactWrapper.contactCount}</p>
        
        <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="First Name">First Name</div>
                    </th>
                    <th scope="col">
                        <div class="slds-truncate" title="First Name">Last Name</div>
                    </th>
                    <th scope="col">
                        <div class="slds-truncate" title="Email">Email</div>
                    </th>
                </tr>
            </thead>
            <tbody>
                <aura:iteration items="{!v.opportunityAccountContactWrapper.contactList }" var="con">
                    <tr>
                        <th scope="row">
                            <div class="slds-truncate" title="{!con.FirstName}">{!con.FirstName}</div>
                        </th>
                        <th scope="row">
                            <div class="slds-truncate" title="{!con.LastName}">{!con.LastName}</div>
                        </th>
                        <th scope="row">
                            <div class="slds-truncate" title="{!con.Email}">{!con.Email}</div>
                        </th>
                    </tr>
                </aura:iteration>
            </tbody>
        </table>
    </div>
</aura:component>

Step 3 - Create Aura Component JavaScript Controller named OpportunityAccountContactController.

1
2
3
4
5
6
({
    initData: function(component, event, helper) {
        console.log('***Controller Called');
        helper.getAccountContactWrapperhelper(component, event, helper);
    },
})

Step 4 - Create Aura Component JavaScript Helper named OpportunityAccountContactHelper.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
({
    getAccountContactWrapperhelper: function(component, event, helper) {
      console.log('***helper Called');
      var a= component.get("v.recordId");
      console.log('***In helper'+a);
      //call apex class method
      var action = component.get('c.getOppWithAccAndCon');
        action.setParams({
            oppId : component.get("v.recordId")
        });
      action.setCallback(this, function(response) {
        //store state of response
        var state = response.getState();
        if (state === "SUCCESS") {
          //set response value in wrapperList attribute on component.
          component.set('v.opportunityAccountContactWrapper', JSON.parse(response.getReturnValue()));
        }
      });
      $A.enqueueAction(action);
    },
})

Step 5 - Create a Lightning Action Button on Opportunity Object.


Step 6 -  Add Action Button on Opportunity Record Page Layout.




Step 7 -  Go to any Opportunity Record. Then click the button from the Opportunity Record Page.


Step 8 -  See the bound data on the Modal Popup of Component.


Thanks for come to my Blog. If this post is helpful to you then share and follow to get a new coming SFDC4Students blog posts.

Tuesday, 27 October 2020

Create Rest API in Salesforce | Execute Rest API on Workbench

 Step 1: Create an Apex class for Rest API.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
@RestResource(urlMapping='/api/Account/*')
global with sharing class MyFirstRestAPIClass
{
    @HttpPost
    global static String doPost(String name,String phone,String AccountNumber ) 
    {
        Account acc = new Account();
        acc.Name = name;
        acc.Phone = phone;
        acc.AccountNumber = AccountNumber ;
        insert acc;
        
        return acc.id;
    }
}

Step 2: Open the Workbench and log in with Salesforce Credential.

Step 3: Click on "Utilities (1)" and then click on the "REST Explorer".



Step 4: Select the POST radio button (2).

Step 5: Use the REST API URL (3).

As you can see, the class is annotated with @RestResource(urlMapping='/api/Account/*).
The base endpoint for Apex REST is
https://instance.salesforce.com/services/apexrest/.

The URL mapping is appended to the base endpoint to form the endpoint for your REST service.
For example, in the class example, the REST endpoint is
https://instance.salesforce.com/services/apexrest/api/Account/.

Step 6: Use the Request Body in JSON format and click on the Execute button (4).

{
  "name" : "My Rest API Account",
  "phone" : "9876543210",
  "AccountNumber" : "54321"
}

Step 7: Finally you will get the response with status code 200 (5).

Thanks for spending your valuable time with us.

Total Pageviews