function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jim BoudreauxJim Boudreaux 

Compile Error: DML requires SObject or SObject list type: MyInspection

Here's my code.

 

Visual Force:

 

<apex:page controller="sendEmail" tabStyle="Inspection__c"> <apex:messages /> <apex:pageBlock title="Designated Totalreport Recipients."> <apex:repeat value="{!account.account_contacts__r}" var="contact"> {!contact.EMAIL__c}, </apex:repeat> <apex:form > <br /> <apex:commandButton value="Email TOTALREPORT" action="{!send}" /> </apex:form> </apex:pageBlock> <apex:detail /></apex:page>

 And here is my Custom Controller:

 

public class sendEmail { public String subject = 'Text Only Version of TOTALREPORT'; public String body = 'This is your TOTALREPORT.'; public string id = ApexPages.currentPage().getParameters().get('id'); //inspection ins = [Select building__c from inspection__c where id = :id]; myInspection ins = new myInspection(id); private final account_c__c account; public myInspection getIns() { return ins; } public sendEmail() { account = [Select (Select EMAIL__c From account_contacts__r where TR_Recipient__c = true) From Account_c__c a where id = :ins.buildingid]; } public account_c__c getAccount() { return account; } public PageReference send() { // Define the email Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); // Reference the attachment page and pass in the account ID PageReference pdf = Page.totalreport; pdf.getParameters().put('id',this.id); pdf.setRedirect(true); // Take the PDF content Blob b = pdf.getContent(); // Create the email attachment Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment(); efa.setFileName('TOTALREPORT.pdf'); efa.setBody(b); String addresses; if (account.account_contacts__r != null) { addresses = account.account_contacts__r[0].EMAIL__c; // There may be more contacts, so loop through the whole list for (Integer i = 1; i < account.account_contacts__r.size(); i++) { if (account.account_contacts__r[i].EMAIL__c != null) { addresses += ':' + account.account_contacts__r[i].EMAIL__c; } } } //String bccAddresses = 'totalreportmailinglist@criticalsystems.us'; //bccAddresses += ':' + ins.lead_inspector__r.email__c; String[] toAddresses = addresses.split(':', 0); //String[] tobccAddresses = bccaddresses.split(':', 0); String[] tobccAddresses = new String[] {'totalreportmailinglist@criticalsystems.us'}; // Sets the paramaters of the email email.setSenderDisplayName('Critical Systems'); email.setSubject( subject ); email.setBccAddresses( tobccAddresses ); email.setReplyTo('totalreportmailinglist@criticalsystems.us'); email.setToAddresses( toAddresses ); email.setPlainTextBody( body ); email.setUseSignature(false); email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa}); ins.distributed = true;

update ins; 

// Sends the email Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email}); inspection__c thisIns = new Inspection__c(id = ins.id); return null; }}

 This is all just a slightly modified version of the Email an Attachment from Visualforce example in the Force.com Visualforce Development documentation found on page 112.

 

I have a checkbox that I want to change to True when the user clicks the send button. I am trying to do this via the two lines "ins.distributed = true;  update ins;"

 

But it won't work.

 

Why not? 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
mikefmikef

Jim:

 

you are trying to perform DML on the ins variable and I am assuming that is causing you the issue.

The ins variable is an object called myInspection, but myInspection is not a standard sObject and if it's a custom object in your org then you should add the __c to the end of the object name.

My guess is you also have myInspection as an apex class?  Or maybe the compiler is just erroring out on the DML line before it's get to the myInspection can't be constructed with Id error that you will get soon.

All Answers

mikefmikef

Jim:

 

you are trying to perform DML on the ins variable and I am assuming that is causing you the issue.

The ins variable is an object called myInspection, but myInspection is not a standard sObject and if it's a custom object in your org then you should add the __c to the end of the object name.

My guess is you also have myInspection as an apex class?  Or maybe the compiler is just erroring out on the DML line before it's get to the myInspection can't be constructed with Id error that you will get soon.

This was selected as the best answer
Jim BoudreauxJim Boudreaux

You're absolutely right, myInspection is a custom Apex Class. This whole issue was a brain fart on my part. I realized it about ten minutes after posting, but I had to run an errand and couldn't delete the post. Oh well.

 

At least I got independent verification and you got to exercise your debugging skills.  

 

I adjusted my code accordingly and it works now. Thanks again.