Showing posts with label Salesforce examples. Show all posts
Showing posts with label Salesforce examples. Show all posts

Sunday, May 24, 2015

Schema Programming in Apex and its usage in Visualforce Page.

Schema Programming in Apex and its usage in Visualforce Page.


Schema give the meta data information about the data (Object, Fields).

Schema Methods :

1). public static Map<String, Schema.SObjectType> getGlobalDescribe()
   
 This method returns a map of all the Sobject names as keys and Sobject val tokens as Values.


Ex:

Display all the list of objects available in the salesforce organization in the Visualfoce page.


Controller Class:

public class SchemaDescribeExample {
    public List<SelectOption> options;
    
    public list<SelectOption> getOptions(){
        return options;
    }
    
    public SchemaDescribeExample(){
        options = new List<SelectOption>();
        Map<String, Schema.SObjectType> schemaMap = schema.getGlobalDescribe();
        Set<String> objectSet = schemaMap.keySet();
        for(String str:objectSet){
            //praparing label and values in selection option.
            SelectOption op =new SelectOption(str,str);
            options.add(op);
        }
    }
    
    

}


VF Page:

<apex:page controller="SchemaDescribeExample">
 <apex:form >
  <Apex:selectList size="1">
    <apex:selectOptions value="{!options}"></apex:selectOptions>
   
  </Apex:selectList>
 </apex:form>
</apex:page>



That's it.. now it will display the list of all the objects in picklist.


More about Dynamic Apex:



Schema Programming in Apex and its usage in Visualforce Page.




Saturday, May 23, 2015

How to Use Select options in Visualforce page?

How to Use Select options in Visualforce page?

Selectoption object specifies one of the possible values for visualforce select checkboxes, select list, select radio component.

It consist of label that is displayed to the end user, and the value that will be written to the controller.

Note:- The SelectOption can be displayed in the disable state.

Constructors:

We have 2 types of constructors in this class

Instantiating 

SelectOption one = bew SelectOpton (value(String), label(String), IsDisabled(true/false));

If isDisabled is true, the option is disabled . We can not select that value.


SelectOption one = new SelectOption(value, label) 
          Value as String
          Label as String


Methods:

             Ex: one.getLabel();
             output: : One
            Ex: one.setDisabled(false);


Example: 

Controller Class: 

public class SelectOptionExample {

    String[] countries = new String[]{};

    public PageReference test() {
        return null;
    }

    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('US','US'));
        options.add(new SelectOption('CANADA','Canada'));
        options.add(new SelectOption('MEXICO','Mexico'));
        return options;
    }

    public String[] getCountries() {
        return countries;
    }

    public void setCountries(String[] countries) {
        this.countries = countries;
    }
    

}


VF Page: 

<apex:page controller="SelectOptionExample">

   <apex:form >
        <apex:selectCheckboxes value="{!countries}">
            <apex:selectOptions value="{!items}"/>
        </apex:selectCheckboxes><br/>
        <apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/>
    </apex:form>
    
    <apex:outputPanel id="out">
        <apex:actionstatus id="status" startText="testing...">
            <apex:facet name="stop">
                <apex:outputPanel >
                    <p>You have selected:</p>
                    <apex:dataList value="{!countries}" var="c">{!c}</apex:dataList>
                </apex:outputPanel>
            </apex:facet>
        </apex:actionstatus>
    </apex:outputPanel>
</apex:page>




Once you select the options and click on test it will display the selected checkboxes details.




That's it....



Saturday, January 24, 2015

What is Apex Sharing? How to share a records via apex?



Sharing rules, let us make automatic exceptions to organization-wide defaults for particular groups of users.Sharing rules can never be stricter than our org-wide default settings.We can extend the organization wide default setting with sharing rules.

Example – If any object is private with org-wide default  then we can extend the access to public read only or public read write with sharing rule.

Salesforce provide a way by which we can create sharing rule by only point and click  from the salesforce standard. You can set any criteria and give access to the object’s record . Example – Suppose u need to create a sharing rule for lead object when the lead field “Is_public” become true then you can easily add this criteria and give public access to the particular User or group etc.

But some cases are there where we can’t use the standard sharing rule functionality that’s why we need to create sharing rules with apex.

Let’s take a case example  –  I have a field “Reports to” in case object and this field is lookup to User object and we need to give public access to that user for their particular record. Suppose when a case is created and we select some user in the “Reports to” field then we want to give public access to this selected user for that record.So it is not possible with standard sharing rules. We need to create sharing rule for case object via apex.


Here I am sharing the code for how to create sharing rule for any object via Apex,each object has their own sharing object for case it is “CaseShare ” .We need to write down a trigger on case :
 Example:
trigger ShareWithReportingMng on Case (after insert) {
    List<CaseShare> csShareList = new List<CaseShare>();
    for( Case cs : trigger.new ) {
        if( cs.Reports_to__c != NULL ) {
            // Create a new caseShare object for each case where reports_to__c field is not NULL.
            CaseShare csShare = new CaseShare();
            // Give Read write access to that user for this particular case record.
            csShare.CaseAccessLevel = 'edit';
            // Assign case Id of case record.
            csShare.CaseId = cs.id;
            // Assign user id to grant read write access to this particular case record.
            csShare.UserOrGroupId = cs.Reports_to__c;
            csShareList.add( csShare );
        }
    }
    if( csShareList != null && csShareList.size() != 0 ) {
        try {
            insert csShareList;
        }catch( Exception e ) {
            trigger.new[0].Reports_to__c.addError('Error::::::'+e.getMessage());
        }
    }
}

So now you can create sharing rules from apex as above and delete that sharing when needed with apex(delete event).
Considerations and Limits of Sharing Rules


  • Sharing Rules cannot be stricter than Organization Wide Defaults. If access needs to be restricted, another type of security should be used.  Sharing rules are typically used to extend access to records.
  • Manual Sharing is only available on individual records, it is not available for all records of a certain object.
  • Sharing Rules are only applicable on records that have Private or Public Read Only access.
  • With Sharing Rules you have the option to give read only or read/write access to records.  We recommend being very conscious of what level of security users really need (i.e. is the access for informational purposes only or full collaboration).
  • When setting Automatic and Manual Sharing users and admins have the ability to define if the security should be extended to related records.  Make sure that extending the security makes sense before making the final decision to give this access.


 

 
 



Friday, January 23, 2015

How to use apex:variable in Visualforce Page?

How to use apex:variable in Visualforce Page?


A local variable that can be used as a replacement for a specified expression within the body of the component. Use < apex:variable > to reduce repetitive and verbose expressions within a page.
Note: < apex:variable > does not support reassignment inside of an iteration component, such as < apex:dataTable > or < apex:repeat >. The result of doing so, e.g., incrementing the < apex:variable > as a counter, is unsupported and undefined.
 

This tag supports following attributes:


Attribute
Description
id
An identifier that allows the component to be referenced by other components in the page.
rendered
A Boolean value that specifies whether the component is rendered on the page. If not specified, this value defaults to true.
value
The expression that can be represented by the variable within the body of the variable component.
var
The name of the variable that can be used to represent the value expression within the body of the variable component.

Code Example:

<apex:page controller="repeaterCon">
    <apex:variable value="{!1}" var="rowNum"/>
    <apex:repeat value="{!collection}" var="row">
        {!rowNum}-{!row}<br/>
        <apex:variable var="rowNum" value="{!rowNum + 1}"/>
    </apex:repeat>
   
</apex:page>


Class:

public class repeaterCon {
 public List<String> collection {
        get {
            if (collection == null) {
                collection = new List<String>();
                for (Account a : [SELECT ID, Name FROM 

                      Account  LIMIT  10]) {
                    collection.add(a.Name);
                }
            }
            return collection;
        }
        private set;
    }
}





That's it. 

More about VF Tags

http://sfdcsrini.blogspot.com/2014/06/visualforce-form-tags-with-examples.html


Friday, July 18, 2014

The Importance of Customer Relationship.

The Importance of Customer Relationship.

Hi, 
You can get the clear idea of what exactly Customer Relationship Management (CRM) is in the below link.

http://www.openlearningworld.com/books/CRM/The%20Importance%20of%20Customer%20Relationship/index.html




Wednesday, July 16, 2014

How to view Force.com IDE log?

How to view Force.com IDE log?








Tuesday, July 15, 2014

How to use static resource in image formula field in Salesforce?

How to use static resource in image formula field in Salesforce? 


Sample formula:

IMAGE( '/resource/sfdcsrini__Dial', 'Dial');

here "sfdcsrini__Dial" is the static resource name for the image.





Monday, July 14, 2014

Sales Process flow

Sales Process flow 

Sales Process flow starts from Leads i.e.


Creation of Leads --> Conversion of Leads once qualified (Potential Identified) --> Converting Lead to Account, Contact & Opportunity for New customer Or only Opp for an existing customer --> Update & maintaining the Opportunity details (Product, visits, closing date, etc) as per the customer follow up --> closing the Opp after the logical conclusion of the order.
Our Sales process flows through various stages in Sales funnel starting from Prospecting/design Assit --> Budget Price --> RFQ (Request for Quote) --> Tender/Proposal --> Negotiation/Review --> Order Agreed --> Closing of Opp (Won / Lost / No Bid / Cancelled).


Note: It changes from one organization to another.





How to find the deployment status in Salesforce?

How to find the deployment status in Salesforce? 


Note: that this page shows deployments started via the deploy() metadata API, which includes Force.com IDE, Force.com Migration Tool, but not change sets.







Sunday, July 13, 2014

How to Read/Create record types in Apex test class?

How to Read/Create record types in Apex test class?

We have some important consideration while creating test data in our test classes.
So let say if we need to create a record based on the record type id , what we do normally , we query on record type object based on SobjectType and the record type Name.

for example :
let say we have a Account Record Types A & B Not if we need to create a Account Record with record type A or B . We normally query like this .
RecordType rt = [select id,Name from RecordType where SobjectType='Account' and Name='A' Limit 1];
And now use this value further while creating the Account Record.
Account acc = new Account(Name='Test' , recordTypeId=rt.id);

To avoid this query in test class we can the same in some other manner 
Schema.DescribeSObjectResult cfrSchema = Schema.SObjectType.Account; 
Map<String,Schema.RecordTypeInfo> AccountRecordTypeInfo = cfrSchema.getRecordTypeInfosByName();
Now to get the recordTypeId we will have to use a method getRecordTypeId.
Id rtId = AccountRecordTypeInfo .get('A').getRecordTypeId(),
Now Use can insert Account Record like

Account Acc = new Account(Name='test',recordtypeid=AccountRecordTypeInfo .get('A').getRecordTypeId());
insert Acc;







Friday, July 11, 2014

Setting up Single Sign-On in Salesforce.

Setting up Single Sign-On in Salesforce.

Hi,

Here is the video to setup a signle sign on salesforce and the documentation is available in https://developer.salesforce.com/page/How_to_Implement_Single_Sign-On_with_Force.com


Is it possible to mass update users licenses in Salesforce?

Is it possible to mass update users licenses in Salesforce

It can possible mass update users licences in salesforce with Data loader or On-Demand Tools.To simply update the User ProfileID. When you mass update users ProfileID the underlying User License gets updated.

Please refer user object details





Thursday, July 10, 2014

How to execute the batch apex class using developer console?

How to execute the batch apex class using developer console?

Example:

Batch Apex Class:
global class accountList implements Database.Batchable<Sobject> {

global Database.querylocator start(Database.BatchableContext BC) {
return Database.getQueryLocator([select Id, Name from Account]);
}
global void execute(Database.BatchableContext BC, list<Account> scope) {
return Database.getQueryLocator(query);
}
global void finish(Database.BatchableContext BC) {
}
}

Execute the Batch Apex Class using Developer Console using below,

accountList objTest = new accountList();
Database.executebatch(objTest);




 
| ,