+ Start a Discussion
tzuvytzuvy 

How to run apex classes without a page?

I've created a class that receives a url and based on it parses all the variables and creates an instances of a custom object in a specific account.

 

The class works perfectly when called on an apex page. The problem is that I'm sending the url from a server directly to SF and the server doesn't know how to open the page. For this reason the class is never called and the object never inserted.

 

Is there a way to create a page/process in SF that would create a record without loading a page?

 

Here are the class and the page I'm referring to.

 

public class ChatDetail {

   


      private final Account account;
      public Chat_Details__c chat {get; private set;}

      private list<String> operator;
      private list<String> params;
      string DefaultSiteSMB;
      string DefaultSiteENT;

      public ChatDetail() {
      
      DefaultSiteSMB='74714728';
       try {
           
           params=ApexPages.currentPage().getParameters().get('siteid').split('\\?');
           account = [select   LPSiteID1__c, id, name, site from Account where LPSiteID1__c =
                        :params.get(0)];
           operator = ApexPages.currentPage().getParameters().get('siteid').split('\\?var\\=');
           
           
           chat=new Chat_Details__c();
           chat.Account_del__c= account.id;
            chat.Chat_End_Time__c=operator.get(3);
            chat.Chat_Operator__c=operator.get(1);
            chat.Chat_Start_Time__c=operator.get(2);
            chat.Customer_Admin_Level__c=operator.get(5);
            chat.Customer_Subscription__c=operator.get(4);
            chat.Name=operator.get(6);
            
           
          
          

           
       } 
       // System.DMLException e
       //catch(System.DMLException e) {
         //  params=ApexPages.currentPage().getParameters().get('siteid').split('\\?');
           //account = [select   LPSiteID1__c, id, name, site from Account where LPSiteID1__c =
             //           :DefaultSiteSMB];
           //operator = ApexPages.currentPage().getParameters().get('siteid').split('\\?var\\=');
          
           
       //}
       catch(Exception e) {
           params=ApexPages.currentPage().getParameters().get('siteid').split('\\?');
           account = [select   LPSiteID1__c, id, name, site from Account where LPSiteID1__c =
                        :'74714728'];
           operator = ApexPages.currentPage().getParameters().get('siteid').split('\\?var\\=');
           
           chat=new Chat_Details__c();
           chat.Account_del__c= account.id;
           chat.Chat_End_Time__c=operator.get(3);
           chat.Chat_Operator__c=operator.get(1);
           chat.Chat_Start_Time__c=operator.get(2);
           chat.Customer_Admin_Level__c=operator.get(5);
           chat.Customer_Subscription__c=operator.get(4);
           chat.Name=operator.get(6);
       }

                                    
      }
      
    
          
    public String getOperator() {
        return operator.get(1);
    }
    
    public String getStartDate() {
        return operator.get(2);
    }
    
     public String getEndDate() {
        return operator.get(3);
    }
    
     public String getSubscription() {
        return operator.get(4);
    }
    
     public String getAdminLevel() {
        return operator.get(5);
    }
    
     public String getName() {
        return operator.get(6);
    }

     public Account getAccount() {
            return account;
      }

     public PageReference save() {
     try {
     upsert(chat);
     } catch(System.DMLException e) {
     ApexPages.addMessages(e);
     return null;
     }
     //  After Save, navigate to the default view page:  
    
     return (new ApexPages.StandardController(chat)).view();
   }

     
}

 

 

 

And here is the page:

 

 

<apex:page controller="ChatDetail" tabStyle="Account" action="{!save}" >



</apex:page>

 

 

Any help will be appreciated.

 

Thanks

 

Tzuvy

 

 

paul-lmipaul-lmi

you can either do it the way you already are, or by exposing it as a webservice and using the ajax toolkit/javascript to get to it.  if you're doing this via force.com sites, note that you have security as a factor as well (which you probably saw in setting this up via the page).

tzuvytzuvy

Thanks for the quick reply. Can you tell me a little more about the webservice? Or how to make the page run without the need to be loaded?

 

Basically what I want to do is to run the save function in the constructor but is not possible, I get a DML exception.

 

How could I set a Web Service? Is this a type of object? Or is it a include in the class?

 

I will appreciate if you could point me to some tutorial on how to build this kind of solution.

 

The security I'm passing by sendind the un and pw elements in the url, that is why I changed the splitter in the querying function.

 

Thanks

 

Tzuvy

paul-lmipaul-lmi

take a look at the apex developer guide.  essentially, you define your class as global, and your desired method as a webservice.  you can then have your server connect to the API, instead loading a web page.  you didn't really provide much detail on why the server can't load a page/what type of server it is, etc, so just throwing out suggestions.