Saturday, September 13, 2014

How to call Apex class method from Javascript Custom Button?

How to call Apex class method from Javascript Custom Button?

In this post i am giving an example of how to call Apex Class method when ever we click on custom button. i this scenario i have one custom button that is created in my project custom object and place in details page layout.

Custom Button Code:

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

var id = sforce.apex.execute(""Projectctrl"",""createProject"",{easySupportId:""{!Support__c.Id}""});

location.reload();

Apex Class:

global class Projectctrl {
 webservice static Id createProject(String easySupportId){
  
  Support__c  supportobj=[select id,Category__c,OwnerId from Support__c where id=:easySupportId];
  
  SFDC_Project__c  projObj=new SFDC_Project__c();
  projObj.Easy_Support_No__c=supportobj.id;
  projObj.Road_Map_Identification__c='Enable';
  if(supportObj.Category__c=='Project')
   projObj.Request_Type__c='Project';
  else
   projObj.Request_Type__c='Minor Enhancement'; 
  projObj.Estimated_Monthly_Release__c=String.valueOf(System.today().month()+1);
  projObj.User_project_requested_by__c=supportObj.OwnerId;
  insert projObj;
  SFDC_Project__c sfdcProjectObj=[select Name from SFDC_Project__c where id=:projObj.Id];
  projObj.SFDC_Project_Name__c='Project-'+sfdcProjectObj.Name;
  update projObj;
  return projObj.id;
 }
}


Test class:

@isTest(seeAllData=true)
private class TestProjectctrl {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        Support__c  supportobj=new Support__c();
        supportobj.Category__c='Project';
        supportobj.ContactName__c=UserInfo.getUserId();
        supportobj.Contact_No__c='9246263254';
        supportobj.Description__c='Test description';
        insert supportobj;
        Projectctrl.createProject(supportobj.id);
        
    }
    static testMethod void myUnitTest1() {
        // TO DO: implement unit test
        Support__c  supportobj=new Support__c();
        supportobj.Category__c='Others';
        supportobj.ContactName__c=UserInfo.getUserId();
        supportobj.Contact_No__c='9246263254';
        supportobj.Description__c='Test description';
        insert supportobj;
        Projectctrl.createProject(supportobj.id);
        
    }
}






2 comments:

seravina danniella said...


Your blog has given me that thing which I never expect to get from all over the websites. Nice post guys!
regards,
Melbourne Web Designer

JIMY said...

thank you....

Server and storage

Server and Storage Solutions

Post a Comment

 
| ,