You need to sign in to do that
Don't have an account?
sancarlos94070
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>
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;
}
}
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;
}
}
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;
}
}
}
All Answers
<!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>
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;
}
}
Best Regards,
Rajan Jasuja
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;
}
}
}