+ Start a Discussion
Shawn Kaiser 7Shawn Kaiser 7 

How to have validation rule fire on button?

New to coding: 
I have the below code being used on a button to fire a process builder. I need add a rule to make sure prior to the process builder being fired that the following field on the contact record "Termination__c" is not blank. 
If blank a message should appear = termation date is required AND the process builder does NOT fire until this termination date is filled in:

Current javascript in the button:
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")} 
//Select the current contact records and put it into the record //variable 
var records = sforce.connection.query("SELECT Id, Offboarding_Process_Builder_Trigger__c FROM Contact Where id = '{!Contact.Id}'"); 
//Pull the contact object out of the records variable and set it to //contactRec 
var contactRec = records.getArray('records')[0]; 
//Increase contactRec Offboarding_Process_Builder_Trigger__c 
//JS sees the number field as a string so this will make sure that //is a number - Number(contactRec.Offboarding_Process_Builder_Trigger__c) 
contactRec.Offboarding_Process_Builder_Trigger__c = Number(contactRec.Offboarding_Process_Builder_Trigger__c) + 1; 
//Update contactRec object 
sforce.connection.update([contactRec]); 
//Reload the page 
window.location.reload();

I thought I could insert the below somewhere in the above to make it work?
//Termination field is requied when selecting the termination button
var contactReq = "{!Contact.Termination__c}";
if(contactReq != '') ;
else {
alert('Termination Date is required');

 
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}
//Select the current contact records and put it into the record
//variable 
var records = sforce.connection.query("SELECT Id, Termination__c, Offboarding_Process_Builder_Trigger__c FROM Contact Where id = '{!Contact.Id}'"); 
//Pull the contact object out of the records variable and set it to
//contactRec 
var contactRec = records.getArray('records')[0]; 
if ( !contactRec.Termination__c ){
alert( "Put your error message here." );
}else{
//Increase contactRec Offboarding_Process_Builder_Trigger__c 
//JS sees the number field as a string so this will make sure that
//is a number - Number(contactRec.Offboarding_Process_Builder_Trigger__c) 
contactRec.Offboarding_Process_Builder_Trigger__c = Number(contactRec.Offboarding_Process_Builder_Trigger__c) + 1; 
//Update contactRec object 
sforce.connection.update([contactRec]); 
//Reload the page 
window.location.reload();
}

 
Shruti SShruti S
I think you just need this - 
{!REQUIRESCRIPT("/soap/ajax/42.0/connection.js")} 

var isFilled = {!NOT(ISBLANK(Contact.Termination__c))};

if( isFilled ) {
    var contactRec = new sforce.SObject( "Contact" );

    contactRec.Offboarding_Process_Builder_Trigger__c = {!Contact.Offboarding_Process_Builder_Trigger__c} + 1;
    
    sforce.connection.update([contactRec]); 
    
    window.location.reload();
}
else {
    alert( "Termination Date is required" );
}
Shruti SShruti S
This contains a much optimised version of your old code which would continue to function the same way.
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
I like Shruti's solution; but there might be an error in it.  If it doesn't work for you, try this:
{!REQUIRESCRIPT("/soap/ajax/42.0/connection.js")} 

if( {!NOT(ISBLANK(Contact.Termination__c))} ) {
    var contactRec = new sforce.SObject( "Contact" );
    contactRec.Id = {!Contact.Id};
    contactRec.Offboarding_Process_Builder_Trigger__c = {!Contact.Offboarding_Process_Builder_Trigger__c} + 1;
    sforce.connection.update([contactRec]); 
    window.location.reload();
}
else {
    alert( "Termination Date is required" );
}

 
Shawn Kaiser 7Shawn Kaiser 7
Neither of these are working. The button still fires the process builder. hmmmm?
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
Hmm.  Try changing line 03 to one of these:
 
if( !!{!NOT(ISBLANK(Contact.Termination__c))} ) {
if( {!NOT(ISBLANK(Contact.Termination__c))} == true ) {
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
If that doesn't work, let's see what the JS thinks the Termination__c field is.  Add an alert at line 02:
alert( 'Termination = ' + {!Contact.Termination__c} + '\nNOT ISBLANK = ' + {!NOT(ISBLANK(Contact.Termination__c))} );

 
Shawn Kaiser 7Shawn Kaiser 7
sorry folks had to change the field to be: Termination_Date__c

So, Glyn - #1 does not work, #2 I changes == true to == null but still does not work and the last #1 you were so kind to try is not working either. Saying ISBLANK is incorrect arguement.

thanks in advance!
SK


 
Shruti SShruti S
If nothing works please try this -
{!REQUIRESCRIPT("/soap/ajax/42.0/connection.js")} 

var termination = "{!Contact.Termination_Date__c}";

if( termination !== "" ) {
    var contactRec = new sforce.SObject( "Contact" );
    contactRec.Offboarding_Process_Builder_Trigger__c = {!Contact.Offboarding_Process_Builder_Trigger__c} + 1; 
    sforce.connection.update([contactRec]); 
    window.location.reload();
}
else {
    alert( "Termination Date is required." );
}
Shawn Kaiser 7Shawn Kaiser 7
Shruti - getting closer. So now the error message shows YAY! However, when I do put a date in the termination date field and click termination button, the process builder does not fire.
thoughts?
Shawn Kaiser 7Shawn Kaiser 7
After some playing around with the code, I got the button to fire accordingly and it give error message and not fire if termination date is not filled in. 
Here is the code:
{!REQUIRESCRIPT("/soap/ajax/42.0/connection.js")} 
var isFilled = {!NOT(ISBLANK(Contact.Termination_Date__c ))}; 
if( isFilled){var contactRec = new sforce.SObject( "Contact" ); 
var records = sforce.connection.query("SELECT Id, Offboarding_Process_Builder_Trigger__c FROM Contact Where id = '{!Contact.Id}'"); 
var contactRec = records.getArray('records')[0]; 
contactRec.Offboarding_Process_Builder_Trigger__c = {!Contact.Offboarding_Process_Builder_Trigger__c} + 1; 
sforce.connection.update([contactRec]); 
window.location.reload();} 
else {alert( "Termination Date is required." );}
Shawn Kaiser 7Shawn Kaiser 7
If I wanted to add one more field requirement on here to be termination date needs to be filled in but also the department can you help me with that?
Shruti SShruti S
Shawn, Can you please tell us the field API Name and it's type ?
Shawn Kaiser 7Shawn Kaiser 7
There are two more:
Riskonnect_Department__c  which is a picklist and Escort_Day_1__c which is a lookup to contact