+ Start a Discussion
arun kumar 577arun kumar 577 

Hi guys i have urgent requirement on number of views field when custom url field is click

Hi guys,
i have requirement like this..

for example i have two fileds in my system. one is Number of views and pdfURL.

i have added one url link to pdfURL. when i am going to click that url field i need to display number of views field is 1

if i will going to click 10 times that link value number of views field should be 10.

Please help me on this urgent.

thanks advacne
Salesforce DeveloperSalesforce Developer
You can do this in following way:
Before going on the pdfURL page go to a temproray visualforce page and have javascript/controller method to update the record with Number of Views +1. To go on the temp VF Page you can write a trigger as below:
 
trigger AccountURLUpdate on Account (before insert, before update) {
    For(Account a: Trigger.New){
        if(a.Url__c != NULL && a.Url__c !='' ){
            string add= a.url__c;
            boolean containsURL = add.containsIgnoreCase('tempVFPage');
            if(!containsURL )
            	a.Url__c = 'https://ap1.salesforce.com/apex/tempVFPage?url='+a.Url__c+'&recordid='+a.id;
        }
    }
}

And then the code for the tempVFPage can query the record check if it has any number of views and if yes then increase that with 1, the code of this tempVFPage could be something like :
 
<apex:page >
    <script type="text/javascript">__sfdcSessionId = '{!$Api.Session_Id}';</script>
    <script src="/soap/ajax/29.0/connection.js" type="text/javascript"></script>
    <script src="/soap/ajax/29.0/connection.js" type="text/javascript"></script>
    <script>
       	var recordid= getParameterByName('recordid');
    	var myquery = "SELECT Id, No_of_Views__c,Url__c FROM Account WHERE Id = '"+ recordid + "' limit 1"; 
    	var result = sforce.connection.query(myquery);
    	var records = result.getArray("records");
    	if(records[0]!= null){
        	var acc = records[0];
            if(acc.No_of_Views__c !=null)
            	acc.No_of_Views__c = parseInt(acc.No_of_Views__c) + 1;
            else
                acc.No_of_Views__c = 1;
            var updateResult = sforce.connection.update([acc]);
            parent.window.location.href =""+getParameterByName('url')+"";
            
        }
    
    function getParameterByName(name) {
        var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
        return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
	}
    </script>
</apex:page>


 
arun kumar 577arun kumar 577
this requirement needs to override standard page of my object.
my object name is customer
how can i update number of views field when i click url field in record detail page.

thanks advacne