Saturday 20 May 2017

Insert Account and show popup after insert Account using JavaScript Remoting

1. I create a visualforce page to insert the account using JavaScript Remoting and display popup when insert successfully otherwise show popup if account is already exist.

2. Pass the input text value as a parameter in remoting function to class method.
3. Controller method checks the account is already exist or not , if exist it return account value if not then it insert the account and return inserted account.
4. In visualforce page, record return in remoting function as a result and event.
5. If method works fine then  event.status return true. Then  result checks that the value return in account is same as value pass in the parameter then it means account is already exist other account inserted successfully.
6. Another text box to search your already exist account and new inserted account.

Click For Demo

Visualforce Page:

<apex:page controller="AccountRemoterInsert" sidebar="false" showHeader="false">
    <script type="text/javascript">
    
        // JavaScript Remoting to insert the account and show popup messages.
        function InsertRemoteAccount() 
        {
            var accountName = document.getElementById('acctSearch').value;
            var insAccountName = document.getElementById('acct').value;
            
            Visualforce.remoting.Manager.invokeAction(
                '{!$RemoteAction.AccountRemoterInsert.InsertAccountDetail}',
                insAccountName, 
                function(result, event)
                {
                    if (event.status) 
                    {
                        
                        // Get DOM IDs for HTML and Visualforce elements like this
                        if(result==true)
                        {
                            alert('Account already exist,Search below');
                        }
                        else
                        {
                            alert('Account has been inserted successfully,Search Below.');
                        }
                        
                    } else if (event.type === 'exception') {
                        document.getElementById("responseErrors").innerHTML = 
                            event.message + "<br/>\n<pre>" + event.where + "</pre>";
                    } else {
                        document.getElementById("responseErrors").innerHTML = event.message;
                    }
                }, 
                {escape: true}
            );
        }
        
        // JavaScript Remoting for the Accounts
        function getRemoteAccount() 
        {
            var accountName = document.getElementById('acctSearch').value;
    
            Visualforce.remoting.Manager.invokeAction(
                '{!$RemoteAction.AccountRemoterInsert.getAccountDetail}',
                accountName, 
                function(result, event)
                {
                    if (event.status) 
                    {
                        document.getElementById('remoteAcctId').innerHTML = result.Name
                        document.getElementById(
                            "{!$Component.block.blockSection.secondItem.acctNumEmployees}"
                            ).innerHTML = result.Phone;
                    } else if (event.type === 'exception') {
                        document.getElementById("responseErrors").innerHTML = 
                            event.message + "<br/>\n<pre>" + event.where + "</pre>";
                    } else {
                        document.getElementById("responseErrors").innerHTML = event.message;
                    }
                }, 
                {escape: true}
            );
        }
    </script>

    <input id="acct" type="text"/>
    <button onclick="InsertRemoteAccount()">Insert Account</button>
    
    <br/><br/>
    <input id="acctSearch" type="text"/>
    <button onclick="getRemoteAccount()">Get Account</button>
    <div id="responseErrors"></div>
    <br/>
    <apex:pageBlock id="block">
        <apex:pageBlockSection id="blockSection" columns="2">
            <apex:pageBlockSectionItem id="firstItem">
                <span id="remoteAcctId"/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem id="secondItem">
                <apex:outputText id="acctNumEmployees"/>
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
    </apex:pageBlock>
    
    <a target="_blank" href="https://demobyarun-developer-edition.ap5.force.com/AccountDetail" style="color:blue;text-decoration: underline;">Click Here To See All Accounts</a>
</apex:page>

Apex Class:

global class AccountRemoterInsert
{
    public String accountName { get; set; }
    public static Account account { get; set; }
    @RemoteAction
    global static Boolean InsertAccountDetail(String accountName) 
    {
        System.debug(accountName);
         List<Account> lstAcc = [SELECT Id, Name, Phone, Type, NumberOfEmployees 
                   FROM Account WHERE Name = :accountName];
         if(lstAcc!=null && lstAcc.size()>0)
         {
             System.debug('True');
             return true;
         }
         else
         {
             Account a=new Account();
             a.Name=accountName;
             insert a;
             return false;
         }
    }
    
    // Return the search result.
    @RemoteAction
    global static Account getAccountDetail(String accountName) {
        account = [SELECT Id, Name, Phone, Type, NumberOfEmployees 
                   FROM Account WHERE Name = :accountName];
        return account;
    }
}

0 comments:

Post a Comment

If you have any doubts, please let me know.