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
ckellieckellie 

Custom Button Error:

I am recieving the following error when I click a custom button that customizes the new button.

 

Error

 

A problem with the OnClick JavaScript for this button or link was encountered:

sforce is not defined

 

 

Custom Button - List Button - Execute JavaScript - OnClick JavaScript

 

 

var MPS__c = sforce.sObject("MPS");
var id = sforce.apex.execute("NewMPSGlobal","makeMPS",
                             {MPS_Scheduled_Ship_Date__c: Opportunity.Projected_Ship_Date__c,
                              o:opportunity});

 

Apex Class:

 

 

global class NewMPSGlobal { 
  webService static Id makeMPS( Opportunity o) { 
        MPS__c mps = new MPS__c(MPS_Scheduled_Ship_Date__c= o.Projected_Ship_Date__c, Opportunity__c = o.Id); 
        return mps.id; 
    }
}

 

 

How do I solve the error?

 

Thank you

 

Ritesh AswaneyRitesh Aswaney
Have you included the salesforce js references


<script src="/soap/ajax/15.0/connection.js" type="text/javascript"></script>
<script src="/soap/ajax/15.0/apex.js" type="text/javascript"></script>

Doc link http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_and_ajax.htm
ckellieckellie

I was missing that and included them in the button. But I now am seeing the following error:

 

A problem with the OnClick JavaScript for this button or link was encountered:

missing ; before statement

 

 

I am adapting the code from the apex user guide. Here is what I have for the button.

 

 

<script src="/soap/ajax/15.0/connection.js" type="text/javascript"></script>
<script src="/soap/ajax/15.0/apex.js" type="text/javascript"></script>

var MPS = sforce.sObject("MPS__c");
var id = sforce.apex.execute("NewMPSGlobal","makeMPS",
                             {MPS_Scheduled_Ship_Date__c: Opportunity.Projected_Ship_Date__c,
                              o:opportunity});

 

 

What am I missing?

 

Thank you

dotnet developedotnet develope

Hi,

 

 

1) Why you are initializing two parameters for the method makeMPS().

 

sforce.apex.execute("NewMPSGlobal","makeMPS",
                             {MPS_Scheduled_Ship_Date__c: Opportunity.Projected_Ship_Date__c,
                              o:opportunity}
);

2) why you need to pass opportunity. just pass the id of the opportunity.

 

class

 

global class NewMPSGlobal {
  webService static Id makeMPS( id oppid) {
        MPS__c mps = new MPS__c(MPS_Scheduled_Ship_Date__c= o.Projected_Ship_Date__c, Opportunity__c = oppid);

 
        return mps.id;
    }
}

 

Button

 

var sOppid = "{!Opportunityid}"; 

sforce.apex.execute("NewMPSGlobal","makeMPS",{oppid:sOppid});

 

 

 

ckellieckellie

Thank you for the help. I am running into syntax problems:

 

1.   


Error: Compile Error: Variable does not exist: o.Projected_Ship_Date__c at line 3 column 62

 for the recommended code:

 

 

global class NewMPSGlobal {
  webService static Id makeMPS( id oppid) {
        MPS__c mps = new MPS__c(MPS_Scheduled_Ship_Date__c = o.Projected_Ship_Date__c, Opportunity__c = oppid);

 
        return mps.id;
    }
}

 

 

I then changed o.Projected_Ship_Date__c to opportunity.Projected_Ship_Date__c

 

global class NewMPSGlobal {
  webService static Id makeMPS( id oppid) {
        MPS__c mps = new MPS__c(MPS_Scheduled_Ship_Date__c = opportunity.Projected_Ship_Date__c, Opportunity__c = oppid);

 
        return mps.id;
    }
}

 

And I recieve the following error:

 

 

	Error: Compile Error: Invalid initial expression type for field MPS__c.MPS_Scheduled_Ship_Date__c, expecting: Date at line 3 column 62

 

 

Both fields are date formatted fields. What is the error and how do I solve it?

 

Thank you,

Ckellie

 

 

Pradeep_NavatarPradeep_Navatar

You need to include the JS references while calling salesforce API. Tryout the syntax given below :

 

{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}

{!REQUIRESCRIPT("/soap/ajax/20.0/apex.js")}

ckellieckellie

Thank you, the updated version solved that problem, but I am now experiencing this error:

 

 

A problem with the OnClick JavaScript for this button or link was encountered:

{faultcode:'soapenv:Client', faultstring:'System.NullPointerException: Attempt to de-reference a null object

Class.NewMPSGlobal.makeMPS: line 5, column 55
External entry point', }

 

 

As I am not familiar with this JavaScript error, I don't know what to do to fix this. Is this concerning the Javascript button or the apex class?

 

Here is the apex class:

 

global class NewMPSGlobal {
  
  webService static mPS__c makeMPS( id oppid, Opportunity o) {
        MPS__c mps = new MPS__c();
        mps.MPS_Scheduled_Ship_Date__c = date.valueof(o.Projected_Ship_Date__c);
                mps.Opportunity__c = oppid;

        return mps;
    }
}

 

 

Thank you,

ckellie