+ Start a Discussion
Andrew Aldis 15Andrew Aldis 15 

XmlStreamWriter does not seem to work with lighting.

Hello,

I am trying to use the xmlstreamwriter class in a lighting component and I keep getting an error "Non static method cannot be referenced from a static context", and i cannot figure out how to fix it.  My code is below, any help is appreciated.


@auraEnabled
public static String getOrderURL(string recordId) {
system.debug('get Order URL called Record Id is '+recordId);

system.debug('record Id is not Null');
Contact C;
C = [SELECT CreatedById, Id, FirstName, LastName, Account.ID, Account.Name,
MailingStreet, MailingCity, MailingStateCode, MailingPostalCode, MailingCountry,
Phone, Email
FROM Contact WHERE Id = :recordId];
system.debug('contact is '+C);
FGS_SSO__c sso = FGS_SSO__c.getInstance(); // Custom Settings data for URL, Company ID, etc.
system.debug('sso is '+sso);
XmlStreamWriter w = new XmlStreamWriter();
w.writeStartDocument('utf-8', '1.0');
w.writeStartElement(null, 'NewDataSet', null);
w.writeStartElement(null, 'Customer', null); // Customer
w.WriteElement(w, 'CustomerID', sso.CustomerID__c);
w.WriteElement(w, 'FirstName', UserInfo.getFirstName());
w.WriteElement(w, 'LastName', UserInfo.getLastName());
w.WriteElement(w, 'UserEmail', UserInfo.getUserEmail());
w.WriteElement(w, 'UserName', UserInfo.getFirstName() + ' ' + UserInfo.getLastName());
w.WriteElement(w, 'UserID', UserInfo.getUserName());
w.writeEndElement(); //end Customer
w.writeStartElement(null, 'Recipients', null); // Recipients
w.writeStartElement(null, 'Recipient', null); // Recipient
w.WriteElement(w, 'ShipFirstName', C.FirstName);
w.WriteElement(w, 'ShipLastName', C.LastName);
w.WriteElement(w, 'ShipCompany', C.Account.Name);
w.WriteElement(w, 'ShipAddress', C.MailingStreet);
w.WriteEmptyElement(null, 'ShipAddress2', null); // Required
w.WriteEmptyElement(null, 'ShipAddress3', null); // Required
w.WriteElement(w, 'ShipCity', C.MailingCity);
w.WriteElement(w, 'ShipStateProvince', C.MailingStateCode);
w.WriteElement(w, 'ShipZipPostal', C.MailingPostalCode);
w.WriteElement(w, 'ShipCountry', C.MailingCountry);
w.WriteElement(w, 'ShipPhone', C.Phone);
WriteElement(w, 'ShipEmail', C.Email);
w.WriteElement(w, 'CustomerID', C.Id); // Must be non-null
w.WriteElement(w, 'CreatedBy', UserInfo.getUserId()); // this is wrong - should be the UserInfo.getUserId()
w.WriteElement(w, 'AccountID', C.Account.Id);
w.WriteElement(w, 'ContactID', C.Id);
w.WriteEmptyElement(null, 'CampaignID', null); // Required
w.writeEndElement();
w.writeEndElement();
w.writeEndDocument();
String xmlOutput = 'inputXML=' + EncodingUtil.urlEncode(w.getXmlString(), 'UTF-8');
system.debug('xmlOutput is '+xmlOutput);
XmlStreamWriter.close();
// III. Post XML to FGS Web Service and return redirect URL.
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(sso.URL__c);
req.setMethod('POST');
req.setHeader('Content-Length', String.valueOf(xmlOutput.length()));
req.setBody(xmlOutput);
HttpResponse res;
if (!isApexTest){
// Make a real callout since we are not running a test
res = h.send(req);
Dom.Document doc = res.getBodyDocument();
return doc.getRootElement().getText();
} else {
// A test is running
return '';
}
}

 
Alain CabonAlain Cabon
Hi,

WriteElement doesn't exist but WriteAttribute does.

w.WriteElement(w, 'CustomerID', sso.CustomerID__c);

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_xml_XmlStream_writer.htm

w.writeAttribute( null, null,  'CustomerID', sso.CustomerID__c);

writeAttribute(prefix, namespaceUri, localName, value) : Writes an attribute to the output stream.
public Void writeAttribute(String prefix, String namespaceUri, String localName, String value)