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
Masechaba Maseli 8Masechaba Maseli 8 

Webservice Callout from visualforce

Hi all
I am attempting to do a webservice call out from a visualforce page- currently I can see that it is connecting with the api but no information is being sent. Can someone please let me know what I am doing wrong.
public with sharing class AttachmentUploadController {

   public Shipment_Order__c shipmentOrder {get;set;}
   public String fileName {get;set;}
   public Blob fileBody {get;set;}
  
   public Customs_Clearance_Documents__c customsclearancedocs {get;set;}
  
    
    //constructor
    
    public AttachmentUploadController (ApexPages.StandardController controller) {
        this.shipmentOrder =  (Shipment_Order__c)controller.getRecord();                            
    
    }

 // creates a new Attachment__c record
    private Database.SaveResult saveCustomAttachment() {
        Attachment__c obj = new Attachment__c ();
        obj.Shipment_Order__c = shipmentOrder.Id; 
        return Database.insert(obj);
    }
    
    // create an actual Attachment record with the Contact_Attachment__c as parent
    private Database.SaveResult saveStandardAttachment(Id parentId) {
        Database.SaveResult result;
        
        Attachment attachment = new Attachment();
        attachment.body = this.fileBody;
        attachment.name = this.fileName;
        attachment.parentId = shipmentOrder.id;
        // inser the attahcment
        result = Database.insert(attachment);
        // reset the file for the view state
        fileBody = Blob.valueOf('attachment.body');
        return result;
    }
    
    /**
    * Upload process is:
    *  1. Insert new Contact_Attachment__c record
    *  2. Insert new Attachment with the new Contact_Attachment__c record as parent
    *  3. Update the Contact_Attachment__c record with the ID of the new Attachment
    **/
    public PageReference processUpload() {
            Database.SaveResult customAttachmentResult = saveCustomAttachment();
        
            if (customAttachmentResult == null || !customAttachmentResult.isSuccess()) {
                ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 
                  'Could not save attachment.'));
                return null;
            }
        
            Database.SaveResult attachmentResult = saveStandardAttachment(customAttachmentResult.getId());
        
            if (attachmentResult == null || !attachmentResult.isSuccess()) {
                ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 
                  'Could not save attachment.'));            
                return null;
            } else {
                // update the custom attachment record with some attachment info
                Attachment__c customAttachment = [select id from Attachment__c where id = :customAttachmentResult.getId()];
                
            update customAttachment;
           }
            
           
      return new PageReference('/apex/Attachment?id='+shipmentOrder.Id);
    }
    
    
    public PageReference Ephesoft() {
        Xmlstreamwriter xmlW = new Xmlstreamwriter();
            xmlW.writeStartDocument('utf-8','1.0');
            xmlW.writeStartElement(null,'BatchMetadata', null);
            
                    xmlW.writeStartElement(null,'Field',null);
                    xmlW.writeStartElement(null,'RecordID',null);
                    xmlW.writeCharacters(String.valueOf(attachment.Id));
                    xmlW.writeEndElement(); //Close RecordID
                    xmlW.writeEndElement(); //Close RecordID
                    
                    xmlW.writeStartElement(null,'Field',null);
                    xmlW.writeStartElement(null,'ParentID',null);
                    xmlW.writeCharacters(shipmentOrder.Id);
                    xmlW.writeEndElement(); //Close ParentID
                    xmlW.writeEndElement();
                    
                    xmlW.writeStartElement(null,'Field',null);
                    xmlW.writeStartElement(null,'ShipmentOrderNumber',null);
                    xmlW.writeCharacters(shipmentOrder.Name);
                    xmlW.writeEndElement(); //Close ShipmentOrderNumber
                    
                   xmlW.writeEndElement();
                    xmlW.writeStartElement(null,'Field',null);
                    xmlW.writeStartElement(null,'ShipmentOrderNumber',null);
                        xmlW.writeCharacters(shipmentOrder.Name);
                    xmlW.writeEndElement(); //Close ShipmentOrderNumber
                    xmlW.writeEndElement();
                    
                    xmlW.writeStartElement(null,'Field',null);
                    xmlW.writeStartElement(null,'SF_Incoterms',null);
                        xmlW.writeCharacters(shipmentOrder.Name);
                    xmlW.writeEndElement(); //Close SF_Incoterms
                    xmlW.writeEndElement();
                    
                    xmlW.writeStartElement(null,'Field',null);
                    xmlW.writeStartElement(null,'SF_NotifyParty',null);
                        xmlW.writeCharacters(shipmentOrder.Name);
                    xmlW.writeEndElement(); //Close SF_NotifyParty
                    xmlW.writeEndElement();
         xmlW.writeEndElement(); //Close BatchMetadata
         xmlW.writeEndDocument(); //Close XML document
   
         
    String xmlStringxmlRes = xmlW.getXmlString();
    System.debug('The XML :'+xmlW.getXmlString());     
    xmlW.close();


    
    String b64String = EncodingUtil.base64Encode(filebody);

// assemble the body payload
    String body = b64String + '\n';
    String body2 = (String.valueOf(xmlW));
    String bodyPayload = body + body2;

// send out the request
      HttpRequest req = new HttpRequest();
      req.setHeader('Content-Type', 'multipart/form-data; boundary=');
      req.setMethod('POST');
      req.setEndpoint('http://34.244.26.115:8080/dcma/rest/uploadBatch/BC8/file');
      req.setBody(bodyPayload);

    Http http = new Http();
      http.send(req);
       System.debug(req.toString());
    return null; 
    
    }    
    
   

}
 
<apex:page standardController="Shipment_Order__c" tabStyle="Shipment_Order__c" extensions="AttachmentUploadController">

 <apex:sectionHeader title="{!Shipment_Order__c.Name}" subtitle="Attach File"/>
 
 <apex:form id="form_Upload">
 <apex:pageBlock >

 <apex:pageBlockButtons >
   <apex:commandButton action="{!Cancel}" value="Cancel"/>
   <apex:commandButton action="{!Ephesoft}" value="Ephesoft"/>
 </apex:pageBlockButtons>
 <apex:pageMessages />
 
  <apex:pageBlockSection columns="1">
  
    <apex:pageBlockSectionItem >
      <apex:outputLabel value="File" for="file_File"/>
      <apex:inputFile id="file_File" value="{!fileBody}" filename="{!fileName}"/>
    </apex:pageBlockSectionItem>
        
    
     
    
    
    <apex:pageBlockSectionItem >
      <apex:outputLabel value="" for="uploadBtn"/> 
      <apex:commandButton id="uploadBtn" value="Attach File" action="{!processUpload}" />
    </apex:pageBlockSectionItem> 
    
    
   
     
 
  </apex:pageBlockSection>
 
 </apex:pageBlock>


 </apex:form>

 <apex:relatedList list="CombinedAttachments" /> 
</apex:page>