Sunday 4 May 2014

Java Script remoting Salesforce!!!

Hi Guys,'

Last week I was playing with Java script remoting.

What is java script remoting?

In very simple words , use java script remoting  in Visualforce to call methods in Apex controllers from javaScript.

So , lemme straight away take you to scenario on which I was working last week.

Say, You want to display a picklist value that you have just selected from a custom field of account object. it is one of the scenario where java script remoting comes handy and useful.
Here , is the code for you:-


Apex Controller:-

global with sharing class CopyFieldUsingRemoteActions{

    public CopyFieldUsingRemoteActions() {

    }
      
    Account acct = [SELECT Id, AnkushTest__SLA__c from Account WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    public static Account account { get; set; }
    
   @RemoteAction
    global static account getAccountSLA(string Fname) {
        account= [Select id , name,TestMap__c,AnkushTest__samplePrefix__c, AnkushTest__SLA__c from account where AnkushTest__SLA__c =: Fname limit 1];
        return account;
    }
    
}
lemme first explain What I have done. So I have tried to querry Account id which is current page ID using :ApexPages.currentPage().getParameters().get('id')] which acytually returns the ID of 
It actually returns the Id of the current  page .  You have to tell controller that a method is going top ber remoting action so you can use in Visualforce. Also , I have passed a parameter in of string type . 


Now , lets go to visualforce section :-


<apex:page Controller="CopyFieldUsingRemoteActions">
     <script>
    function getRemoteAccount(){
          alert(document.getElementById("j_id0:formid:pb:disc:myText").value);
     var test1=document.getElementById("j_id0:formid:pb:disc:myText").value
        Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.CopyFieldUsingRemoteActions.getAccountSLA}',
            test1,
            function(result, event){
                if (event.status) {
                    alert(document.getElementById("j_id0:formid:pb:acctSearch"));
                    // Get DOM IDs for HTML and Visualforce elements like this
                    document.getElementById("j_id0:formid:pb:acctSearch").innerHTML = result.AnkushTest__SLA__c ;       
                } else if (event.type === 'exception') {
                    document.getElementById("j_id0:formid:pb:acctSearch").innerHTML = 
                        event.message + "<br/>\n<pre>" + event.where + "</pre>";
                        alert(event.message);
                } else {
                    document.getElementById("j_id0:formid:pb:acctSearch").innerHTML = event.message;
                     alert('FAil');
                    
                }
            }, 
            {escape: true}
        );
    }
    </script>
    <apex:form id="formid">
        <apex:pageBlock mode="edit" id="pb">
            <apex:pageBlockButtons >
               
                <button onclick="getRemoteAccount()">show </button>
            </apex:pageBlockButtons>
             <apex:outputText id="acctSearch" label="Output"/>
            <apex:pageBlockSection title="My Content Section" columns="2" id="disc" >
                <apex:inputField value="{!account.AnkushTest__SLA__c }" id="myText" onblur="alert('hii');getRemoteAccount();"/>
                
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>


All the java script functionality should be defined under script tag. Now , I have defined a function name getRemoteAccount(). Interesting thing to know that in alert and in that i have used id from inspect element where i want it to be get print. 




Happy Coding!!

Cheers
ankushsalesforce@gmail.com





































Saturday 3 May 2014

Trigger On ContentDocument object Salesforce

Trigger On ContentDocument object Salesforce!


Hi guys!

These days am practicing out Triggers . In-fact I must say it is one of the  best exercise to  build your logic and get familiar with how coding is done in salesforce. 

So, Lemme First explain you the scenario.:-

 Say You want to write a trigger to insert a feed in chatter which could be a link to the library when I create a content  or edit it anything in a  library..

it says If when ever you add or edit any file in library then its link should be posted as link on chatter. 
Its very simple one should be familiar with contentDocument object as this object is not available in salesforce UI. But it is available on background of salesforce.

So you must be thinking then how do I got to know about this one . Well,  Keep exploring. Anyways . I got to know about this one through Developer console. Also from there you can create the trigger. 

Here , is the simple code For you ...


trigger PublishlinkOnChatter on ContentDocument (After insert,After Update) {
    list<FeedItem> lst=new List<feedItem>();
    for(ContentDocument cd:trigger.new){
            FeedItem fi= new FeedItem();
            fi.LinkUrl='https://na15.salesforce.com/'+cd.LatestPublishedVersionId;
            fi.parentid=userinfo.getUserID();
            fi.body='Modified the Content';
            lst.add(fi);
    }
        insert lst;
}


happy Coding!!!


Cheers!!!
ankushsalesforce@gmail.com


















oAuth Use Case -  Headless API Explained in the Easiest way Wondering why? and When we use the Headless API flow Use case example: Imagine a...