• mh@reed
  • NEWBIE
  • 0 Points
  • Member since 2011

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

Scenario:  inserting an object into the database that causes an error.

 

I'm confused by the options here.  If I use a try/catch block,  I can gracefully handle and report the error - great.  What happens if I'm doing the following (without the use of a try/catch block):

 

Database.SaveResult[] results = databse.insert(sObject);

 

If the insert fails,  does the code crash or can I then iterate through the SaveResult array,  test for an error and then report it?

 

Should both methods be used at the same time and what happens if they are?

 

I'm writing some code to take an unkonown custom object and create a sharing record for it.  I'm able to grab the object name using the DescribeSObjectResult object,  work out the sharing object name and then create an instance of it using newSobject and assign it to a sObject.  However,  when I attempt to add data to the object's fields,  I get a "Field expression not allowed for generic SObject" error.  Typically,  I'd get around this by casting the sObject to the object that I want to use,  but,  I can't do that here because I don't want to hardcode the object type (this code could be used to provide sharing for a range of records).

 

Here's the code to quickly get to where I am.

 

sObject objToShare = new CustomObject__c();

Schema.SObjectType objObjectType;
DescribeSObjectResult objDescribeResult;
string strObjectName;
string strShareObjectName;
Map<String, Schema.SObjectType> mapAllSobjects = Schema.getGlobalDescribe();
Sobject objShareRecord;


//What object are we dealing with?
objObjectType = objToShare.getSObjectType();
system.debug('1***  objObjectType: ' + objObjectType);

//Get the name of the object
objDescribeResult = objObjectType.getDescribe();
strObjectName = objDescribeResult.getName();

//Get the name of the sharing object associated with it
strShareObjectName = strObjectName.substring(0, strObjectName.lastIndexOf('__c')) + '__Share';
system.debug('2***  strShareObjectName: ' + strShareObjectName);

//Get an instance of the object
objObjectType = mapAllSobjects.get(strShareObjectName);
objShareRecord = objObjectType.newSobject();
system.debug('3***  objShareRecord: ' + objShareRecord);

//Populate the record
objShareRecord.AccessLevel = 'edit'; //!***   ERROR   ***!//

 

I've tried a couple of things to resolve this without any success and I'm sure that this problem must be encountered fairly regularly,  does anyone have a way to sucessfully cast the sObject?

 

I have a Vf page that is based on a custom object which we'll call CustomQuote.  CustomQuote holds fields which we'll call BillToContact and SoldToContact which are a lookup reference to a standard contact object.  My controller code can examine aspects of the contact through the lookup relationship, e.g  CustomQuote.BillToContact__r.FirstName and in fact does this to validate that the appropriate data has been entered.  The page displays the value from this relationship without any problems.

 

My problem begins when writing the test class for the controller.  I create a CustomQuote and pass it through to the controller in the usual fashion,  i.e.

 

pageRef = Page.MyCustomQuotePage;

MyController controller;

 

Test.setCurrentPage(pageRef);

controller = new MyController(new ApexPages.StandardController(CustomQuote));

 

If I debug the CustomQuote object from the controller,  the CustomQuote is fully populated and contains a contact id for the bill-to and sold-to fields.  However,  when the validation piece runs (is CustomQuote.BillToContact__r.FirstName populated?) it always returns false because the relationship pulls back a null value.  I've coded a small select statement to pull back the contact details and they are in the database.

 

So,  my problem is:  a relationship such as CustomQuote.BillToContact__r.FirstName works in practice,  but,  cannot be tested because under those circumstances it does not.  Am I doing something wrong and,  if not,  how are people getting around it other than just ignoring this useful relationship functionality?