+ Start a Discussion
sundhar.mks1.3962649227519546E12sundhar.mks1.3962649227519546E12 

Javascript using visualforce?

Hi

I have use the following javascript code to Custom button, How to implement the code to visualforce?

Code
==========
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 

var SS = sforce.connection.query("select id,Visit__c from Engineer_Checklist__c where Visit__r.id='{!Visits__c.Id}' and Fire_Extinguisher_Service_complete__c=True"); 
var records1 = SS.getArray("records"); 


var RS = sforce.connection.query ("select id,Visit__c from Service_Sheet_STD__c where Visit__r.id='{!Visits__c.Id}' and Fire_Extinguisher_Service_complete__c=True"); 
var records2 = RS.getArray("records"); 


var MSS = sforce.connection.query ("select id,Visit__c from Mechanical_Service_Sheet__c where Visit__r.id='{!Visits__c.Id}' and Fire_Extinguisher_Service_complete__c=True"); 
var records3 = MSS.getArray("records"); 


var SW = sforce.connection.query ("select id,Visit__c from Small_Works__c where Visit__r.id='{!Visits__c.Id}' and Submit_this_Small_Works_Sheet__c=True"); 
var records4 = SW.getArray("records"); 


var PS = sforce.connection.query("select id,Visit__c from Project_Sheet__c where Visit__r.id='{!Visits__c.Id}' and Fire_Extinguisher_Service_complete__c=True"); 
var records5 = PS.getArray("records"); 


if((records1.length!=0 || records2.length!=0 || records3.length!=0 || records4.length!=0 || records5.length!=0) && 
confirm("You have already submitted a sheet for this visit, are you sure you want to submit another one?")){ 

window.open("https://cs16.salesforce.com/a1Z/e?CF00ND0000005FbyJ={!Visits__c.Name}&CF00ND0000005FbyJ_lkid={!Visits__c.Id}&retURL={!Visits__c.Site_Contract__c}&CF00ND0000005FcIJ_lkid","_top") 

else if(records1.length==0 && records2.length==0 && records3.length==0 && records4.length==0 && records5.length==0) 

window.open("https://cs16.salesforce.com/a1Z/e?CF00ND0000005FbyJ={!Visits__c.Name}&CF00ND0000005FbyJ_lkid={!Visits__c.Id}&retURL={!Visits__c.Site_Contract__c}&CF00ND0000005FcIJ_lkid","_top") 
}
Best Answer chosen by sundhar.mks1.3962649227519546E12
Jigar.LakhaniJigar.Lakhani

Hi Mr Salesforce,

Does not check the if condition it check the else straightly, i want to add this page to formula field using HYPERLINK so i need to check the condition only in (javascript)visualforcepage , how do this?

Thanks.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Hello,

We are already using javascript in visualforce page, create a visualforce page which is in above message as well as create apex class.

For Hyperlink

- Go To --> Your Name --> Setup --> Create --> Objects
- Open your custom object "Visit__c"
- Click "New Button or Link"

Label - Link Label
Name - Link Name
Display Type - Detail Page Link
Behavior - Display in existing window with sidebar / Display in existing window without sidebar
Content Source - Visualforce Page
Content - Created Visualforce Page Name

Include created hyper link to the visit page layout.

Thanks,
Jigar

 

All Answers

Jigar.LakhaniJigar.Lakhani
Hello,

Below is visualforce page and apex class code for your script.

Visualforce Page
<apex:page id="visitPage" standardController="Visit__c" extensions="submitVisitController" tabStyle="Visit__c">
	<apex:form id="visitForm">
		<script>
			window.load = openSubmitVisitPage();
			function openSubmitVisitPage(){
				if ({!blnAlreadySubmited} == true || '{!blnAlreadySubmited}' == 'true') {
					if (confirm("You have already submitted a sheet for this visit, are you sure you want to submit another one?")){
						window.open("https://cs16.salesforce.com/a1Z/e?CF00ND0000005FbyJ={!Visits__c.Name}&CF00ND0000005FbyJ_lkid={!Visits__c.Id}&retURL={!Visits__c.Site_Contract__c}&CF00ND0000005FcIJ_lkid","_top");
					} else {
						window.open("https://cs16.salesforce.com/a1Z/e?CF00ND0000005FbyJ={!Visits__c.Name}&CF00ND0000005FbyJ_lkid={!Visits__c.Id}&retURL={!Visits__c.Site_Contract__c}&CF00ND0000005FcIJ_lkid","_top");
					}
				}
			}
		</script>
	</apex:form>
</apex:page>

Apex Class
public class submitVisitController {
	
	// Visit object
	public Visit__c objVisit{get;set;}
	
	// Flag for confirmation
	public Boolean blnAlreadySubmited{get;set;}
	
	// Collection Variables
	public List<Engineer_Checklist__c> listEngineerCheckList;
	public List<Service_Sheet_STD__c> listServiceSheetSTD;
	public List<Mechanical_Service_Sheet__c> listMechanicalServiceSheet;
	public List<Small_Works__c> listSmallWorks;
	public List<Project_Sheet__c> listProjectSheet;
	
	///========================================================//
    //Constructor
    //========================================================//
    public avPolicyDetailsController(ApexPages.StandardController objStandardController){
		Visit__c objVisitFromPage = (Visit__c)objStandardController.getRecord();
		if (objVisitFromPage != null && objVisitFromPage.Id != null) {
			objVisit = [SELECT Id, Name, Site_Contract__c FROM Visit__c WHERE Id =: objVisitFromPage.Id LIMIT 1];
			if (objVisit != null) {
				
				listEngineerCheckList = new List<Engineer_Checklist__c>(SELECT Id,Visit__c FROM Engineer_Checklist__c 
											WHERE Visit__r.Id =: objVisit.Id AND Fire_Extinguisher_Service_complete__c = True);
				
				listServiceSheetSTD = new List<Service_Sheet_STD__c>(SELECT Id,Visit__c FROM Service_Sheet_STD__c 
											WHERE Visit__r.Id =: objVisit.Id AND Fire_Extinguisher_Service_complete__c = True);
				
				listMechanicalServiceSheet = new List<Mechanical_Service_Sheet__c>(SELECT Id,Visit__c FROM Mechanical_Service_Sheet__c 
											WHERE Visit__r.Id =: objVisit.Id AND Fire_Extinguisher_Service_complete__c = True);
				
				listSmallWorks = new List<Small_Works__c>(SELECT Id,Visit__c FROM Small_Works__c 
											WHERE Visit__r.Id =: objVisit.Id AND Submit_this_Small_Works_Sheet__c = True);
				
				listProjectSheet = new List<Project_Sheet__c>(SELECT Id,Visit__c FROM Project_Sheet__c 
											WHERE Visit__r.Id =: objVisit.Id AND Fire_Extinguisher_Service_complete__c = True);
				
				if (listEngineerCheckList.size() > 0 || listServiceSheetSTD.size() > 0 || listMechanicalServiceSheet.size() > 0 
					|| listSmallWorks.size() > 0 || listProjectSheet.size() > 0) {
					
					blnAlreadySubmited = true;
				} else {
					blnAlreadySubmited = false;
				}
				
			}
		}
	}
	
}

Please let me know if you have any issue.

Thanks and Cheers,
Jigar
sundhar.mks1.3962649227519546E12sundhar.mks1.3962649227519546E12
Hi Mr Salesforce,

The Condition not working correctly,
NOTE: Actually (SS,RS,MSS,SW,PS) these are related list in Visit object. Every related lsit have "submited sheet" field  this field is checkbox.  any one of the Related list have submited sheet in visit object the Warning should be display oetherwise the record should create normaly

Thanks,
 
Jigar.LakhaniJigar.Lakhani
Oh, minor mistakes over there.

Please try again with below code.

Visualforce Page
<apex:page id="visitPage" standardController="Visit__c" extensions="submitVisitController" tabStyle="Visit__c">
	<apex:form id="visitForm">
		<script>
			window.load = openSubmitVisitPage();
			function openSubmitVisitPage(){
				if ({!blnAlreadySubmited} == true || '{!blnAlreadySubmited}' == 'true') {
					if (confirm("You have already submitted a sheet for this visit, are you sure you want to submit another one?")){
						window.open("https://cs16.salesforce.com/a1Z/e?CF00ND0000005FbyJ={!Visits__c.Name}&CF00ND0000005FbyJ_lkid={!Visits__c.Id}&retURL={!Visits__c.Site_Contract__c}&CF00ND0000005FcIJ_lkid","_top");
					}
				} else {
					window.open("https://cs16.salesforce.com/a1Z/e?CF00ND0000005FbyJ={!Visits__c.Name}&CF00ND0000005FbyJ_lkid={!Visits__c.Id}&retURL={!Visits__c.Site_Contract__c}&CF00ND0000005FcIJ_lkid","_top");
				}
			}
		</script>
	</apex:form>
</apex:page>

No any change for class, change the constructor name same as apex class name.

 
Jigar.LakhaniJigar.Lakhani

Hi Mr Salesforce,

Does not check the if condition it check the else straightly, i want to add this page to formula field using HYPERLINK so i need to check the condition only in (javascript)visualforcepage , how do this?

Thanks.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Hello,

We are already using javascript in visualforce page, create a visualforce page which is in above message as well as create apex class.

For Hyperlink

- Go To --> Your Name --> Setup --> Create --> Objects
- Open your custom object "Visit__c"
- Click "New Button or Link"

Label - Link Label
Name - Link Name
Display Type - Detail Page Link
Behavior - Display in existing window with sidebar / Display in existing window without sidebar
Content Source - Visualforce Page
Content - Created Visualforce Page Name

Include created hyper link to the visit page layout.

Thanks,
Jigar

 

This was selected as the best answer