0

Trying to update a child object from a parent object using the Objecttype returned form a getReferenceTo() but getting errors as Method does not exist or incorrect signature: How can I convert the ObjectType to an sobject?

Would appreciate insight on this.

getRefernceTo is returning the MSTR detail field from the child object as seen in the debug here.

DEBUG|GetReferenceTo for child : (Claims_Client_Risk_Assessment__c)

DEBUG|GetReferenceTo for child[0] : Claims_Client_Risk_Assessment__c

this.CCRA = (Claims_Client_Risk_Assessment__c) controller.getRecord();


        public PageReference saveOverride(){
            try{
               Database.update( CCRA );
               this.LOB_NonStdAuto = queryLOB_NonStdAuto(record.Id);
               update LOB_NonStdAuto.Claims_Client_Risk_Assessment__r;
            }
            catch( Exception ex ){
                ApexPages.addMessage( new ApexPages.Message( ApexPages.Severity.ERROR, ex.getMessage() ));
                return null;
            }

                //Get reference To NonStdAuto child object:
                Schema.DescribeFieldResult dfr = CCRA_LOB_NonStdAuto__c.Claims_Client_Risk_Assessment__c.getDescribe();
                List<Schema.sObjectType> child = dfr.getReferenceTo();
                    System.debug('GetReferenceTo for NonStdAUTO : ' + child );
                    System.debug('Point to Field using GetReferenceTo for NonStdAUTO : ' + child[0] );         

             saveChildren(child[0]);

                PageReference pageRef = null;
                pageRef = new PageReference('/' + this.CCRA.Id);
                pageRef.getParameters().put('inline', '0');
                pageRef.setRedirect(true);
                return pageRef;
        }
    //**********************************************************

        public void saveChildren(sObject child ){
            try{
                 Database.update( child );
            }
            catch( Exception ex ){
                system.debug( 'Error saving child: ' + ex.getMessage() );
            }
        }

    //**********************************************************
    Private CCRA_LOB_NonStdAuto__c queryLOB_NonStdAuto(Id CCRA_ID) {    
        LOB_NonStdAuto = [
            SELECT 
                Id
                ,Claims_Client_Risk_Assessment__c
            FROM 
                CCRA_LOB_NonStdAuto__c 
            WHERE 
                IsDeleted = false 
                AND 
                Claims_Client_Risk_Assessment__c = :CCRA_ID 
            ORDER BY CreatedDate DESC NULLS FIRST LIMIT 1
       ];              
            return LOB_NonStdAuto; }
  • Error is at saveChildren(child[0]); – rickmac Jul 10 '18 at 3:30
  • 1
    To update an object you need to have queried for it first. – cropredy Jul 10 '18 at 3:40
1

The sObjectType is a "token" that represents a "type" of record (e.g. Account.sObjectType represents an Account token). Instead, you should query the record you want to update, then you can update the record. The sObjectType token is primarily used to (a) determine if an sObject is a specific sObjectType, (b) create an instance of a specific sObject type, and (c) get describe information for the sObjectType.

In order to update a record, you need to have an sObject, not an sObjectType. This is typically done by either creating a new record in memory (e.g. new Account(...)) or by querying a record (sObject record = [select ... from ... where ...]). Without seeing more of your code, I'm not sure how we can help you, but generally speaking, you just need to query the related record.

| improve this answer | |
  • Thanks. I may have been mislead on using sobjecttype. I do have a handle on the child object already having established its master detail relationship . If if try to update it the values entered on the Visual Force components page clear out and not saved. I have a previous post that someone(s) put on hold? So it’s getting difficult to provide details in the forum. So I’m trying to keep my examples lite? – rickmac Jul 10 '18 at 3:52
  • if you have an example that shows the query approach then I can refactor my code above to illustrate my issue. – rickmac Jul 10 '18 at 3:55
  • @rickmac developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… getSObject(fieldName) method – Ronnie Jul 10 '18 at 4:06
  • @rickmac If you already have the record, you can update the record. I'm guessing something like this: update record.Claims_Client_Risk_Assessment__r; – sfdcfox Jul 10 '18 at 4:33
  • Adopted updating the child record in code above. However, when executing the saveOverride() the fields on the component object are being cleared and not saved. @sfdcfox – rickmac Jul 10 '18 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.