Tuesday, 21 July 2015

Action Status Rerendering in salesforce vf page

<style>
.custPopup{
    background-color: white;
    border-width: 0px;
    border-radius:10px;
    z-index: 9999;
    left: 50%;
    padding:20px;
    position: absolute;
    /* These are the 3 css properties you will need to change so the popup 
    displays in the center of the screen. First set the width. Then set 
    margin-left to negative half of what the width is. You can add 
    the height property for a fixed size pop up if you want.*/
    margin-left: -100px;
    top:40%;
}
.popupBackground{
    background-color:black;
    opacity: 0.30;
    filter: alpha(opacity = 30);
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    z-index: 9998;
}
</style>

 <apex:actionStatus id="fetching">
        <apex:facet name="start">
             <apex:outputPanel id="tstpopup">
                    <apex:outputPanel styleClass="popupBackground" layout="block"/>
                    <apex:outputPanel styleClass="custPopup" layout="block">
                        <img src="/img/loading24.gif" style="vertical-align:middle; horizontal-align:middle"/> 
                        <span>Please wait...</span>  
                    </apex:outputPanel>
              </apex:outputPanel> 
        </apex:facet>
    </apex:actionStatus>

<apex:commandButton reRender="fetching"/>

Friday, 17 July 2015

Get related objects or related list object


You can get related objects with function :

public static map<string,string> getRelatedObjects(string masterObjectName){
map<string,string> relatedObjectsMap = new map<string,string>();
list<Schema.Childrelationship> relatedObjectsList = Schema.getGlobalDescribe().get(masterObjectName).getdescribe().getChildRelationships();

for (Schema.Childrelationship  relatedObject : relatedObjectsList) {
if(relatedObject.getChildSObject().getDescribe().isUpdateable()
&&
relatedObject.getChildSObject().getDescribe().getKeyPrefix()!=null
&&
!relatedObject.getChildSObject().getDescribe().isCustomSetting()
&&
relatedObject.getChildSObject().getDescribe().isCreateable()
)
relatedObjectsMap.put(relatedObject.getChildSObject().getDescribe().getName(),relatedObject.getChildSObject().getDescribe().getLabel());
   }
return relatedObjectsMap;
}

Wednesday, 8 July 2015

Remote Function with parameter in apex

Class code


public class MyController {

    @RemoteAction
    public static List<Account> getAccountsList(String userId, String value1, String value2) {
 
        return [SELECT Id FROM Account WHERE  id =: userId];
    }
}

Page Script

<script type="text/javascript">
    var valueTakenFromSomewhere = 'This will be value1 in the remote action';
    var valueTakenFromSomewhereElse = 'This will be value2 in the remote action';

    Visualforce.remoting.Manager.invokeAction(
        '{!$RemoteAction.MyController.getAccountsList}',
        '{!$User.Id}',
        valueTakenFromSomewhere,
        valueTakenFromSomewhereElse,
        'This will be value2 in the remote action',
        function(result, event) {
            if (event.status) {
                //do whatever rendering of the list you want
            } else if (event.type === 'exception') {
                //handle errors
            } else {
                //handle unknown responses
            }
        },
        {escape: true}
    );
</script>

Saturday, 27 June 2015

How to call rest api webservices

Step 1 : Initially create connected app in salesforce.
here:
callback URL: https://login.salesforce.com/services/oauth2/callback
selected OAuthScope:Full access (full)

Now you can use consumer key, consumer secret for rest api.

Step 2: How to get access token:

Method:POST
URL:https://login.salesforce.com/services/oauth2/token
client_id:consumer Key from salesforce connected app
client_secret:Consumer secret from salesforce connected app
grant_type:password
username:restAPi@soliant.com (user id)
password:password + security token



Output
{
    "id": "https://login.salesforce.com/id/.....",
    "issued_at": "1435335550081",
    "token_type": "Bearer",
    "instance_url": "https://ap2.salesforce.com",
    "signature": "-----xyz-----",
    "access_token": -------xyz------"
}


Step 3 : How to call Rest API from Postman or other rest client
Insert / update

Method:POST
URL:https://ap2.salesforce.com/services/apexrest/webservices
 (<instance_url>/services/apexrest/< urlMapping of class>)
Header1:Authorization
Value1:Bearer <space> <access_token>
Header2:Content-Type
Value2:application/json




Now select raw and write json in it like:

For insert contact
{
    "JSONString": {
        "attributes": {
            "type": "Contact"
        },
        "FirstName": "John",
        "LastName": "test",
        "Email": " testing @gmail.com",
        "Phone": "12121212"
    }
}

For update contact:
{
    "JSONString": {
        "attributes": {
            "type": "Contact"
        },
        "id":"00328000003KfhB"
        "FirstName": "John",
        "LastName": "testing",
        "Email": "testing@gmail.com",
        "Phone": "12121212"
    }
}