Salesforce anonymous web service

To make a web service in Salesforce so that it could be accessed without providing user credentials you need to follow steps below :

Create a apex class

@RestResource(urlMapping=’/WebTest/*’)
global class WebTestClass {

    @HttpPost 
    global static String createContact(String Name) {
        CusotmObject c = new CustomObject();
        c.lastname = Name;        
        insert c;
        return ‘Success’;
   }

}

Go to Setup>Develop>Sites and click site label (I am expecting that you have created site in Salesforce or you can follow these instructions https://help.salesforce.com/HTViewHelpDoc?id=sites_setup_overview.htm&language=en_US)

Click Public Access Settings

1

Edit and set following permission for your custom object

2.jpg

Enable your apex class access

3.jpg

To allow only specific IP to access your web service restrict IP range.

4.jpg

to verify if your web service working go to https://www.hurl.it/

and provide payload like this in body {“Name”:”WebServiceTest”}

and add header

“Content-Type” “application/json; charset=UTF-8”

5.jpg

When you launch request it will create new record.

 

MIXED_DML way around

If you are getting MIXED_DML error or “MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): <setupObject>, original object: <notSetupObject>:

In your test class where you are using setup call use below code:

User currentUser= [ select id from User where id = :UserInfo.getUserId() ];
System.runAs (currentUser) {
//You Setup code here
}

Salesforce rubber duck debugging

I was a rubber duck last week. My colleague asked me to sit with him to review code snippet and due to complexity of that code I asked him to explain me what he wrote in Apex. While explaining me he found the cause of the issue himself .

If you are playing cricket getting duck award is last thing you would expect but in programming if you are duck it means you are doing something useful for the project.

According to wiki:

Rubber duck debugging is an informal term used in software engineering for a method of debugging code. The name is a reference to a story in the book The Pragmatic Programmer in which a programmer would carry around a rubber duck and debug their code by forcing themselves to explain it, line-by-line, to the duck. 

duck1duck2

JSForce quick start example

If you have followed the steps mentioned in this blog you can keep reading this.

This blog is for quick start to get data from Salesforce environment.

Execute npm install jsforce command

1

Change these variable values in App.js:

loginUrl
username
password

App.Js Copy code from this pdf and save it as App.js

Run node app.js in command prompt and go to http://localhost:3000/ to view result. It will return number of accounts in the Salesforce environment.