Category Archive Apex

Bynayakdillip

Write a trigger after update on account object

trigger AccountTrigger on Account (after insert,before insert,after update) {
if(Trigger.IsAfter && Trigger.IsUpdate){
set accountSetID=new set();
for(Account AccountDetail:Trigger.new){
system.debug('Account Name'+AccountDetail.Name);
system.debug('AccountDetail.id'+AccountDetail.id);
accountSetID.add(AccountDetail.id);//Store ALl Account ID in Set
}
system.debug('accountSetID'+accountSetID.size());
List contactList=[SELECT LastName,FirstName FROM Contact WHERE ACCOUNTID=:accountSetID];//All Contacts
for(Contact ContactDetail:contactList){
ContactDetail.FirstName='IBM'+ ContactDetail.FirstName;
}
system.debug('contactList'+contactList.size());
system.debug('Contact Name'+contactList[0].FirstName);
update contactList;
}

}

Bynayakdillip

Write Trigger for before Insert on account object

trigger AccountTrigger on Account (after insert,before insert,after update) {
If(Trigger.IsBefore){
for(Account AccountDetail:Trigger.New){
If(AccountDetail.Type=='Prospect'){
AccountDetail.AnnualRevenue=5;//Update AnnualRevenue
}
}
}
Bynayakdillip

How to consume third party Rest API From salesforce

Rest API:Representational State Transfer

Let’s discuss some stubs about Salesforce Integration.

How to consume third-party Rest API From salesforce.

here I am going to explain how we send request from salesforce to third-party rest API. Then
get a response from third-party API.

the simplest request is a GET request(HTTP Method).

GET–>GET methods to help retrieve data from identified by a URL.

Example:-

First, need to add your third-party REST API to the remote site settings in Salesforce org.Once done, we need to create apex code to retrieve records.

I have found below Third party URL for testing point of view

https://jsonplaceholder.typicode.com/

Step:-1

setup–>Remote Site Setting

Step:-2

Add URL

https://jsonplaceholder.typicode.com

Step:3
I. Open the Developer Console from the Setup gear (Setup gear icon).
II. On the Developer Console, select Debug | Open Execute Anonymous Window.

 Http http=new Http();
 HttpRequest request=new HttpRequest();
 request.setEndpoint('https://jsonplaceholder.typicode.com/users');
 request.setMethod('GET');
 HttpResponse response=http.send(request);
 if(response.getStatusCode()==200)
  {
            list<object> results=(list<object>)JSON.deserializeUntyped(response.getBody());
          
            for(object user:results)
            {
                system.debug('user'+user);
            }
   }

III. Select code, and then click Execute.
IV. After the debug log opens, select Debug Only to view the output of the System.debug statements.

The user detail of user is displayed.




Class Example:-

public class jsonplaceholderAPI {
   
public  static list<object> GetUsers()
    {
        Http http=new Http();
        HttpRequest request=new HttpRequest();
        request.setEndpoint('https://jsonplaceholder.typicode.com/users');
        request.setMethod('GET');
        HttpResponse response=http.send(request);
        list<object> results=new list<object>();
        if(response.getStatusCode()==200)
        {
           results=(list<object>)JSON.deserializeUntyped(response.getBody());
           
        }
        return results;
    }
}
Bynayakdillip

Access Modifiers in apex

Apex has four access modifiers

Private
Public
Protected
Global

Private :If you define private that you don’t want outside code to call(like within class).in other words, methods used by the
public methods of your class.

Note: If you do not specify an access modifier, the method or variable is private.

Public :If you define public that use within class as well as outside the class means any code anywhere can access
the public .

protected : if you define protected it access to any inner class and to the classes that extend the defining apex
class.

global:if you define global it can used by apex code that has access to the class .not just the apex code in
the same application it also access outside of the application like Rest API/SOAP API.