Be Good & Do Good!

Tag: Salesforce (Page 9 of 11)

Apex – Update “non-setup” objects from a “setup” object like User

Dilemma

As this page of the Apex docs indicates, you can’t just make a User trigger that updates a Contact or an Account, because it’s forbidden to modify those “non-setup” objects from a “setup” object like User.

Solution

Fortunately there is a simple solution: the @future annotation.  The @future annotation allows you to create Apex that runs asynchronously at some point in the future (in my tests I’ve found that it runs immediately, or at least very soon after the trigger executes).  Methods that use @future are subject to different limits than normal trigger operations because @future methods don’t hold up the trigger (and therefore the entire user experience) while they’re working.  Therefore @future methods are often used to perform long-running web service callouts from triggers asynchronously.  However, they can also be handy for a case like ours, where we want to update an object that we’re not normally eligible to update.

Calling a method that has been marked as @future is just like calling any other static method.  Here’s my trigger, short and sweet:

trigger UpdateContactFromPortalUser on User (after update) {
	//We only want to run on the single item that the user edited
	if (Trigger.new.size()==1) {
		User u =  Trigger.new[0];
		//And only if it's a portal user
		if (u.ContactId!=null) {
		UpdateContactFromPortalUser.updateContacts(u.Id);
		}
	}
}

Let’s see how that works.  In the example given here, I just update a couple of fields from the user record: the name, the email address, and the title.

global class UpdateContactFromPortalUser {
    @future
    public static void updateContacts(String userId) {
    	User u = [select ContactId,Email,FirstName,LastName,Title
    				from User
    				where Id=:userId];

    	if (u!=null && u.ContactId!=null) {
	    	Contact c = new Contact(Id=u.ContactId);

			c.Email = u.Email;
			c.FirstName=u.FirstName;
			c.LastName=u.LastName;
			c.Title=u.Title;
			update c;
		}

Source :  http://blogs.salesforce.com/support/2009/01/index.html

Salesforce – Render visualforce page without Field Level Security

Scenario :
We are building a Visualforce page for creating a case. Now there are fields in case which are read only for all profiles except System Adminstrator. I need the user to enter the value for this field and so I created my page with a controller and not a standard controller. Also in my controller we have added the modifier as without sharing.

Now when we log in through another user, we still see the field as read only on the page. However from the controller we are able to insert value or update the field. It seems to me that the page is running in User mode following the user profile permissions and field level security while the controller is running in Sytem mode. Is this the intended behaviour or are we missing something

Following are few of the possible solutions….

Solution 1

It is an intented behaviour, If the fields rendered on the page is strongly binded (i.e Value = {!ObjectName.Fieldname}).
Why not create input/HTML fields and bind them using getter setter method (i.e you take input in a normal text field and bind it to the actual field in the controller). As these input Html fields are not binded to an object these fields will be editable regardless of profiles.

Solution 2

Create a Custom Object and assign permissions to this object to all profiles. Now render fields of that custom object, but in your controller don’t insert this custom object. Insert the actual records.
Use this custom object to generate the lookups and date pickers
The controller will be standard controller of this custom object with extensions.
So this object is used just to render fields ….

Solution 3

Make the fields read only at the page layout level and not at the profile level.

Salesforce : Apex : Covert DateTime to Date

I look through the documentation and didn’t see a system method that converts a date time field to a date. There is one for formula fields but is there one for Apex?

Solution : Just create a date newInstance() and pass in just the year,month,day from the dateTime object.

Datetime dateTimetemp = System.now();

Date dateTemp = Date.newInstance(dateTimetemp.year(),dateTimetemp.month(),dateTimetemp.day());

Salesforce Next generation Partner Portal 2.0 Launched

Salesforce today launched a complete new and next generation Partner Portal 2.0.

Log in today using your existing partner portal credentials to:

My favorites are ..
1. Content – It is the best possible location to find any Salesforce presentation, cheat sheets or certification guides … a complete repository of Salesforce documents.
2. Partner Calendar – To view the latest Salesforce.com events and evaluate sponsorship opportunities


Partner Portal 2.0 – Getting Started from John Richter on Vimeo.

Cast Iron Integration for Salesforce.com

The No Software Approach to Integration
Cast Iron Systems created a fast and simple solution specifically for integrating salesforce.com with other applications.

Connect Salesforce.com
Just as the No Software experience of salesforce.com has simplified sales automation, Cast Iron’s No Software approach has dramatically simplified application migration and integration for salesforce.com customers.

Features
Cast Iron solution is AppExchange certified and provides the following features

  • Migration and Data Quality
  • Data Profiling
    Intelligent Data Cleansing
    Data Enrichment

  • Integration and Extraction
  • Connectivity
    Transformation
    Workflow
    Management

    Read more on Cast Iron Integration for Salesforce.com @ CastIron.com

    Salesforce : Access Apex Webservices from Java/NET

    Many a times we define Apex Webservices inside Salesforce to perform operations on Salesforce data from external world. Anyhow always we are left on how to access these custom defined web services from a WSDL consumer (JAVA/ NET / …)

    If we start consuming Parner WSDL only, then we do not get the apex webservice defintions.  Partner WSDL doesn’t contain your custom defined webservice definitions. You’ll also have to cosume Apex Webservice WSDL to call your webservice.

    So this involves following 2 steps …

    1. Step1 : Consume Partner WSDL  and then perform Login call to SFDC
    2. binding1 = (SoapBindingStub) new SforceServiceLocator().getSoap();
      loginResult = binding1.login(“username”,”PwdToken”);
      SessionHeader sh = new SessionHeader();
      sh.setSessionId(loginResult.getSessionId());

    3. Step2 : Consume Apex Webservice WSDL and Set above fetched SessionId as the sessionHeader on the Apex Webservice WSDL stub and then call the required methods.
    4. binding2 = (checkForLeadNumberBindingStub) new checkForLeadNumberServiceLocator().getcheckForLeadNumber();
      binding2.setHeader(new checkForLeadNumberServiceLocator().getServiceName().getNamespaceURI(), “SessionHeader”, sh);
      binding2.extractLeadId(“123456″,”123456”);

    Apex : How to throw a custom built Exception

    If you want to throw an exception in an apex class, following code will help ..

    First you have to create your own Exception class like this:

    public class applicationException extends Exception {}

    Then you can throw an exception using following syntax

    throw new applicationException(‘House Full’);

    For e.g.,

    public class MyException extends Exception {}
    try {
    Integer i;
    // Your code here
    if (i < 5) throw new MyException();
    } catch (MyException e) {
    // Your MyException handling code here
    }

    Force.com webinar – Introduction to the Email Services

    Last week a webinar was organized on Introduction to the Email Services! The webinar looked at:

    :: How to create email services to integrate email with Force.com
    :: How to build a receiving email service
    :: How to send emails and track them
    :: Email security and Visualforce email templates

    The webinar was recorded, and it is available together with a set of resources:

    :: View the recorded webinar @ http://wiki.developerforce.com/index.php/Tech_Talk_Series:_Introduction_to_the_Email_Services_on_Force.com

    :: Check out the webinar FAQ @ http://wiki.developerforce.com/index.php/Tech_Talk:_Introduction_to_the_Email_Services_on_Force.com_FAQ

    Using Dynamic Apex to retrieve Picklist Values

    With the increasing popularity and adoption of the Force.com platform, there’s a huge growth in custom Visualforce pages, in particular pages which require interaction with multiple objects. One requirement that comes up regularly is how to display picklist values from an object when the page is using a custom controller.

    In order to achieve this, we have to look at the very powerful and useful Dynamic Apex Describe Information capabilities.  These capabilities allow us to interrogate a field or sObject, and describe their characteristics. In our requirement above, we want to describe the fields on an object, in particular, a picklist field to determine its list of values.

    Read Complete Article by Quinton Wall @ Salesforce Blog

    « Older posts Newer posts »