+ Start a Discussion
lopezclopezc 

Problems creating an Attachment record

Hi,

 

i am getting the following exception when creating an attachment:

 

System.Exception: DML currently not allowed 

Class.AttachmentSearchController.getInsertAttachment: line 18, column 44External entry point 

 

It is failing on this line of my code: Database.SaveResult insertResult = Database.insert(attachment); 

 

and my attachment looks like this:   

 Attachment Attachment:{Name=Test Attachments.txt, ParentId=003S0000005w8WiIAI, Body=Blob[0]}

 

 Any ideas why it is failing?

Message Edited by lopezc on 01-28-2010 08:21 AM
lopezclopezc

I have to say that I am calling a method from my VF page within a JavaScript function:

 

<script>

function post_value(){

var attId = "{!insertAttachment}";   

alert(attId); 

if(attId != "" && attId != null){      

parent.window.opener.document.getElementById('j_id0:requestForm:j_id54:j_id55:j_id57:j_id58:j_id62:att').value = attId;

parent.window.close();

}                

}

</script> 

 

And the insertAttachment method looks like this:

 

 public String getInsertAttachment(){

     String attId;

     if(attachment!=null){

    attachment.ParentId = '003S0000005w8WiIAI'; //Ramdom contact id    

    if(attachment.Name != null && attachment.Body != null && attachment.ParentId != null){

    try{

    System.debug('Attachment ' + attachment);

    Database.SaveResult insertResult = Database.insert(attachment); 

    if(insertResult.getErrors().size() > 0){

            

        } else if(insertResult.isSuccess()){

        attId = insertResult.getId();        

        }     

    } catch (Exception ex){

    System.debug('System exception; ' + ex.getMessage());

    }

    }    

     }

     return attID;

    } 

 

Do you think this might be related? 

bob_buzzardbob_buzzard

Are you using this from a VF page or a component?  If its a component, you need to set the allowDML attribute to true in the component declaration. 

 

 

lopezclopezc

no, it is made within an apex:page

 

I changed the code. If I call the same method from an action in a commandButton it works. However it requires a second button in my page what is very annoying:

 

<apex:commandButton action="{!insertAttachment}" value="Send Attachment"/> 

<apex:commandButton onclick="post_value()" value="Close Window"/> 

</apex:panelGrid>

<script>

function post_value(){

var attId = "{!attId}";   

if(attId != "" && attId != null){      

parent.window.opener.document.getElementById('j_id0:requestForm:j_id54:j_id55:j_id57:j_id58:j_id62:att').value = attId;

parent.window.close();

}                

}

</script>

Message Edited by lopezc on 01-29-2010 03:56 AM
bob_buzzardbob_buzzard

It may be that your method starts with get - I know VF is funny about DML in constructors, and it will consider that method a property getter in some circumstances.  Try removing the get and calling it insertAttachment.