Monday, June 30, 2014

How to Change Salesforce user Password via Developer Console without resetting password?

How  to Change Salesforce user Password via Developer Console  without resetting password?


We need to reset a password or change the password on an user utilizing a 3rd party app in salesforce. This user is there for the app only and not really a physical user in the instance. The email for this user is a person who has left the company or is on vacation but you need the app to work.

Solution:
    You can do this 2 ways:

  1. Change the Email of the user in question
  2. Click on Generate Password and click save.


But lets say you can not change the Email of the user because you should not be the owner of the app or you are not part of the app as an admin. Here is where you change using Developer Console.

Go to Setup and Drop it down to Developer Console or sometimes called the System Log

Within the “Logs” tab you will see a button called “Execute”.

Click on the text box or white space next to it.

It shall open up another window which is titled “Enter Apex Code”

Place in the following SOQL query:

User usr = [select Id from User where      username='test@sfdcsrini.com'];
System.setPassword(usr.Id,’test1234′);

Then click on Execute.
It should bring up a success window and you should be good to go.
Now you may need to log in and make sure everything is ok.




Sunday, June 29, 2014

What is With Sharing in Saleforce and with sharing Uses in APEX Class

What is With Sharing in Saleforce and with sharing Uses in APEX Class


Keyword "with sharing" using: 

Apex scripts generally run in the system context, that is, current user’s profile-based permissions, field-level security, and sharing rules are not taken into account during script execution. Because these rules are not the enforced, developers who use Apex must take care that they do not inadvertently expose sensitive data that would normally be hidden from users by profile-based permissions, field-level security, or organization-wide defaults. They should be particularly careful with Web services, which can be restricted by profile, but execute in system context once they are initiated.
  • Use the with sharing keywords when declaring the class to enforce the sharing rules that apply to the current user.
  • Use the without sharing keywords when declaring the class to ensure that the sharing rules for the current user are not enforced.

Enforcing the current user’s sharing rules can impact:
  • SOQL and SOSL queries. A query may return fewer rows than it would operating in system context.
  • DML operations. An operation may fail because the current user does not have the correct permissions. For example, if the user specifies a foreign key value that exists in the organization, but which the current user does not have access to.
*Note: If a class is not declared as either with or without sharing, the current sharing rules remain in effect. This means that if the class is called by a class that has sharing enforced, then sharing is enforced for the called class.



Saturday, June 28, 2014

What is Transient Variable in Salesforce?

What is  Transient Variable in Salesforce?


Transient Variable in Sales force:
        Transient keyword to declare instance variable that can not be saved and should not transmitted as part of view state for visual force page.

View state maintains state of the Database between request and visual force page.

     Given below example is use of transient variable where we have created two Date Time and populating. Refresh button is using for refresh the VF page however only time T2 will change at because of this is declare as transient.

VF Page:

<apex:page controller="ExampleController">
  T1: {!t1} <br/>
  T2: {!t2} <br/>
  <apex:form >
    <apex:commandLink value="refresh"/>
  </apex:form>
</apex:page>

Controller :

public class ExampleController {

    DateTime t1;
    transient DateTime t2;

    public String getT1() {
        if (t1 == null) t1 = System.now();
        return '' + t1;
    }

    public String getT2() {
        if (t2 == null) t2 = System.now();
        return '' + t2;
    }
}







Thursday, June 26, 2014

What is Order of execution of apex code?

What is Order of execution of apex code?


Order of execution of apex code:
1. Before triggers:
     If we update child records in before triggers then parent record automatically update.
2. Validation rules:
 If validation matches the criteria then error will populate in same page.
Runs most system validation steps again, such as verifying that all required fields have a non-null value, and runs any user-defined validation rules. The only system validation that Salesforce does not run a second time (when the request comes from a standard UI edit page) is the enforcement of layout-specific rules.
*Note: Saves the record to the database, but does not commit yet.
3. After Triggers:
4. Assignment Rules:
5. Auto Response Rules:
6. Workflow Rules:
If there are workflow field updates, updates the record again.
7. Escalation Rules:
       If the record contains a roll-up summary field or is part of a cross-object workflow, performs calculations and updates the roll-up summary field in the parent record. Parent record goes through save procedure.
     If the parent record is updated, and a grand-parent record contains a roll-up summary field or is part of a cross-object workflow, performs calculations and updates the roll-up summary field in the parent record. Grand-parent record goes through save procedure.
Executes Criteria Based Sharing evaluation.
Commits all DML operations to the database.

Executes post-commit logic, such as sending emails







Wednesday, June 25, 2014

What are Email Services in Salesforce and How to create Email Services?

What are Email Services in Salesforce and How to create  Email Services?


Email services:
 Email services are automated processes that use the Apex classes to process the contents, headers, and attachments of inbound email.

For example, you can create an email service that automatically creates contact records based on contact information in messages. Each email service has one or more email service addresses that can receive messages for processing.

To use the email services, click Your Name ➤ Setup ➤ Develop ➤ Email Services.
Given below email service class inserts Contact which is declare in setting of Email Services.


Class for Email Services:
global class ProcessJobApplicantEmail implements Messaging.InboundEmailHandler {
  global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
    Messaging.InboundEnvelope envelope) {
    Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
    Contact contact = new Contact();
    contact.FirstName = email.fromname.substring(0,email.fromname.indexOf(' '));
    contact.LastName = email.fromname.substring(email.fromname.indexOf(' '));
    contact.Email = envelope.fromAddress;
    insert contact;
    System.debug('====> Created contact '+contact.Id);
    if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
      for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) {
        Attachment attachment = new Attachment();
        // attach to the newly created contact record
        attachment.ParentId = contact.Id;
        attachment.Name = email.binaryAttachments[i].filename;
        attachment.Body = email.binaryAttachments[i].body;
        insert attachment;
      }
    }
     return result;
  }
}


Test Class for Email Service:

@isTest
private class EmailTest{
static testMethod void testMe() {
  // create a new email and envelope object
  Messaging.InboundEmail email = new Messaging.InboundEmail() ;
  Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
  // setup the data for the email
  email.subject = 'Test Job Applicant';
  email.fromname = 'FirstName1 LastName1';
  env.fromAddress = 'raees.sabir@accenture.com';
  // add an attachment
  Messaging.InboundEmail.BinaryAttachment attachment = new Messaging.InboundEmail.BinaryAttachment();
  attachment.body = blob.valueOf('my attachment text');
  attachment.fileName = 'textfile.txt';
  attachment.mimeTypeSubType = 'text/plain';
  email.binaryAttachments =
    new Messaging.inboundEmail.BinaryAttachment[] { attachment };
  // call the email service class and test it with the data in the testMethod
  ProcessJobApplicantEmail emailProcess = new ProcessJobApplicantEmail();
  emailProcess.handleInboundEmail(email, env);
  // query for the contact the email service created
  Contact contact = [select id, firstName, lastName, email from contact
    where firstName = 'FirstName1' and lastName = 'LastName1'];
  System.assertEquals(contact.firstName,'FirstName1');
  System.assertEquals(contact.lastName,'LastName1');
  System.assertEquals(contact.email,'raees.sabir@accenture.com');
  // find the attachment
  Attachment a = [select name from attachment where parentId = :contact.id];
  System.assertEquals(a.name,'textfile.txt');
 }

}






Tuesday, June 24, 2014

What are the issues while generating the apex class from WSDL Document in salesforce?

What are the issues  while generating the apex class from WSDL Document in salesforce?


It’s difficult to connect to external webservices without the following basic features.

1) Salesforce doesn’t support the WSDL SOAP1.2 Version.
2) Without a wsdl binding (wsdl:binding), it is not possible to call a Web Service.
3) It does not support multiple WSDL bindings (wsdl:binding).
4) Inheritance is not supported in wsdl to APEX conversion
5) Enumeration Object types are not supported.
6) WSDL Import functionality is not supported.


How to generate the Apex class using WSDL file in salesforce?

An Apex class can be automatically generated from a WSDL file that is stored on a local system or network
Below is the path To access this functionality:

1) from Setup, click Develop -> Apex Classes.
2) Click Generate from WSDL button.
3) Click Browse to navigate to a WSDL file on your local system or network.
4) Done. 

Monday, June 23, 2014

How to get field data types for each fields in Salesforce objects?

How to get field data types for each fields in Salesforce objects?


We can get the all the standard and custom objects fields data types using the getGlobalDescribe, getDescribe, getType.

String objType=’Account’;
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Schema.SObjectType leadSchema = schemaMap.get(objType);
Map<String, Schema.SObjectField> fieldMap = leadSchema.getDescribe().fields.getMap();

for (String fieldName: fieldMap.keySet()) {

//get all the fields label for Account Object
String fieldLabel = fieldMap.get(fieldName).getDescribe().getLabel();

//get data types for each fields

Schema.DisplayType fielddataType = fieldMap.get(fieldName).getDescribe().getType();
if(fielddataType != Schema.DisplayType.TextArea) {
build your logic if the Field data type is TextArea
}
if(fielddataType != Schema.DisplayType.String) {
build your logic if the Field data type is String
}

if(fielddataType != Schema.DisplayType.Integer) {

build your logic if the Field data type is Integer
}

if(fielddataType != Schema.DisplayType.DateTime) {

build your logic if the Field data type is DateTime
}

}


Here Schema.DisplayType enum value is returned by the field describe result’s getType method.

Click here for more details:

http://www.salesforce.com/us/developer/docs/dbcom_apex250/Content/apex_methods_system_fields_describe.htm#apex_displaytype



How to write message class in apex controllers and Visualforce pages?

How to write message class in apex controllers and Visualforce pages?

Message Class:
Whenever we perform validations in our custom or extension controllers, we would need to display error messages if the user enters invalid data and these error messages can be constructed and displayed with the help of this Message class.

If your application uses a custom controller or extension, you must use the message class for collecting errors.

Syntax:
In a custom controller or controller extension, you can instantiate a Message in one of the following ways:

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity, summary);

where ApexPages.severity is the enum that is determines how severe a message is, and summary is the String used to summarize the message.
For example:

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'my error msg');

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity, summary, detail);

where ApexPages.severity is the enum that is determines how severe a message is, summary is the String used to summarize the message, and detail is the String used to provide more detailed information about the error.

ApexPages.Severity Enum: 
Using the ApexPages.Severity enum values, specify the severity of the message. The following are the valid values:

1. CONFIRM
2. ERROR
3. FATAL
4. INFO
5. WARNING

All enums have access to standard methods, such as name and value.


Page Reference Class:
A PageReference is a reference to an instantiation of a page. Among other attributes, PageReferences consist of a URL and a set of query parameter names and values.
Use a PageReference object:

1. To view or set query string parameters and values for a page
2. To navigate the user to a different page as the result of an action method

Syntax:
     In a custom controller or controller extension, you can refer to or instantiate a PageReference in one of the following ways:

Page.existingPageName     

Refers to a PageReference for a Visualforce page that has already been saved in your organization. By referring to a page in this way, the platform recognizes that this controller or controller extension is dependent on the existence of the specified page and will prevent the page from being deleted while the controller or extension exists.

PageReference pageRef = new PageReference('partialURL');

Creates a PageReference toany page that is hosted on the Force.com platform. For example, setting 'partialURL' to'/apex/HelloWorld' refers to the Visualforcepage located at http://mySalesforceInstance/apex/HelloWorld. Likewise, setting 'partialURL' to '/' + 'recordID' refers to the detail page for the specified record.

This syntax is less preferable for referencing other Visualforce pages than Page.existingPageName because the PageReference is constructed at runtime, rather than referenced at compile time. Runtime references are not available to the referential integrity system. Consequently, the platform doesn't recognize that this controller or controller extension is dependent on the existence of the specified page and won't issue an error message to prevent user deletion of the page.

PageReference pageRef = new PageReference('fullURL');

Creates a PageReference for an external URL. For example:

PageReference pageRef = new PageReference('http://www.google.com');

You can also instantiate a PageReference object for the current page with the currentPage ApexPages method. For example:

PageReference pageRef = ApexPages.currentPage();


Select Option Class:

A SelectOption object specifies one of the possible values for a Visualforce selectCheckboxes, selectList, or selectRadio component. It consists of a label that is displayed to the end user, and a value that is returned to the controller if the option is selected. A SelectOption can also be displayed in a disabled state, so that a user cannot select it as an option, but can still view it.

Syntax:
     In a custom controller or controller extension, you can instantiate a SelectOption in one of the following ways:

SelectOption option = new SelectOption(value, label, isDisabled);

where values is the String that is returned to the controller if the option is selected by a user,label is the String that is displayed to the user as the option choice, andis Disabled is a Boolean that, if true, specifies that the user cannot select the option, but can still view it.

SelectOption option = new SelectOption(value, label);

where value is the String that is returned to the controller if the option is selected by a user, and label is the String that is displayed to the user as the option choice. Because a value for is Disabled is not specified, the user can both view and select the option.

Standard Controller Class:
StandardController objects reference the pre-built Visualforce controllers provided by salesforce.com. The only time it is necessary to refer to a StandardController object is when defining an extension for a standard controller. StandardController is the data type of the single argument in the extension class constructor.

Syntax:
You can instantiate a StandardController in the following way:
ApexPages.StandardController sc = new ApexPages.StandardController(sObject);







Message Class:
                     Whenever we perform validations in our custom or extension controllers, we would need to display error messages if the user enters invalid data and these error messages can be constructed and displayed with the help of this Message class.
 If your application uses a custom controller or extension, you must use the message class for collecting errors.


Syntax:

In a custom controller or controller extension, you can instantiate a Message in one of the following ways:
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity, summary);
        where ApexPages.severity is the enum that is determines how severe a message is, and summary is the String used to summarize the message.
For example:
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'my error msg');

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity, summary, detail);
           where ApexPages.severity is the enum that is determines how severe a message is, summary is the String used to summarize the message, and detail is the String used to provide more detailed information about the error.

ApexPages.Severity Enum:


           Using the ApexPages.Severity enum values, specify the severity of the message. The following are the valid values:
1. CONFIRM
2. ERROR
3. FATAL
4. INFO
5. WARNING
All enums have access to standard methods, such as name and value.

Page Reference Class:
A PageReference is a reference to an instantiation of a page. Among other attributes, PageReferences consist of a URL and a set of query parameter names and values.
Use a PageReference object:


    1. To view or set query string parameters and values for a page
    2. To navigate the user to a different page as the result of an action method

      Syntax:

                   In a custom controller or controller extension, you can refer to or instantiate a PageReference in one of the following ways:

      Page.existingPageName       
                     Refers to a PageReference for a Visualforce page that has already been saved in your organization. By referring to a page in this way, the platform recognizes that this controller or controller extension is dependent on the existence of the specified page and will prevent the page from being deleted while the controller or extension exists.
      PageReference pageRef = new PageReference('partialURL');
                   Creates a PageReference toany page that is hosted on the Force.com platform. For example, setting 'partialURL' to'/apex/HelloWorld' refers to the Visualforcepage located at http://mySalesforceInstance/apex/HelloWorld. Likewise, setting 'partialURL' to '/' + 'recordID' refers to the detail page for the specified record.
        This syntax is less preferable for referencing other Visualforce pages than Page.existingPageName because the PageReference is constructed at runtime, rather than referenced at compile time. Runtime references are not available to the referential integrity system. Consequently, the platform doesn't recognize that this controller or controller extension is dependent on the existence of the specified page and won't issue an error message to prevent user deletion of the page.
      PageReference pageRef = new PageReference('fullURL');

      Creates a PageReference for an external URL. For example:


      PageReference pageRef = new PageReference('http://www.google.com');
      You can also instantiate a PageReference object for the current page with the currentPage ApexPages method. For example:
      PageReference pageRef = ApexPages.currentPage();

      Select Option Class:
      A SelectOption object specifies one of the possible values for a Visualforce selectCheckboxes, selectList, or selectRadio component. It consists of a label that is displayed to the end user, and a value that is returned to the controller if the option is selected. A SelectOption can also be displayed in a disabled state, so that a user cannot select it as an option, but can still view it.
      Syntax:
                In a custom controller or controller extension, you can instantiate a SelectOption in one of the following ways:
      SelectOption option = new SelectOption(value, label, isDisabled);

              where values is the String that is returned to the controller if the option is selected by a user,label is the String that is displayed to the user as the option choice, andis Disabled is a Boolean that, if true, specifies that the user cannot select the option, but can still view it.
      SelectOption option = new SelectOption(value, label);

             where value is the String that is returned to the controller if the option is selected by a user, and label is the String that is displayed to the user as the option choice. Because a value for is Disabled is not specified, the user can both view and select the option.

      Standard Controller Class:
               StandardController objects reference the pre-built Visualforce controllers provided by salesforce.com. The only time it is necessary to refer to a StandardController object is when defining an extension for a standard controller. StandardController is the data type of the single argument in the extension class constructor.
      Syntax:
      You can instantiate a StandardController in the following way:
      ApexPages.StandardController sc = new ApexPages.StandardController(sObject);
      - See more at: http://sfdcgurukul.blogspot.in/2013/07/message-class.html#sthash.uZaP6FFF.dpuf
      Message Class:
                           Whenever we perform validations in our custom or extension controllers, we would need to display error messages if the user enters invalid data and these error messages can be constructed and displayed with the help of this Message class.
       If your application uses a custom controller or extension, you must use the message class for collecting errors.


      Syntax:

      In a custom controller or controller extension, you can instantiate a Message in one of the following ways:
      ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity, summary);
              where ApexPages.severity is the enum that is determines how severe a message is, and summary is the String used to summarize the message.
      For example:
      ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'my error msg');

      ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity, summary, detail);
                 where ApexPages.severity is the enum that is determines how severe a message is, summary is the String used to summarize the message, and detail is the String used to provide more detailed information about the error.

      ApexPages.Severity Enum:


                 Using the ApexPages.Severity enum values, specify the severity of the message. The following are the valid values:
      1. CONFIRM
      2. ERROR
      3. FATAL
      4. INFO
      5. WARNING
      All enums have access to standard methods, such as name and value.

      Page Reference Class:
      A PageReference is a reference to an instantiation of a page. Among other attributes, PageReferences consist of a URL and a set of query parameter names and values.
      Use a PageReference object:


        1. To view or set query string parameters and values for a page
        2. To navigate the user to a different page as the result of an action method

          Syntax:

                       In a custom controller or controller extension, you can refer to or instantiate a PageReference in one of the following ways:

          Page.existingPageName       
                         Refers to a PageReference for a Visualforce page that has already been saved in your organization. By referring to a page in this way, the platform recognizes that this controller or controller extension is dependent on the existence of the specified page and will prevent the page from being deleted while the controller or extension exists.
          PageReference pageRef = new PageReference('partialURL');
                       Creates a PageReference toany page that is hosted on the Force.com platform. For example, setting 'partialURL' to'/apex/HelloWorld' refers to the Visualforcepage located at http://mySalesforceInstance/apex/HelloWorld. Likewise, setting 'partialURL' to '/' + 'recordID' refers to the detail page for the specified record.
            This syntax is less preferable for referencing other Visualforce pages than Page.existingPageName because the PageReference is constructed at runtime, rather than referenced at compile time. Runtime references are not available to the referential integrity system. Consequently, the platform doesn't recognize that this controller or controller extension is dependent on the existence of the specified page and won't issue an error message to prevent user deletion of the page.
          PageReference pageRef = new PageReference('fullURL');

          Creates a PageReference for an external URL. For example:


          PageReference pageRef = new PageReference('http://www.google.com');
          You can also instantiate a PageReference object for the current page with the currentPage ApexPages method. For example:
          PageReference pageRef = ApexPages.currentPage();

          Select Option Class:
          A SelectOption object specifies one of the possible values for a Visualforce selectCheckboxes, selectList, or selectRadio component. It consists of a label that is displayed to the end user, and a value that is returned to the controller if the option is selected. A SelectOption can also be displayed in a disabled state, so that a user cannot select it as an option, but can still view it.
          Syntax:
                    In a custom controller or controller extension, you can instantiate a SelectOption in one of the following ways:
          SelectOption option = new SelectOption(value, label, isDisabled);

                  where values is the String that is returned to the controller if the option is selected by a user,label is the String that is displayed to the user as the option choice, andis Disabled is a Boolean that, if true, specifies that the user cannot select the option, but can still view it.
          SelectOption option = new SelectOption(value, label);

                 where value is the String that is returned to the controller if the option is selected by a user, and label is the String that is displayed to the user as the option choice. Because a value for is Disabled is not specified, the user can both view and select the option.

          Standard Controller Class:
                   StandardController objects reference the pre-built Visualforce controllers provided by salesforce.com. The only time it is necessary to refer to a StandardController object is when defining an extension for a standard controller. StandardController is the data type of the single argument in the extension class constructor.
          Syntax:
          You can instantiate a StandardController in the following way:
          ApexPages.StandardController sc = new ApexPages.StandardController(sObject);
          - See more at: http://sfdcgurukul.blogspot.in/2013/07/message-class.html#sthash.uZaP6FFF.dpuf

           
          | ,