You need to sign in to do that
Don't have an account?
Debbie61
Problem: Invalid type: QueryResult
I am trying to write a trigger that will check to see if an xls spreadsheet attachment has been uploaded for a custom object. In the API, Attachments are of type="tns:QueryResult". But when I deploy my trigger to Salesforce, I get an error back that says: Problem: Invalid type: QueryResult
Here is the code:
trigger dmbSurveyContainsSpreadsheet on dmb_survey__c (after update) { for(dmb_survey__c obj : Trigger.new) { Boolean contains = false; if (obj.Status_Setting__c == 'Complete') { QueryResult qr = obj.Attachments; if (qr != null) { for (int i = 0; i < qr.records.Length; i++) { Attachment attach = (Attachment)attachQR.records[i]; string contentType = attach.ContentType; contains = contentType.Contains('ms-excel'); if (contains) { break; } } } if (!contains) { obj.Contains_Spreadsheet__c = 1; } } } }
Could somebody please tell me what I am doing wrong? Thank you!
I do not know why Apex code is so painful. I can code Java and C# til the cows come home. Here is the final solution that works.
All Answers
Try :
You are confusing the Java code and Apex. They are differences when you write something in Java v/s Apex.
Also, you will have to query for the Attachment related to the records in your trigger first. Trigger does'nt automatically bring the related objects when it executes your code..
I have re-written my query but am getting a new error that claims that the following statement must be a list type, which I am not understanding why it would be a List type at this point.
The error thrown is: Compile Error: Expression must be a list type: SOBJECT:Attachment at line 25 column 42
The entire code block follows:
String contentType = attachMap.get(obj.Id)[i].ContentType;
contentType is an Id, Attachment map
When you do attachMap.get(obj.Id) you dont need to use the i index cause you are not in a list or array it is an attachment
all you need to do is
attachMap.get(obj.Id).ContentType;
That will gives you the content type of the attachment related to the Id on the map.
Ignacio.
When I revise it, I get an error when I try to save the record that the reference is null for that line of code. The record does have an attachment but my query for getting the attachments must not be right?
Error: System.NullPointerException: Attempt to de-reference a null object: On this line:
Here is the modified code:
I do not know why Apex code is so painful. I can code Java and C# til the cows come home. Here is the final solution that works.