0

I am using model window VF page to add attachments in another Vf(Main) page. Attachments is getting added to the case. once the attachment is added, the entire page is redirect to Modal window page(Refreshes), Its not staying in main page.

Javascript code in Main VF page:

 $scope.getAttach = function(sNORId, sCaseId, sRequestFrom) {

                var baseUrlLink = $scope.getBaseURL();
                $('#getAttachId').dialog({
                    modal: true,
                    open: function ()
                    {
                        $(this).load(baseUrlLink+'/apex/Rforce_GetAttachmentTypes_VFP?nId='+sNORId+'&csId='+sCaseId+'&reqName='+sRequestFrom);
                    },
                    close: function (e) {
                        $(this).empty();
                        $(this).dialog('destroy');
                    },         
                    height: 400,
                    width: 400,
                    title: 'Add Attachments'
                });
            }

Modal Window VF page:

<apex:page controller="Rforce_GetAttachmentTypes_CTR" showHeader="false" sidebar="false" standardStylesheets="false">
    <apex:form >
        <apex:pageBlock >
            <apex:repeat value="{!Attachments}" var="a" rendered="{!Attachments.size>0}">
                <label>{!a.oATTLinked.Name}</label>
                <apex:inputFile value="{!a.oAttachment.body}" filename="{!a.oAttachment.name}" id="file1"/>
            </apex:repeat>
            <apex:commandButton value="Upload" action="{!uploadAttachments}"/>
        </apex:pageBlock>
    </apex:form>

</apex:page>

Controller for model window:

public class Rforce_GetAttachmentTypes_CTR {
    public List<attachmentTypes> lAttachmentType{get;set;}
    public string sNaofReId = ApexPages.currentPage().getParameters().get('nId');
    public string caseId = ApexPages.currentPage().getParameters().get('csId');
    public string requestName = ApexPages.currentPage().getParameters().get('reqName');
    public Rforce_GetAttachmentTypes_CTR(){        
    }
  //Getting List of attachments types 
    public List<attachmentTypes> getAttachments(){
        lAttachmentType = new List<attachmentTypes>();
        if(!String.isBlank(sNaofReId)){
            NAT_Request__c oNaOfRe = [Select id,(select id,Name,Mandatory__c from Attachments_Linkeds__r) 
                                      FROM NAT_Request__c Where Id=:sNaofReId];
            if(!oNaOfRe.Attachments_Linkeds__r.isEmpty()){
                for(Integer i=0 ;i<oNaOfRe.Attachments_Linkeds__r.size();i++){
                    lAttachmentType.add(new attachmentTypes(new attachment(),oNaOfRe.Attachments_Linkeds__r[i])); 
                } 
            }
        }else
            lAttachmentType.add(new attachmentTypes(new attachment(),new ATT_Linked__c()));
            System.debug('testttttttttt------------->'+lAttachmentType);     
        return lAttachmentType;
    }
  //inseting attachments for a case
    public void uploadAttachments(){
        List<Attachment> lAttachments = new List<Attachment>();
        for(attachmentTypes atTypes : lAttachmentType){
            if(atTypes.oAttachment.Body!=null && !String.isBlank(atTypes.oAttachment.name)){
                atTypes.oAttachment.ParentId= caseId; 
                if(requestName == 'finance') {
                    atTypes.oAttachment.Description= 'custinfoattachment';
                }else {
                    atTypes.oAttachment.Description= 'fininfoattachment';
                }
                lAttachments.add(atTypes.oAttachment); 
            }
        }
        if(!lAttachments.isEmpty()){
            try{
                DataBase.Insert(lAttachments,False);

            }catch(Exception e){
                system.debug('Unable to insert Attachments'+e);
            }finally{
                lAttachmentType=null;    
            }
        }
    }
  //Wrapper Class to get attachment and attachment name  
    public class attachmentTypes{
        public ATT_Linked__c oATTLinked{get;set;}
        public Attachment oAttachment{get;set;}
        public attachmentTypes(Attachment oAtt,ATT_Linked__c oAttLink){
            oAttachment=oAtt;
            oATTLinked=oAttLink;
        }
    }
}

Before Upload button is clicked in Modal Window : enter image description here

After Upload button is clicked in Modal Window : enter image description here

0

The apex:commandButton by default will reload the whole page after the user clicks on it. If you want to workaround it - add a reRender attribute to the button:

<apex:commandButton value="Upload" 
                    action="{!uploadAttachments}"
                    reRender="none"/>
3
  • I am not using apex tags for upload button. I am using HTML tags i tried in that <button reRender="none" ng-click="getAttach('',caseIdInfo)" id="fileUploadId">. Its not working . – Sathish Aug 12 '16 at 8:20
  • @Sathish So you have posted a wrong code? – Sergej Utko Aug 12 '16 at 8:55
  • I am sorry . I have tried with your answer. Its not working. – Sathish Aug 12 '16 at 10:36

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.