• Zoomzoom
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 11
    Replies

One of my interests lately has been to bring the functional specs closer to the apex code, make it easier for users to know what is actually been implemented. Yes, there should be a documentation, but in my experience it can quickly go out of sync after several iterative changes in the code. Also, sometimes the wording is key. "Record types A and B" can be the same than "Every record type but C and D", but it may not in the future.

 

Here are some things I've been working on. Let me know if you have suggestions:

 

  • When fields values are computed by some apex code, I use the fields' inline help to store the description of how they are computed. It's not a perfect solution because one needs to change those on top of deploying code. But this allows to put these definitions readily available for users, as well as display these them inside a VF page if need be.
  • Expanding on the previous concept, for some fields I used the inline help to define my own formulas, like computing the time between a case was first opened and the time the status was last set to a given value. An apex scheduled job computes the value every night.
  • In order to test a custom apex+VF scorecard which computes all sorts of metrics out of Cases, I have defined a series of tests comprised of 1) test input cases which will be fed to the scorecard and 2) several scenarios detailing the user filters and expected output. The same code that generate this test input and expected output (in the form of objects) is used for both the apex test and a VF page which displays them, allowing us to check it is computing what it's supposed to.
  • I recently discovered Ruby's cucumber, which is testing some code by parsing a description in plain English ("Given I have entered 'foo' in the field and I press 'create' then I should see the record 'foo' on the list"). I was wondering if anybody has written a similar tool in apex.

Thanks,

Laurent

 

All,

 

I'm trying to write a test method in Apex v25 without seeing pre-existing data, i.e. I have to create my own data.

 

If I was able to create Accounts and Cases, I have a problem creating CaseMilestone and CaseHistory records. I can explicitly create such records, but fields such as CaseMilestone.CaseId or CaseHistory.newValue cannot be updated in apex.

 

CaseMilestones don't seem to be created at case creation (at least I can't see any). And updating tracked fields on the case doesn't seem to create a CaseHistory record.

 

Any idea?

 

All,

 

I don't know about you guys, but deploying some Apex code now takes a *looong* time for me, just because it needs to test all the classes each and everytime.

 

This is why I posted an Idea to optimize the test sequence. There is no reason why SFDC should test apex code which is unrelated to what is deployed - especially when no schema change is being deployed.

 

If you believe it's a worthy idea, please vote for it.

 

Thanks,

Laurent

 

All,

 

Because of a problem in a migration of knowledge base articles from a 3rd party system, we need to update a few thousands articles. Unfortunately, Salesforce doesn't allow to programmatically update the article objects, whether through the API or Apex.

 

The solutions seems to be either update manually the solutions (which is hardly feasible considering the number) or wipe all the solutions and start the migration again (except we've already started using some articles).

 

An alternative I'm investigating is to write a program that would generate a huge Windows PowerShell script that would create an Internet Explorer instance and control it to log in and programmatically modify each article, one at a time. But before going that route I was wondering if people have other suggestions.

 

Thanks,

Laurent

 

Hello,

 

Is it possible to start an Apex job as soon as another job is completed? It looks like Apex jobs cannot call Database.executeBatch()

 

I'm trying to implement a large precomputing job which is composed of several passes, so it's difficult to add all the passes inside the same Database.Batchable object.

 

Thanks,

Laurent

 

I have an issue with the v25.0 .  I am trying to test one of the new functionality that was release in v25.0. " KbManagement.PublishingService.publishArticle(KnowledgeArticleId, true)"

This is suppose to allow you programmatically publish an  KnowledgeArticle that is in draft status.  I wrote a simple class to test this functionality and it worked fine but I have about 120000 records to process to I figure I write a batchable class(seen below). 

 

// 

// (c) 2012 Appirio, Inc.

//

// Description: This class implements the batchable interface and is used to publish  KnowledgeArticles in draft Status  

//               

//

global with sharing class  PublishKnowledgeArticlesBatchClass implements Database.Batchable <sObject>{

 

  global Database.QueryLocator start(Database.BatchableContext BC){

    //String should be modifed depending on the number of records that the Publish Service can support

      String query = 'SELECT Title, Id, KnowledgeArticleId, PublishStatus, VersionNumber '

              + ' FROM KnowledgeArticleVersion ' 

                  +  ' WHERE PublishStatus =\'Draft\' '

                    + '  AND language =\'en_US\' AND LastModifiedDate =TODAY LIMIT 5';                    

        return Database.getQueryLocator(query);

      

    }

 

    global void execute(Database.BatchableContext BC, List<KnowledgeArticleVersion> scope){

       //Call the PublishService with the id of each KnowledgeArticleId

        for(KnowledgeArticleVersion s : scope){

            KbManagement.PublishingService.publishArticle(s.KnowledgeArticleId, true);

        }      

       

    }

 

    global void finish(Database.BatchableContext BC){

  

    }

}

 

However when i run the batch process, this is the response I get in the log.  

 

25.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO

09:50:00.144 (144586000)|EXECUTION_STARTED

09:50:00.144(144629000)|CODE_UNIT_STARTED[EXTERNAL]|01pQ00000001y9b|PublishKnowledgeArticlesBatchClass

09:50:00.164 (164988000)|FATAL_ERROR|Internal Salesforce.com Error

09:50:00.096 (165018000)|CUMULATIVE_LIMIT_USAGE

09:50:00.096|CUMULATIVE_LIMIT_USAGE_END

09:50:00.169 (169593000)|CODE_UNIT_FINISHED|PublishKnowledgeArticlesBatchClass

09:50:00.169 (169604000)|EXECUTION_FINISHED

 

Please any assistance will be greatly appreciated.

Regards

-H

All,

 

I don't know about you guys, but deploying some Apex code now takes a *looong* time for me, just because it needs to test all the classes each and everytime.

 

This is why I posted an Idea to optimize the test sequence. There is no reason why SFDC should test apex code which is unrelated to what is deployed - especially when no schema change is being deployed.

 

If you believe it's a worthy idea, please vote for it.

 

Thanks,

Laurent

 

Anybody hear about any roadmap information for tasks/activities and triggers for Salesforce Knowledge? These two missing items, in particular, severly hamper the customizability/flexibility of the system.

 

One other item is the ability to not just track the number of views, but also who has viewed articles. Is this on the roadmap?

 

Thanks!

Hey all,

 

This is one of those questions when I feel like I'm probably going about a simple process the wrong way and over complicating it, but I'm not sure the alternative. So I'll ask my question and if you have a better approach, please feel free to share.

 

The deal is that I have an Apex REST service that allows a user to pass in an object type, and any field values in the URL.

EX : http://na2.salesforce.com/apex/reservice/contact?firstname=frank&lastname=jones&age__c=21

 

My code works fine for any string values, but chokes on numerics. Because I instantiate a new instance of the type of object they pass in, then I loop over all the fields in the query string and dynamically add them to the sObject. Of course as far as apex is concerned all those arguments in the query string are in fact strings, so when adding non string things to the sObject it explodes. 

 

Ideally I could use the get describe info about the field I'm inserting to find it's type and cast the value to be inserted to the correct type, but as far as I know there isn't a way to do dynamic casting at runtime. A series of if statments is about the only alternative I see, but that feels really dirty. 

 

This is what I have currently (you can see I'm kind of using the if statment path, trying to make it as simple as possible).

 

Just to be clear, this code works, it's just not as efficient/dynamic as I'd like.

 

    public static sObject saveSObject(string objectType, string recordid, RestRequest req)
    {
        //create a generic sObject to contain the update values.
        sObject updateObj;      
                
        //get the describe object for the type of object passed in (its just passed in as a string from the URL)
        Schema.sObjectType objectDef = Schema.getGlobalDescribe().get(objectType).getDescribe().getSObjectType();
        
        //find all the fields for this object type
        Map<String, Schema.SobjectField> ObjectFieldsMap = objectDef.getDescribe().fields.getMap();
        
 
        //this method can handle updates or inserts. If a record ID was passed in, 
        //cast the object as the type represented by the ID. If not, just create a
        //new object of the type found in the object describe.
        if(recordId != null)
        {
            updateObj = objectDef.newSobject(recordid);
        }
        else
        {
            updateObj = objectDef.newSobject();
        }    
        // populate the object's fields by looping over all the params in the rest request.
        for (String key : req.params.keySet())
        {
            // only add params if they are valid field on the object
            if (ObjectFieldsMap.containsKey(key))
            {
                //figure out the type of this field so we can cast it to the correct type
                string fieldType = ObjectFieldsMap.get(key).getDescribe().getType().name().ToLowerCase();
                
                //since I don't know how to do, or if it's even possible to do dynamic casting we need a 
                //series of if statments to handle the casting to numeric types. I think all the others should
                //be fine if left as a string. Dates might explode, not sure.
                
                
                if(fieldType == 'currency' || fieldType == 'double' || fieldType == 'percent' || fieldType == 'decimal' )
                {
                    updateObj.put(key, decimal.valueOf(req.params.get(key).trim())); 
                }
                else if(fieldType == 'boolean')
                {
                    updateObj.put(key, Boolean.valueOf(req.params.get(key))); 
                }                   
                else if(fieldType == 'date')
                {
                    updateObj.put(key, date.valueOf(req.params.get(key))); 
                }                
                else
                {
                    updateObj.put(key, req.params.get(key));
                }
            }
            else
            {
                system.debug('Invalid field: '+ key + ' for object type ' + objectType);
            }
        }
        //update/insert the object
        upsert updateObj;
        
        //return the saved object.
        return updateObj;
        
    }

 

All,

 

Because of a problem in a migration of knowledge base articles from a 3rd party system, we need to update a few thousands articles. Unfortunately, Salesforce doesn't allow to programmatically update the article objects, whether through the API or Apex.

 

The solutions seems to be either update manually the solutions (which is hardly feasible considering the number) or wipe all the solutions and start the migration again (except we've already started using some articles).

 

An alternative I'm investigating is to write a program that would generate a huge Windows PowerShell script that would create an Internet Explorer instance and control it to log in and programmatically modify each article, one at a time. But before going that route I was wondering if people have other suggestions.

 

Thanks,

Laurent

 

I am facing weird problem with Run all test.  I have trigger and respective test class. When I run individual run test its coverage is 97% but then when I do Run All test for my org it showing 52% fro same trigger. When I see graphical coverage red lines are on commented line and blank line of trigger :( Is this normal bug? Or special case?

 

Help me.....

 

 

 

San

Hi All,

 

I would like to be able to call the SF Metadata API from Apex code in my custom app.

 

I went to Setup/App Setup/Develop/API and clicked on 'Download Metadata WSDL' and saved it to my hard drive.

 

Then I went to Setup/App Setup/Develop/ApexClasses and clicked on the 'Generate from WSDL' button. For Step 1, I navigate to the Metadata WSDL that I just saved and click 'Parse WSDL.' This takes me to the Step 2 screen where I then click on the 'Generate Apex code' button.

 

This takes me to the Step 3 screen which shows this error:

 

<error>

Apex generation failed.

Error message:
Error: Class name 'Metadata' already in use. Please edit WSDL to remove repeated names

 

</error>

 

Does this error mean that what I am attempting to do is not allowed? If it is allowed, how do I get around this error.

 

Any help would be appreciated.

 

Thanks,

Andrew