1

I'm a bit of a novice when it comes to Salesforce development. Basically, I'm branching out from triggers/scheduleable classes. What I'm trying to do is create a custom button that runs a class to "clone" some fields from an account into a new account of a different type. I followed the webservice template pretty closely on the developer guide but I'm getting nowhere. When I click my custom button I get

"A problem with the OnClick JavaScript for this button or link was encountered:Cannot read property 'push' of undefined"

Hopefully someone can see where I'm going wrong and point me in the right direction.
Class:

   global with sharing class KA_Clone {  
        webservice static Id Mk_Ka(Account a) {  
        account a2 = New account(Parent = a.Parent, name = a.Parent.Name +'- Key Account',    
          Parent_Account_External_ID__c = a.Parent_Account_External_ID__c, 
          BillingStreet = a.BillingStreet,   
          BillingState = a.BillingState,   
          BillingPostalCode = a.BillingPostalCode,  
          RecordTypeId = '012F0000001MHUN');  
                insert a2;
                return a2.id;
            }
        }

Button Code:

{!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")}  
{!REQUIRESCRIPT("/soap/ajax/31.0/apex.js")}  
var account = sforce.SObject("Account");  
var result = sforce.apex.execute("KA_Clone","Mk_Ka",{a:account});  
2
  • 2
    you need to insert a2 prior to return it's id. so put insert a2 before return a2.id; statement
    – Himanshu
    Jan 4 '16 at 18:44
  • I put (back) in the insert command. I'm still receiving the above error on click.
    – bcarlock
    Jan 4 '16 at 18:56
3

Your code is not correct at different place for example when you write

var account = sforce.SObject("Account");

It doesn't point to current Account record it will only create a instance of Account object. so in your case you can pass the Account id of current record and query inside your global class method in following way

global with sharing class KA_Clone {  
        webservice static Id Mk_Ka(String accId) {  

        //query the data
        Account a = [select id,ParentId,parent.name,Parent_Account_External_ID__c,
        billingstreet,billingstate,billingpostalcode from account where id =: accId];

        account a2 = New account(ParentId = a.ParentId, name = a.Parent.Name +'- Key Account',    
          Parent_Account_External_ID__c = a.Parent_Account_External_ID__c, 
          BillingStreet = a.BillingStreet,   
          BillingState = a.BillingState,   
          BillingPostalCode = a.BillingPostalCode,  
          RecordTypeId = '012F0000001MHUN'); 
          insert a2; 
                return a2.id;
            }
        }

and your button code will update in following way

{!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")}  
{!REQUIRESCRIPT("/soap/ajax/31.0/apex.js")}  
var recId= '{!Account.Id }';
var result = sforce.apex.execute("KA_Clone","Mk_Ka",{accId:recId});
alert(result);
1
  • I had a hunch that as part of the problem. I'll tinker and get back to you. Thanks!
    – bcarlock
    Jan 4 '16 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.