• Larry Leonidas
  • NEWBIE
  • 0 Points
  • Member since 2011

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

I'm facing a problem trying to search for Content. We are basically trying to return search results using SOSL on the ContentVersion object. We're expecting full-text search results (words within the PDF or DOC), but results are returned only on ContentVersion.Title.

 

List<List<SObject>> contentresults1 = [FIND :SearchVal IN ALL FIELDS RETURNING ContentVersion (id, Title, Description)];

I'm trying to Upload the trigger below into our production environment and receiving the errors below. Test Coverage for the trigger is 85%. Not sure why I would get the errors below:

 

 

 

trigger ExpenseReportUpdate on Expense__c (before insert) {

Expense__c exp = Trigger.new[0];

date trx_d = exp.date__c;
Integer trx_m = trx_d.month();
Integer trx_y = trx_d.year();
String exname = exp.Employee_First_Name__c + ' ' + exp.Employee_Last_Name__c;
String exname_id;
String expR_id;
date trx_st = trx_d.toStartOfMonth();
date stdate = trx_st.addmonths(1);

    try {
        exname_id = [SELECT id
                    FROM User
                    WHERE name = :exname].id;
        }
        
    catch (QueryException e) {
        exname_id = [SELECT id
                    FROM User
                    WHERE name = 'Force Admin'].id;
        }
          
    try { 
    
        expR_id = [SELECT id
            FROM Expense_Report__c
            WHERE CALENDAR_MONTH(Statement_Date__c) = :trx_m
            AND CALENDAR_YEAR(Statement_Date__c)= :trx_y
            AND Employee__c = :exname_id].id; 
            
        exp.Expense_Report__c = expR_id;
       
        }
                    
    catch (QueryException e) {
            Expense_Report__c newER = new Expense_Report__c(Employee__c = exname_id, Statement_Date__c = stdate);
            insert newER;
            
            exp.Expense_Report__c = newER.id;       
    }
    
}

 

 

ERRORS I GET:

MyProfilePageController.testSave()            Failure Message: "System.QueryException: List has no rows for assignment to SObject", Failure Stack Trace: "Class.MyProfilePageController.testSave: line 78, column 35 External entry point"

"not even referencing this?"

ExpenseReportUpdateTest  Coverage of selected Apex Trigger is 0%, at least 1% test coverage is required

"coverage is 89%?"

Deploy Error Average test coverage across all Apex Classes and Triggers is 66%, at least 75% test coverage is required

"not sure why this is happening"

I'm facing a problem trying to search for Content. We are basically trying to return search results using SOSL on the ContentVersion object. We're expecting full-text search results (words within the PDF or DOC), but results are returned only on ContentVersion.Title.

 

List<List<SObject>> contentresults1 = [FIND :SearchVal IN ALL FIELDS RETURNING ContentVersion (id, Title, Description)];

I have custom fields on the ContentVersion object and I need to test querying ContentVersion based on these custom fields in my Apex test method.

 

I've tried creating a new ContentVersion in my test method:

--------------------------------------------------------------------------------

ContentVersion contentVersionObj = newContentVersion();

contentVersionObj.ContentURL = 'http://www.google.com';

contentVersionObj.title = 'Google';

contentVersionObj.Enablement_Area__c = 'Acct Mgmt';

insert contentVersionObj;

--------------------------------------------------------------------------------

 

However, when I try to test that, I got an error:

--------------------------------------------------------------------------------

15:55:36.756 (756244000)|EXCEPTION_THROWN|[136]|System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, You cannot set custom fields or tags on a document published into a personal library. Fields set: Enablement Area: []

15:55:36.757 (757794000)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, You cannot set custom fields or tags on a document published into a personal library. Fields set: Enablement Area: []

--------------------------------------------------------------------------------

 

Ok, so based on the ContentVersion docs (http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_contentversion.htm) I need to assign a value to FirstPublishedLocationId in order to have the ContentVersion not be assigned to a personal library.

--------------------------------------------------------------------------------

ContentWorkspace library = [SELECT id FROMContentWorkspace LIMIT 1];

ContentVersion contentVersionObj = newContentVersion();

contentVersionObj.ContentURL = 'http://www.google.com';

contentVersionObj.title = 'Google';

contentVersionObj.Enablement_Area__c = 'Acct Mgmt';

contentVersion.FirstPublishLocationId = library;

insert contentVersionObj;

--------------------------------------------------------------------------------

 

Only, when I run that, I get another error:

--------------------------------------------------------------------------------

Expression cannot be assigned

--------------------------------------------------------------------------------

 

So, I guess I can't assign to the FirstPublishLocationId field...

 

Is there any way to create a ContentVersion object with a custom field in an Apex test method?

 

I'm doing this all in a sandbox and using the Force.com IDE if it matters.

 

Thanks,

Matt

I've just discovered that if a test calls code that generates a PDF, then the test always passes.

 

Generating a PDF cause code execution to fall into a black hole of success, which is a shame, because sometimes tests are supposed to fail.

 

Here's a test that passes:

 

private class KerryTest {
    class MyException extends Exception {}
    public static testMethod void thisIsCrazy() {

          PageReference pdf = Page.ForgotPassword;       

          Blob pdfBody = pdf.getContentAsPDF();


          throw new MyException('This test should fail!');

    }

}

 

.... it really, really, shouldn't!

 

I know that the docs say I can't call "getContentAsPDF" in a test, which is itself ridiculous, but SURELY it should throw an error, or return an empty PDF, or SOMETHING -- not simply disappear into the ether?

 

Can somebody who knows something explain? I'd love to hear the justification for this behaviour.

 

I've just wasted an awful lot of time figuring out why my test wasn't failing, when I knew it should've been!

 

Thanks

Kerry 

 

 

I figured out how to create and link ContentVersion objects from Apex code. Now I want to search through the content of these objects using Apex. Is this possible at all? I can not find any thing about this. Has somebody succeeded in doing this?

 

I'm trying to Upload the trigger below into our production environment and receiving the errors below. Test Coverage for the trigger is 85%. Not sure why I would get the errors below:

 

 

 

trigger ExpenseReportUpdate on Expense__c (before insert) {

Expense__c exp = Trigger.new[0];

date trx_d = exp.date__c;
Integer trx_m = trx_d.month();
Integer trx_y = trx_d.year();
String exname = exp.Employee_First_Name__c + ' ' + exp.Employee_Last_Name__c;
String exname_id;
String expR_id;
date trx_st = trx_d.toStartOfMonth();
date stdate = trx_st.addmonths(1);

    try {
        exname_id = [SELECT id
                    FROM User
                    WHERE name = :exname].id;
        }
        
    catch (QueryException e) {
        exname_id = [SELECT id
                    FROM User
                    WHERE name = 'Force Admin'].id;
        }
          
    try { 
    
        expR_id = [SELECT id
            FROM Expense_Report__c
            WHERE CALENDAR_MONTH(Statement_Date__c) = :trx_m
            AND CALENDAR_YEAR(Statement_Date__c)= :trx_y
            AND Employee__c = :exname_id].id; 
            
        exp.Expense_Report__c = expR_id;
       
        }
                    
    catch (QueryException e) {
            Expense_Report__c newER = new Expense_Report__c(Employee__c = exname_id, Statement_Date__c = stdate);
            insert newER;
            
            exp.Expense_Report__c = newER.id;       
    }
    
}

 

 

ERRORS I GET:

MyProfilePageController.testSave()            Failure Message: "System.QueryException: List has no rows for assignment to SObject", Failure Stack Trace: "Class.MyProfilePageController.testSave: line 78, column 35 External entry point"

"not even referencing this?"

ExpenseReportUpdateTest  Coverage of selected Apex Trigger is 0%, at least 1% test coverage is required

"coverage is 89%?"

Deploy Error Average test coverage across all Apex Classes and Triggers is 66%, at least 75% test coverage is required

"not sure why this is happening"

I've written a custom controller that exploits the StandardSetController class to return a page of records and allow pagination.

 

VF controller has three (relevant) methods:

 

getPage()  - returns List of records in current pageset (pagesize = 10)

previousPage() - executes the previous() method on the standard set controlller object

nextPage() - executes the next() method on the standard set controller object

 

The StandardSetController is constructed using a Database.getQueryLocator per the doc

 

setCtlr = new ApexPages.StandardSetController(

Database.getQueryLocator(

[Select id, name, Account__r.name from Foo__c where id in :fooIdSet]));

When I set up my APEX test method:

 

1.  Insert 11 rows into database  //works ok

2.  Assert that getPage() returns 10 rows  // assertion passes

3.  Execute nextPage() on the controller

4.  Assert that getPage() returns 1 row (the 11th)

 

Here's the problem: step #3 fails because VF comes back with:

 

System.VisualforceException: Modified rows exist in the records collection!

 

I checked the creation datetime and lastmodify datetime just prior to executing Step3 and all is OK, the records haven't mysteriously been changed underneath.

 

I get the same error (in a non-Sites test whilst logged in as admin and simply executing the VF page in the browser.

 

What would cause this error?

Message Edited by crop1645 on 04-08-2009 05:12 PM
Message Edited by crop1645 on 04-08-2009 05:14 PM
The "MIXED_DML_OPERATION" issue that plagues unit tests (see this forum discussion) is now effecting production code as well.

Our application adds an extra custom field to Users called "Other Emails".  This is a string field where a User can enter a bunch of email aliases; whenever it's updated we normalize the information into a separate "EmailAddress" table and the Address then points back at the User.

As of yesterday, that worked great.  I updated my User record to add an address to the "Other Emails" field.  The trigger code executed perfectly, and the EmailAddress object was created & linked to my User record.

As of today, it is failing:



This is a critical part of an Apex-based application that we have been developing for over 8 months, and this section of code has always worked until today.

We've had to comment out unit tests to deal with this issue - which was lame, but acceptable - but actually changing our object relationships and code at this late date would be difficult.
  • June 19, 2008
  • Like
  • 0