function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sales4cesales4ce 

Custom Button-Onclick Java Script error

Hi,

 

I have a custom button on Contact Page layout. The button actually overrides a Visualforce page.

The visualforce page Uses controller extension to extend Standardcontroller 'Contact". The Extension is " InactiveandMove".

 

Now, i need to execute Javascript when clicked on the button which pops up a confirmation dialog like, " Are you sure?".

When clicked OK, it has to execute the Controller extension Action : "Move".

 

But for some reason i am unable to do it. when i click on the button, it raises a error like this:

 

A problem with the OnClick JavaScript for this button or link was encountered:
missing ; before statement

 

Everything looks fine, but i am unable to figure out the problem.

Am i missing anything or should this be done in a different way?

 

Here is the VF Page:

 

<apex:page standardController="Contact"  extensions="Inactiveandmove"   >
<apex:form >
<apex:actionFunction name="SendtoMoveFunction" action="{!Move}" reRender="{!Id}"/>
</apex:form>
<apex:PageBlock title="Confirmation">
<b><Font Color="Red" Size="3"> <apex:outputLabel rendered="{!errorFlag}" > {!errorMsg}</apex:outputLabel></Font></b><br/><br/>
<b><Font Color="Green" Size="2"> <apex:OutputText >Please Contact your System Admininstartor for any further action on this!</apex:OutputText></Font></b><br/>
</apex:PageBlock>

</apex:page>

Here is the Controller extension:

 

public class Inactiveandmove {

    public String errorMsg { get; set;}
    public Boolean errorFlag {get ; set;}  
     
     //Constructor
    public Inactiveandmove(ApexPages.StandardController controller) 
    {
        errorMsg = '';
        errorFlag = false;

    }  

    public Contact contact  { get; private set;}
    

//Function that does the main job

    public PageReference Move(){ // This is the Action function to be called from Java script.

    //System.debug('id==='+Apexpages.Currentpage().getparameters().get('Id'));
   //Check if the contact is associated with any campaign that is Active.
    List<CampaignMember> active_Campaigns=[Select ContactId from CampaignMember where Campaign.Isactive=True and Contactid=:Apexpages.Currentpage().getparameters().get('Id')];
   
    if(active_Campaigns.size()!=0)  //If present in any active campaign, do not delete the contact
    {
        errorFlag=true;
        errorMsg='Cannot Delete the Contact, as it is associated with an Active Campaign';
        return null;
    }
    
    else
    {
        //Get all the campaigns(Inactive Ones) the contact is associated with
        List<CampaignMember> inactive_Campaigns=[Select ContactId from CampaignMember where Contactid=:Apexpages.Currentpage().getparameters().get('Id')];
        for(CampaignMember cm:inactive_Campaigns)
            delete cm; //Delete the contact from the inactive campaigns
       
        //Get the contact ownerID and AccountName which has to be logically deleted.
        contact c=[SELECT ownerId,Account.Name FROM contact where Id=:Apexpages.Currentpage().getparameters().get('Id')];

        c.OwnerId='005A0000000h2hh'; //change to Admin
        //Set the other values accordingly
        //c.Status__c='Retired/Resigned';
        c.HasOptedOutOfEmail=True;
        c.DoNotCall=True;
        c.Email=' ';
        c.Phone=' ';
        //c.No_Postal_Mail__C=True;
        //c.Previous_Account_Name__c=c.Account.Name; // Gets the account name and puts that in this custom field.
        //System.debug('account name == '+c.Account.Name);
        c.AccountId=NULL;

        update c; //Update the contact with New Values.
        
        errorFlag=true;
        errorMsg='Record Deleted';
        return Null;
        }

    }

    //Test Method
    public static testMethod void InactiveandmoveTest()
    {
        Contact con = [Select id from contact limit 1];
        ApexPages.StandardController sc = new ApexPages.StandardController(con);
    
        System.debug('sc id =='+sc.getId());
    //    sc.setId('003Q0000008c1By');
        Inactiveandmove InactiveAndMoveTest = new Inactiveandmove(sc);
        Apexpages.currentPage().getParameters().Put('id',sc.getId());
        InactiveAndMoveTest.Move();
    
    }
}

 

Added a custom button on Contact:

 

Detail Page button

Onclick Javascript

 

Here is the JavaScript&colon;

 

{!REQUIRESCRIPT("/soap/ajax/17.0/connection.js")}
function confirmation() {
var answer = confirm("Are You Sure?")
if (answer){
S2M()

}
else{
alert("Thanks for sticking around!")
}
}

function executeS2M() {
var pageref = sendtoMoveFunction();
return pageref;
}
confirmation();

 

Any help on this is highly appreciated.

 

Thanks,

Sales4ce

Best Answer chosen by Admin (Salesforce Developers) 
sforce2009sforce2009

You can not call the controller extension methods directly from onclick js. Instead you have to redirect to the vf page by sending proper ids.

 

if(confirm('Are you sure?'))

{

    window.location.href = "../apex/yourVFPage?contId={!Contact.Id}";     //try window.parent.location.href if it not works

}

//This is the only statement you need in your onclick js. nothing else.

All Answers

aballardaballard

You're missing semicolons at the end of three statements in your function confirmation() .

I know javascript is fairly forgiving about this, but since the error message complains about a missing semicolon, I'd suggest adding them and seeing if that fixes it. 

 

 

sales4cesales4ce

Hi,

 

Thanks for your quick reply!

I even tried the same but to no luck.

 

Also, i changed the function this way, but it now says "SendtoMoveFunction" Not defined.

 

{!REQUIRESCRIPT("/soap/ajax/17.0/connection.js")}
function confirmation() {
var answer = confirm("Are You Sure?")
if (answer){
var Pageref=SendtoMoveFunction();
return Pageref;

}
else{
alert("Thanks for sticking around!")
}
}
confirmation();

What i feel is Javascript is unable to identify the function.

Am i missing anything?

 

thanks,

sales4ce

aballardaballard

I don't understand how you are tieing these pieces together.... where does the visual force page come into it at all?   

 

If your custom button is onclick javascript and appears on a standard page, it certainly can't call a function on  some visualforce page. 

 

 

sales4cesales4ce

Thats a good question . I am not quite sure too.

Then how do we execute javascript and then call controller extension methods?

sforce2009sforce2009

You can not call the controller extension methods directly from onclick js. Instead you have to redirect to the vf page by sending proper ids.

 

if(confirm('Are you sure?'))

{

    window.location.href = "../apex/yourVFPage?contId={!Contact.Id}";     //try window.parent.location.href if it not works

}

//This is the only statement you need in your onclick js. nothing else.

This was selected as the best answer
sales4cesales4ce

Thanks Srinivas, it worked like charm!

 

Thx,

sales4ce

Raja JammulaRaja Jammula
if('{!$UserRole.Name}'=='XYZ' || '{!$UserRole.Name}'=='ABC'){ 
var url = '/a0Y/e?retURL={!Opportunity.Id}&RecordType=01250000000J5bY&ent=01I50000000QcYO&CF00N50000002DH5h={!Opportunity.Account}&CF00N50000002DH5h_lkid={!Opportunity.AccountId}&CF00N50000002DH5v={!Opportunity.Name}&CF00N50000002DH5v_lkid={!Opportunity.Id}&CF00N50000002DH5i_lkid={!$User.Id}&CF00N50000002Dnha={!Opportunity.Primary_Third_Party_Acc__c}& CF00N50000002Dnha_lkid={!Opportunity.Primary_Third_Party_Account_ID__c}&CF00N50000002DX3C={!Opportunity.Primary_Third_Party_Contact__c}&CF00N50000002DX3C_lkid={!Opportunity.Primary_Third_Party_Contact_ID__c}&CF00N50000002DH5i={!$User.FirstName}+{!$User.LastName}&00N50000002DK1t={!TODAY()}'; 
window.open(url,'_self'); 

else{ 
alert('You don\'t have permission to access this button'); 
}

Getting the same error
A problem with the OnClick JavaScript for this button or link was encountered:
 
missing ; before statement

can you help me plz?