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
sancarlos94070sancarlos94070 

Have trouble invoking Apex using custom button with S-Control / Ajax

I need to have a custom button that will clone a lead then give it a different record type. I am using the custom button to invoke S-Control which will then call my Apex code as webservice but so far, the call not able to invoke anything, without any error message.

S-Control code here:





<script type="text/javascript" src="/js/functions.js"></script>
<script src="/soap/ajax/15.0/connection.js" type="text/javascript"></script>
<script src="/soap/ajax/15.0/apex.js" type="text/javascript"></script>
<script>
function createCharterLead() {
var lead = sforce.sObject("lead");
var lead2 = sforce.apex.execute("spinOffCharterLead","createCharterLead",l:lead);
}
</script>



Charter Lead created.








Apex code:

global class spinOffCharterLead {

// eyCDUkymDHyd9MisHBFLPIYJm

webService static Lead createCharterLead(Lead l) {

// log entry
System.debug('Now in spin off class');

ID cid = [select id from RecordType where name='CharterLead' limit 1].id;
System.debug('Now selected recordtype ID');

Lead lc = l.clone(false, true);
lc.RecordTypeId = cid;

// Now insert
insert(lc);
System.debug('Inserted Lead');

// Track
Program_Charter_Link__c pcl = new Program_Charter_Link__c();
pcl.Charter_Lead__c = lc.Id;
pcl.Program_Lead__c = l.Id;
insert(pcl);
System.debug('Inserted tracking');

return lc;

}

}
Best Answer chosen by Admin (Salesforce Developers) 
Force2b_MikeForce2b_Mike

First, I recommend against using an S-Control since these are being eliminated next year. Instead, use an OnClick JavaScript event for your button.

 

Here's some code that I have to call a method from a JavaScript button:

 

 

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

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

 

var ICS_RecordType = "012300000000Be1";

var SPS_UserProfiles = "00e30000000f8h4";

var currentProfileID = "{!$Profile.Id}";

var runButton = true;

 

if ("{!Opportunity.ContractID__c}" != "") {

parent.document.location.href = "{!IF( ISNULL(Opportunity.ContractIDId__c) == false, URLFOR( $Action.Contract.View, Opportunity.ContractIDId__c, [retURL=Opportunity.Link]), '')}";

} else {

if ( ("{!Opportunity.RecordTypeId}" == ICS_RecordType) && ({!Opportunity.IsClosed} == false) ) {

alert("The [Create Contract] button cannot be used on Opportunities that are already marked as Closed/Won or Closed/Lost.");

runButton = false;

}

if ( ( "{!Opportunity.RecordTypeId}" != ICS_RecordType) && (SPS_UserProfiles.search(currentProfileID) == -1) ) {

alert("The [Create Contract] button is restricted to specific User Profiles for SPS Record Types. Please Contact your admin for additional assistance if needed.") ;

runButton = false;

}

if (runButton) {

var result = sforce.apex.execute( "OpportunityButtons", "createContract", {oppID:"{!Opportunity.Id}"} );

if (result != "") {

result = new String(result);

if (result.substr(0,1) != "/") alert(result);

else parent.document.location.href = result;

}

}

 
Message Edited by scrappydog on 06-23-2009 08:06 AM (to fix the javascript formatting)
Message Edited by scrappydog on 06-23-2009 08:11 AM

All Answers

sancarlos94070sancarlos94070
On a related note I do -not- see any output from System.debug into the System Log window at all!? Any ideas why? Thank you.
RajanJasujaRajanJasuja

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
<script type="text/javascript" src="/js/functions.js"></script>
<script src="/soap/ajax/15.0/connection.js" type="text/javascript"></script>
<script src="/soap/ajax/15.0/apex.js" type="text/javascript"></script>
<script>
function createCharterLead() {
var lead =new sforce.SObject("Lead");

lead.LastName="{!Lead.LastName}";
lead.Company="{!Lead.Company}";

var lead2 = sforce.apex.execute("spinOffCharterLead","createCharterLead",{l:lead});
alert(lead2);
}
</script>
 </HEAD>

 <BODY onload="createCharterLead();">


 </BODY>
</HTML> 

sancarlos94070sancarlos94070

Thanks but it is still not working.  No system log generated.  No alert pop-up.  Also, should it be ' or " to enclose text string?

 

Attaching apex code here again:

 

global class spinOffCharterLead {
   
    // eyCDUkymDHyd9MisHBFLPIYJm
   
    webService static Lead createCharterLead(Lead l) {
       
        // log entry
        System.debug('Now in spin off class');
       
        ID cid = [select id from RecordType where name='CharterLead' limit 1].id;
        System.debug('Now selected recordtype ID');
       
        Lead lc = l.clone(false, true);
        lc.RecordTypeId = cid;
       
        // Now insert
        insert(lc);
        System.debug('Inserted Lead');
       
        // Track
        Program_Charter_Link__c pcl = new Program_Charter_Link__c();
        pcl.Charter_Lead__c = lc.Id;
        pcl.Program_Lead__c = l.Id;
        insert(pcl);
        System.debug('Inserted tracking');
               
        return lc;
       
    }

}

RajanJasujaRajanJasuja
Hi, Are your custom button is on the detail page of Lead? It should be on the detail page of lead.In S-Control you can use " and in Apex class user ' for strings.More I can help you after login into your SFDC Org, user my Id Rajan_Jasuja2005@yahoo.comto share your login details.

 

 

Best Regards,

Rajan Jasuja

 

Force2b_MikeForce2b_Mike

First, I recommend against using an S-Control since these are being eliminated next year. Instead, use an OnClick JavaScript event for your button.

 

Here's some code that I have to call a method from a JavaScript button:

 

 

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

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

 

var ICS_RecordType = "012300000000Be1";

var SPS_UserProfiles = "00e30000000f8h4";

var currentProfileID = "{!$Profile.Id}";

var runButton = true;

 

if ("{!Opportunity.ContractID__c}" != "") {

parent.document.location.href = "{!IF( ISNULL(Opportunity.ContractIDId__c) == false, URLFOR( $Action.Contract.View, Opportunity.ContractIDId__c, [retURL=Opportunity.Link]), '')}";

} else {

if ( ("{!Opportunity.RecordTypeId}" == ICS_RecordType) && ({!Opportunity.IsClosed} == false) ) {

alert("The [Create Contract] button cannot be used on Opportunities that are already marked as Closed/Won or Closed/Lost.");

runButton = false;

}

if ( ( "{!Opportunity.RecordTypeId}" != ICS_RecordType) && (SPS_UserProfiles.search(currentProfileID) == -1) ) {

alert("The [Create Contract] button is restricted to specific User Profiles for SPS Record Types. Please Contact your admin for additional assistance if needed.") ;

runButton = false;

}

if (runButton) {

var result = sforce.apex.execute( "OpportunityButtons", "createContract", {oppID:"{!Opportunity.Id}"} );

if (result != "") {

result = new String(result);

if (result.substr(0,1) != "/") alert(result);

else parent.document.location.href = result;

}

}

 
Message Edited by scrappydog on 06-23-2009 08:06 AM (to fix the javascript formatting)
Message Edited by scrappydog on 06-23-2009 08:11 AM
This was selected as the best answer
sancarlos94070sancarlos94070
Yes it's on Detail page of Lead. I will also try the onclick Javascript as well & post the results. Thank you.
sancarlos94070sancarlos94070
Using OnClick helps greatly!!  At least now i can get the stack trace.  Thanks!!