Deprecated: Optional parameter $list declared before required parameter $is_script is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php on line 21

Deprecated: Optional parameter $register declared before required parameter $footer_or_media is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php on line 45

Deprecated: Optional parameter $register declared before required parameter $footer_or_media is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php on line 104

Deprecated: Optional parameter $expire declared before required parameter $path is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_functions.php on line 54

Deprecated: Optional parameter $depth declared before required parameter $output is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/themes/entaro/inc/classes/megamenu.php on line 155

Deprecated: Optional parameter $depth declared before required parameter $output is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/themes/entaro/inc/classes/mobilemenu.php on line 147

Deprecated: Optional parameter $args declared before required parameter $wp_customize is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/extensions/customizer/extension_customizer.php on line 583

Deprecated: Optional parameter $args declared before required parameter $wp_customize is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/extensions/customizer/extension_customizer.php on line 606

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/feed-rss2.php on line 8
sObject Archives - Salesforce Next Gen https://salesforcenextgen.com/tag/sobject/ Trailhead and Beyond Mon, 28 Dec 2020 19:46:37 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.3 https://salesforcenextgen.com/wp-content/uploads/2020/10/cropped-76dc0dd6-326a-4956-a412-bfdf20c7fb23_200x200-32x32.png sObject Archives - Salesforce Next Gen https://salesforcenextgen.com/tag/sobject/ 32 32 Apex Programming for Beginners https://salesforcenextgen.com/apex-programming-beginners/ https://salesforcenextgen.com/apex-programming-beginners/#comments Thu, 27 Jul 2017 04:36:47 +0000 http://www.salesforcenextgen.com/?p=840 Apex Programming for Beginners Introduction to Apex Class and Trigger What is Apex Programming and why is it required for customizing Salesforce? Apex is an object oriented Programming language, it is stored, compiled and run on force.com platform and is, therefore, on-demand programming language. Apex’s syntax is similar to Java, they behave similar to a …
Continue reading Apex Programming for Beginners

The post Apex Programming for Beginners appeared first on Salesforce Next Gen.

]]>
Apex Programming for Beginners

Introduction to Apex Class and Trigger

What is Apex Programming and why is it required for customizing Salesforce?

Apex is an object oriented Programming language, it is stored, compiled and run on force.com platform and is, therefore, on-demand programming language.

Apex’s syntax is similar to Java, they behave similar to a stored procedure, and the logic written in Apex can be linked to the record save procedure. Salesforce’s apex provides features for unit test creation and execution.

What are the capabilities of Apex?

Apex help in programmatic access to the exposed Salesforce.com feature. The list of these features is mentioned below.

Actions, Approval process, Authentication, chatter, communities, email, metadata, Salesforce knowledge, Salesforce connect, territory management, visual workflow etc.

What are Apex transactions and Governor limit?

Salesforce has made its foundation based on multi-tenant architecture, to ensure the resources are used efficiently, Salesforce introduced the governor execution limit. Following terms are used together when it comes to talk about Governor limits.

Mostly Governor limits are applicable per Apex Transactions basis, but some are on rolling 24 hours basis. Apex transaction is the execution of a set of operation as one unit, boundary of the transaction can be a trigger, anonymous code or a web service etc.

Execution Governors and Limits- since Salesforce is in a multitenant environment,  its servers ensures that a single apex code does not monopolize all the resource and keeps a check on all the execution and these checks are known as governor limits.

Running Apex within Governor Execution Limits- sometimes there arises a need or requirement which wants us to execute code which will breach the governor limit. In those circumstances, to adhere to these limits certain design patterns are used, i.e. bulk calls and relationship queries.

Getting started with Coding in Apex

Before we proceed to deep dive into the code we need to know certain fundamentals of Apex.

As we mentioned earlier, the syntax of Apex is similar to java, but there are other fundamentals as well which are quite similar, below are the features similar to Java.

Data Types and Variables

Control Flow Statements

Classes, Objects, and Interfaces

Working with Data in Apex

Data Types and variables

All variable in apex have a data type, it is basically an alias for a memory location allocated to hold a value. For eg. sObject, integer, enum etc.

Then Salesforce has primitive data type, integer string, double etc. It also has collections namely list, set and Map.

Control Flow Statements

To control the flow of statements, apex provides if-else and loops.

Conditional (If-Else) Statements

Loops

Loops

Apex is presently supporting 5 types of loops, namely: while loop, do while loop, for loop, for-each loop

For loop with SOQL as an argument.

Objects interface and classes

Similar to Java, Apex is also based on the concept of Classes, interface, and objects. So what is the difference between a Java class and Apex Class, main differences are mentioned below:

  1. Classes and methods are final by default.
  2. Default access modifier is private, i.e. specifying no access modifier and declaring privately is synonymous in Apex.
  3. An interface is always global, no other access modifiers are allowed.
  4. Inner class and interface are allowed only one level deep, inner class behaves like static without specifying it as static.

How to create a class?

Salesforce has provided us with the  Apex and Visualforce editor in the UI. There are many ways to reach the editor.

Kindly note that Apex class and triggers can only be created in either sandbox or developer edition, but Visualforce is allowed to be created at the production environment.

Apex Class editor has a help text inbuilt, this helps in auto completion of the syntax, which save a lot of time.

Now with all the Bookish knowledge which is available on the internet, we should now proceed to write some code.

Next : Working with Data

The post Apex Programming for Beginners appeared first on Salesforce Next Gen.

]]>
https://salesforcenextgen.com/apex-programming-beginners/feed/ 1
Salesforce: Dynamic SOQL Chapter 3 contd. https://salesforcenextgen.com/salesforce-dynamic-soql-chapter-3-contd/ https://salesforcenextgen.com/salesforce-dynamic-soql-chapter-3-contd/#comments Wed, 05 Jul 2017 14:35:25 +0000 http://salesforcenextgen.com/?p=768 Salesforce: Dynamic SOQL Chapter 3 Contd. Now in the previous post, we were able to fetch the fields related to an sObject at the run time and we used string o create a structured query according to the SOQL syntax. Below is the complete code where I have selected an sObject at run time and …
Continue reading Salesforce: Dynamic SOQL Chapter 3 contd.

The post Salesforce: Dynamic SOQL Chapter 3 contd. appeared first on Salesforce Next Gen.

]]>
Salesforce: Dynamic SOQL Chapter 3 Contd.

Now in the previous post, we were able to fetch the fields related to an sObject at the run time and we used string o create a structured query according to the SOQL syntax. Below is the complete code where I have selected an sObject at run time and found its field and fetched a list of sObject.

When it comes to displaying this list of sObject on the Visualforce page, then it becomes little tricky. As we know that to print on the Visualforce page we need to know the field of the sObject before hand, therefore, the regular way of displaying the table is out of the question.

To overcome this issue, we created a list of string as well while we created the query string for the database.query() method. With help of this, we were able to display all the fields fetched at the run time. As we find in SQL.

Below is the complete code :-

Apex:

public class PostTestingClass {   

    public list<sObject>sObjectList {get;set;}

    public list<string> FieldNameForTable1{get;set;}

    public list<string> FieldNameForTable{get;set;}

    public list<selectoption> fieldList {

        get{return findTheFields();}

        set;}

    public boolean resultTableVisible {get;set;}

    public string selectedObject = ‘sfng__ob1__c’;

            public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe(); // Org Map

     public string queryString {

        get{

            FieldNameForTable = new list<string>();

            string finalQueryString = ‘select ‘ ;

            for(SelectOption s:fieldList){

                finalQueryString = finalQueryString + s.getValue() + ‘,’;

                FieldNameForTable.add(s.getValue());

            }

            finalQueryString = finalQueryString.removeEnd(‘,’);

            finalQueryString += ‘ from ‘ + selectedObject;          

            return finalQueryString ;}

        set;

    } 

    public  PostTestingClass(){resultTableVisible = false;}

    public list<selectoption> findTheFields(){

                                    list<selectoption> fieldList1  = new list<selectoption>();

                                    Map<String, Schema.SObjectField> schemaFieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();

               

                    for(Schema.SObjectField sfield : schemaFieldMap.Values()){                       

                        schema.describefieldresult dfield = sfield.getDescribe();                       

                        fieldList1.add(new SelectOption(dfield.getname(),dfield.getlabel()));

                }

        return fieldList1;

    }

        public void fetchResultOfQuery(){

            resultTableVisible = true;

        sObjectList = new list<sObject>();

        FieldNameForTable1 = new list<string>();      

      

        system.debug(‘queryString — > ‘ + queryString);

        sObjectList = database.query(queryString);       

        for(string s: FieldNameForTable){

            s = s.replace(‘ ‘, ”);

            FieldNameForTable1.add(s);

        }

                        system.debug(‘FieldNameForTable1 — > ‘ + FieldNameForTable1);       

    }           

}

Visual Force Code:-

<apex:page controller=”PostTestingClass” >

    <apex:form >

        <apex:pageBlock >

            <apex:selectList size=”1″>

                <apex:selectOptions value=”{!fieldList}”></apex:selectOptions>

            </apex:selectList><br/><br/><br/>

            <apex:inputTextarea value=”{!queryString}” /><br/>

            <apex:commandButton action=”{!fetchResultOfQuery}” value=”Search” />

        </apex:pageBlock>

         <apex:pageBlock id=”resultTable” rendered=”{!resultTableVisible}”>           

            <apex:outputPanel >

                <apex:pageBlockTable value=”{!sObjectList}” var=”records”>

                    <apex:repeat value=”{!FieldNameForTable1}” var=”fields”>

                        <apex:column value=”{!records[fields]}”></apex:column>

                    </apex:repeat>

                </apex:pageBlockTable>

            </apex:outputPanel>

        </apex:pageBlock>

    </apex:form>

</apex:page>

Dynamic SOQL result

Next, I will show you how to implement all of this in a small project which works just as SOQL Query builder of WorkBench.

The post Salesforce: Dynamic SOQL Chapter 3 contd. appeared first on Salesforce Next Gen.

]]>
https://salesforcenextgen.com/salesforce-dynamic-soql-chapter-3-contd/feed/ 6
Salesforce Dynamic Apex Chapter 2 Fetching Fields of sObject Dynamically !! https://salesforcenextgen.com/salesforce-dynamic-apex-chapter-2-fetching-fields-of-sobject-dynamically/ https://salesforcenextgen.com/salesforce-dynamic-apex-chapter-2-fetching-fields-of-sobject-dynamically/#comments Tue, 04 Jul 2017 04:07:27 +0000 http://salesforcenextgen.com/?p=725 Salesforce Dynamic Apex Chapter 2 Fetching Fields of sObject Dynamically !! In the previous post, we were able to fetch all the sObjects which are present in the Org at that time. So now the obvious question is, can we fetch the fields of a sObject dynamically as well? yes, indeed we can. Salesforce has provided …
Continue reading Salesforce Dynamic Apex Chapter 2 Fetching Fields of sObject Dynamically !!

The post Salesforce Dynamic Apex Chapter 2 Fetching Fields of sObject Dynamically !! appeared first on Salesforce Next Gen.

]]>
Salesforce Dynamic Apex Chapter 2 Fetching Fields of sObject Dynamically !!

In the previous post, we were able to fetch all the sObjects which are present in the Org at that time. So now the obvious question is, can we fetch the fields of a sObject dynamically as well? yes, indeed we can. Salesforce has provided us with the SObjectField Class and Schema.DescribeFieldResult class. These classes provide us access to the metadata of a specific sObject’s fields at run time. With the help of these classes, we not only get information about name or label, we get a lot more information for e.g. whether the field is accessible or is it a calculated field etc.

Below I will show how to fetch fields of a sObject dynamically. Here I am going to fetch all the fields available for a specific sObject and show it as a select list on a Visualforce page.

First method that I am going to use is “schemaMap.get(selectedObject).getDescribe().fields.getMap();” this method returns a map of String and Schema.SObjectField. using this returned map we make use of a for loop where we traverse through each field and make a call to describe the method, which returns us with the schema describe field result variable. This variable holds all the values we require to make the select list.

Below is the apex code:

public list<selectoption> findObjectsInOrgSelectList(){

        list<selectoption> fieldList         = new list<selectoption>();

        if((string.isNotBlank(selectedObject)) || (string.isNotEmpty(selectedObject))){           

                if(schemaMap.get(selectedObject) != schemaMap.get(”)){

                    Map<String, Schema.SObjectField> schemaFieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();

               

                    for(Schema.SObjectField sfield : schemaFieldMap.Values()){                       

                        schema.describefieldresult dfield = sfield.getDescribe();                       

                        fieldList.add(new SelectOption(dfield.getname(),dfield.getlabel()));

                }

            }

        }       

        return fieldList;

    }

The method “findObjectsInOrgSelectList” returns a list of selectoption populated with all the fields related to a selected sObject present in the Org dynamically.

Following is the property in Apex class which uses this method and is accessible on visual force page.

public list<selectoption> fieldList {

        get{ return findObjectsInOrgSelectList();}

        set;

    }

With help of this method and property we make the select list in the Visual force page as shown below:-

<apex:page>                                      

            <apex:form>

                        <apex:pageBlock>

                                    <apex:outputPanel>

                                                <apex:selectList size=”6″ multiselect=”true” value=”{!selectedfield}” style=”width:150px;”

                                 onchange=”rerenderQueryString()”  >                 

                    <apex:selectOptions value=”{!fieldList}”></apex:selectOptions>

                    <apex:actionfunction name=”rerenderQueryString” reRender=”queryString” />

                        </apex:selectList>

                                    </apex:outputPanel>

                        </apex:pageBlock>

            </apex:form>

</apex:page>

sObjectField dynamic

Here we see that on the selection of a custom object the related fields of the object are populated in the select list.

**Note this code is in continuation of the previous post, I will merge all the code to make a complete working model of Dynamic Apex.

Next: 

The post Salesforce Dynamic Apex Chapter 2 Fetching Fields of sObject Dynamically !! appeared first on Salesforce Next Gen.

]]>
https://salesforcenextgen.com/salesforce-dynamic-apex-chapter-2-fetching-fields-of-sobject-dynamically/feed/ 4