Salesforce Development Interview Questions 2







How to access SObjects Fields?

SObjects fields can be accessed or changed with simple "Dot" notation

Syntax: <SObject Name> <variable Name> = new <SObject Name>();

// for accessing SObject fields

<variable Name>.<FieldAPI Name> = <Assign any value>;

Ex: Account a = new Account();

a.Name = 'acme';

In Which Object "All Apex Triggers" are stored?

  • All Triggers are stored in "ApexTriggers" object.
  • Each apex trigger code will get resides inside the file with the extension “.apxt”

In which Object "All Apex Classes" are stored?

  • All Apex classes are stored in "ApexClass" Object
  • Each apex class code will get resides inside the file with the extension “.apxc”

In Which object all Apex Pages are stored?

ApexPage

What is "Array" in sales force apex?

  • Arrays is nothing but allows to store a group of (same data type elements) elements in side the variable. It won't to support store (different data type elements)
  • It is fixed size collections, where up on defining the array we have to specify array size, which can't be grow/shrink at run time. 
  • Arrays purely supports static memory allocations. which causes the memory wastage.

Syntax: 

<DataType>[] <arrayName> = new <DataType>[<size>];

Example:

Integer[] someValues = new Integer[10];

// Assigning the Values 

someValues[0] = 1;

someValues[1] = 1;

someValues[2] = 1;

someValues[3] = 1;

What is "Object Relational Mapping" in sales force apex?

  • ORM transforms each and every(table) objects in the form of class. It will create with the same name as like table Name only.
  • ORM transform each field inside the object in the form of variables in the Associated class.

How to Create Related Records or Associated Records any two objects in sales force Apex?

While we are inserting or creating related records, we have to follow below steps.

Step 1: write the code to insert parent record, because with out parent record id we can't able to create or insert child record.

Step 2: write the code to insert child record

Step 3: This step we need must whenever we are creating related records or updating records need to map with parent id and child id

Step 4: Make the related child record to be relate with parent record.

Example:

public static void childRelaredRecords()

{

//1.write the insert parent record

// create instantiation for class or SObject

Account acc = new Account();

//assign the values associated object

acc.Name = 'Parent Account Record';

acc.Rating = 'Hot';

acc.Active__c = 'Yes';

acc.BillingCity = 'kkl';        

//insert records use insert statement

insert acc;

if(acc.Id !=Null)

{

system.debug('record inserted successfully....:' + acc.Id);

//create instantiation for child class or SObject

Contact con = new Contact();

//assign the child values 

con.LastName = 'rama';

con.FirstName = 'Contact Record';

con.Email = 'test@sjgdkfhe.com1';

//make the contact to be related Account

con.AccountId = acc.Id;

//insert child records

insert con;

if(con.id !=Null)

{

system.debug('records created successfully.....:' + con.id);

}       

}     

}

Note: If we need to create child record an 'existing parent record' how can we do?

public static void existingParentRecord()

{

        //create instantiation of the child class or SObject child object

        CopadoChildObject__c coChRec1 = new CopadoChildObject__c();

        //assign the values for child object of the class

        coChRec1.Name = 'SomeTest';

        coChRec1.EmailChild__c = 'shdskdh@gmail.com';

        coChRec1.MobilePhoneNumber__c = 89987987;

        //make the child object to relate parent object

        //we need to take relation parent field Api Name

        coChRec1.CopadoObject__c =  'a045i00000ApLlOAAV';

        insert coChRec1;

        if(coChRec1.id != Null)

{

            system.debug('record created successfully.....:' + coChRec1.id);

        }

    }

What are the Draw backs of "Arrays" in sales force Apex?

1. arrays supports to store only same data type elements. It won't support other data type elements

2. arrays are fixed size collections. where the array can't be grow/shrink at run time

3. arrays are purely support static memory allocation, which causes the memory wastage.

4. arrays Doesn't provide ready-made methods to be used to manage the elements

What are the "Sub Program"?

  • While implementing the business logic for the application, we have to divide the logic into the various smaller manageable pieces called as 'Sub Program'
  • It is part of a Programme, which contains set of statements, to be used to perform certain operation inside the application

Apex provides two types of Sub Program

1. Procedures

2. Functions

Note: Sub programs are also called as "Methods". All the procedures and functions should be implemented inside the class

By using this we divided manageable pieces and we can reuse the features based on the need. Hence we can reduce the complexity of the application

1. Procedures: It is contains set of statements, which is used to perform the complex operations inside the application.

It is divided into two types

1. Non-parameterized procedures(No input values)

2. Parameterized procedures(With input values)


What is "Parameterized Procedure" in apex sales force?

We Can assign multiple number of input parameters based on our requirement. There is no limit in sales force.

In this method, we can define the procedure which can take the input values from the end user. so that we can make functionality dynamic and we an generate different results.

Note: While defining the procedure, we have to specify required parameters with associated data types. we can supply "N" number of parameters procedure.

Syntax: 

[Access Specifiers] void <procedureName>(<DataType> <ParameterName>, <DataType> <ParameterName>, <DataType> <ParameterName>,.....);

Example:

public void getAccountRecords(Id recordId);

{

}

Note: While calling/ invoking the procedure we have to supply the values for the parameters

we have to supply the values for the parameters in the same order as per the definition

Syntax: <objectName>.<procedureName>(<value 1>,<value 2>,.....<value N>);

Ex: commonHelper cHelper = new commonHelper();

cHelper.Addition(2400, 4500);

cHelper.Concatenate('Welcome', 'Hyderabad');

cHelper.GetAccountRecord('3483240234084334');

Note: we can pass the values from outside  apex class to our Apex class

Example:

//write an apex program, insert or create a record any object with the help of parameterized process

    public static void insertCopadoObjectRecord(String cOpRecName, String cOpRecEmailId, String cOpRecMobile)

{

        // create instantiation of the class or sObject and give the input parameters

        CopadoObject__c cOpRec = new CopadoObject__c();

        // assign the values given parameters

        cOpRec.Name = cOpRec.Name;

        cOpRec.CopadoEmail__c = cOpRecEmailId;

        cOpRec.MobilePhone__c = cOpRecMobile;

        // use insert statement

        insert cOpRec;

    }

What is "Collections" in apex sales force?

  • It is allows us to store a group of elements of type both "Homogeneous" and "Heterogeneous". i.e. we can store both same data type and different data type elements inside the collection.
  • Collections allows us to store any number of elements. where size can grow/ shrink at run time.
  • It's supports purely dynamic memory allocation. Hence there is no memory wastage.
  • It provides a set of ready-made methods to be used to manage the elements inside the collection.

What are the Types of Collections available in apex?

  • 1.List(Ordered and allow duplicates)
  • 2.Set(Un-Ordered and It will not allow duplicates)
  • 3.Map(Key and Value Pair)

What is "List" Collection?

  • List is Ordered Collection (i.e., List collection will hold the elements in same order which we were inserted elements.)
  • List Collections allows us to store Both data type elements(primitive, SObject, Apex, Collection and User defined type of elements)
  • It supports dynamic memory allocation, the collection size grow/shrink at run time.
  • We can access list elements with index Position, Which always starts with 'Zero'.
  • List collections will allows us to store duplicate elements also
  • It provides ready made methods, to be used to manage the elements inside the collection.
  • We can sort list elements with sort method (default sorting order is ascending).
  • Contains method is not available in List.
  • We can process records which are stored in list using DML statements (insert, update, delete and undelete).

Syntax:

List<DataType> <ObjectName> = new List<DataType>();

Examples:

List<Integer> customerCodes = new List<Integer>();

It can hold only collection of integer values or elements

List<String> customerName = new List<String>();

It can hold only collection of string values or elements

List<Id> recordIds = new List<Id>();

It can hold collection of record Id's

List<Account> lstAccounts = new List<Account>();

It holds collection of Account records

List<Object> lstElements = new List<Object>();

It holds a collection of Heterogeneous primitive elements

List<SObject> lstRecords = new List<SObject>();

It holds a collection of Heterogeneous object records or elements

Note: List collection supports the nested List collections up to 5 levels

Ex: List<List<List<List<List<String>>>>> lstElements = new List<List<List<List<List<String>>>>>();

What are the methods available in "List Collection"?

1.Add(<ElementName>):This method is used to add a new element into the collection. It will add the element at the end of the collection

Ex: lstElements.add('Apex');

lstElements.add('subba');

lstElements.add('Reddy');

lstElements.add('India');

2.Add(<ElementName>, <indexPostion>):This method is used to insert an element at the specified index position

Ex: lstElements.add('Hyderabad', 4);

3.AddAll(<collection Name / Array Name>):This method is used to add a Group of elements to the list collection at a time

Ex: String[] countryName = new String[] ('India', 'China', 'Germany');

lstElements.AddAll(countryName);

4.Integer Size():This method is used to fetch the number of elements count inside the collection.

Ex: lstElements.size();

5.Boolean IsEmpty():This method is used to verify the collection is empty or not

Ex: if(lstElements.isEmpty())

system.debug('collection is empty');

Else

system.debug('collection is not empty');

6.Get(<IndexPosition>):It returns the elements exist at the specified index position

Ex: lstElements.get(2);

7.Boolean Contains(<ElementName>):It returns True, if the element found in the collection, else returns False

Ex: if(lstElements.Contains('India'))

system.debug('value is available');

Else

system.debug('value is not available');

8.Integer IndexOf(<ElementName>):It returns the index position of specified element

  • If the element found, then it returns the index position
  • If the element not found, it returns (-1), Negative value
  • If the element is duplicate, it returns the "first occurrence" of the element index position.

Ex: lstElements.IndexOf('Bangalore');

9.Set(<elementName>, <IndexPosition>):It will replace the element values based on the specified index position.

Ex: lstElements.Set('Some', 4);

10.Remove(<IndexPosition>): This method will remove the element from the collection on the specified index position

Ex: lstElements.Remove(3);

11.Sort():This method will arrange the elements an Ascending order by default.

Ex: lstElements.Sort();

12.Clone():It creates duplicate copy of the list collection.

Ex: List<String> backupCopy = lstElements.Clone();

13.boolean equals(<ListCollectionName>):This method will returns True, if both collections are identical, else it returns False

Ex: if(lstElements.Equals<ListCollectionName>)

system.debug('both collections are identical');

Else

system.debug('Both collections are not identical');

14.Clear():This method will remove all the elements from the list collection.

Ex: lstElements.Clear();

What is "Set Collection" in apex sales force?

Set is an unordered collection(i.e. The elements will not be available in the same order in which we were inserted.)

  • It allows us to store a group of elements both "Homogeneous and Heterogeneous"("Primitive, SObject, Collection, Apex, and User defined types")
  • Set doesn't allow duplicates. It maintains the uniqueness of the elements by using "Binary Comparison"
  • Set elements cannot be accessed with index.
  • Set collection supports the dynamic memory allocation, hence no memory wastage.
  • Set collection size can grow/shrink at run time
  • Set collection provides a group of ready made methods to be used to mange the elements inside the collection.
  • Set is working as like a filter. it won't allow duplicates with the help of Binary comparison.

Syntax:

set<DataType> <objectName> = new set<DataType>();

Examples:

set<String> countryNameSet = new set<String>();

holds a collection of unique String elements

set<Integer> productCodeSet = new set<Integer>();

holds a collection of unique String elements

set<Id> RecordId = new set<Id>();

holds a collection of unique Id elements

set<Account> setAccounts = new set<Account>();

holds a collection of unique Account elements

What are the methods available "Set Collection"?

1. add(set Element)

2. AddAll(SetElement)

3. clear()

4. clone()

5. add(index, setElement)

6. equals(set2)

7. removeAll(setOfElementsToRemove)

8. remove(setElement)

9. size()

10. AddAll(from List)

11. hashCode()

12. IsEmpty()

13.containsAll(SetToCompare)

What is "Map Collection"?

Map is a Key-Value pair Collection. Where each element contains a "Keys" and "Values".

Inside the element, 'Key' should be always "Unique" and Value may be "Unique/Duplicates"

It allows us to store a group of elements of type both "Homogeneous and Heterogeneous" elements

Both "Key and Value" can be either 'Primitive/SObject/Collection/Apex/User Defined' type of elements

It supports the dynamic memory allocation. Hence the collection size can Grow/ Shrink at run time

It Provides a set of ready made methods to be used to manage the elements in the collection.

Syntax:

Map<KeyDataType, ValueDataType> <objectName> = new Map<KeyDataType, ValueDataType>();

Examples:

Map<String, String> mapCountries = new Map<String, String>();

Holds a collection of Country Details, where countryCode = Key; countryName = Value

Map<Id, Integer> mapRelatedCases = new Map<Id, Integer>();

Holds a collection of Account records and related Cases count, where Account ID = key; Cases count = Values

Map<Id, All_flights__c> mapAllFlightRecords = new Map<Id, All_flights__c>();

Holds a collection of All flight records, where Flight record id = Key; flight record = Value

Map<Id, Lead> mapLeadRecords = new Map<Id, Lead>();

Holds a collection of lead records, where lead record id = key; complete lead record = Value

Map<Account, List<Opportunity>> mapOpportunities = new Map<Account, List<Opportunity>>();

Holds a collection of Account records along with related opportunities. where Account Record = key; List of related Opportunities = Values

Map<Id, List<Position__c>> mapPositions = new Map<Id, List<Position__c>>();

Holds a collection of Position records where Hiring manager record id = Key; List of related positions = Values

What are the methods available in "Map Collection"?

1.Put Method: Put(<key>, <value>);

Ex: fruitsMap.Put('Red', 'Apple');

fruitsMap.Put('Yellow', 'Mango');

fruitsMap.Put('Black', 'Grapes');

fruitsMap.Put('Orange', 'Orange');

fruitsMap.Put('Green', 'Grapes');

2.PutAll: PutAll(<mapCollection>);

Ex: fruitsMap.PutAll(<mapCollectionName>);

3.Size: Size();

Ex: fruitsMap.size();

4.Boolean IsEmpty(): IsEmpty();

Ex: if(fruitsMap.IsEmpty())

system.debug(collection is empty....);

Else

system.debug(collection is not empty....);

5.Get: Get(<keyName>);


Ex: fruitsMap.Get('Orange'); o/p is: Orange


6.Boolean ContainsKey: ContainsKey(<keyName>);

Ex: if(fruitsMap.ContainsKey('Yellow'))

system.debug('element found in the collection');

Else

system.debug('element not found in the collection');

7.Remove: Remove(<keyName>);

Ex: fruitsMap.Remove('Red');

8.Clear: Clear();

Ex: fruitsMap.Clear();


9.KeySet: Set<DataType> KeySet();

Ex: Set<String> keysCollection = fruitsMap.KeySet();if

10.values:Values();

Ex: List<DataType> values();

List<String> valuesCollection = fruitsMap.Values();

11.Clone: Clone();

Ex: Map<String, String> backUpCopy = fruitsMap.Clone();

12.Boolean Equals:  Equals(<mapCollectionName>);

Ex: if(fruitsMap.Equals(<mapCollectionName>))

system.debug('Both the collections are identical');

Else

system.debug('Both the collections are not identical');


Post a Comment (0)
Previous Post Next Post