You need to sign in to do that
Don't have an account?
Apex to XML creation file
Hi Team,
I want to create a simple XMl file with few fields...
Below is my code for the same
global class cls_OppFilessent{
public static string OppFilesend(ID Optid)
{
Opportunity rew=new Opportunity();
rew =[SELECT id,Name,Booking_Date__c,Type,Trade_In__c,VW_Delivery_Date__c FROM Opportunity where id='0067F00000E9VxaQAF'];
Dom.Document doc = new Dom.Document();
XmlStreamWriter w = new XmlStreamWriter();
w.writeStartDocument(null, '1.0');
w.writeStartElement(null, 'Opportunities', null); //this will be ROOT of <Opportunities> in XML
w.writeStartElement(null, 'Opportunity', null);//Element <Opportunity>
w.writeStartElement(null, 'Id', null);//Element </Id>
w.writeCharacters(rew.Id);
w.writeEndElement();//Element </Id>
//Element <BookingDate>
w.writeStartElement(null, 'BookingDate', null);
//w.writeEndElement();
if(rew.Booking_Date__c!=null)
{
//w.writeStartElement(null, 'BookingDate', null);
w.writeCharacters(String.valueOf(rew.Booking_Date__c));
}
w.writeEndElement();
w.writeStartElement(null, 'Name', null);
if(rew.Name!=null)
{
w.writeCharacters(rew.Name);
}
w.writeEndElement();
//Element <TradeIN>
w.writeStartElement(null, 'TradeIN', null);
if(rew.Trade_In__c!=null)
{
w.writeCharacters(String.valueOf(rew.Trade_In__c));
}
w.writeEndElement();
w.writeStartElement(null, 'Type', null);
if(rew.Type!=null)
{
w.writeCharacters(rew.Type);
}
w.writeEndElement();
//Element <DeliveryDate>
w.writeStartElement(null, 'DeliveryDate', null);
if(rew.VW_Delivery_Date__c!=null)
{
w.writeCharacters(String.valueOf(rew.VW_Delivery_Date__c));
}
w.writeEndElement();
w.writeEndDocument();//this will be ROOT CLOSE of </Opportunities> in XML
string xmlOutput = w.getXmlString();
system.debug('XML is xmlOutput '+xmlOutput );
w.close();
doc.Load(xmlOutput);
string xmldata = doc.toXmlString();
system.debug('XML is '+xmldata);
/*
Blob csvBlob;
try{
csvBlob = Blob.valueOf(xmldata);
}catch(exception e){
csvBlob = Blob.valueOf('Some Text');
}
if(rew.Active__c==true){
return 'Reward feed already sent';
}else{
Attachment attachment = new Attachment();
attachment.Body = csvBlob ;
attachment.Name = 'Rewardfile.xml';
attachment.ParentId = rew.id;
insert attachment;
rew.Active__c=true;
Update rew;
return 'XML: '+xmldata;
}*/
return 'XML: '+xmldata; // Pranav - temporary return - remove this once BLOB code is tested
}
}
Test Class:
@isTest
public class test_OppFilessent{
public static testMethod void m1(){
/*Time_Based_Mgmnt__c t = new Time_Based_Mgmnt__c ();
t.Name='Q1';
t.Start_Date__c=Date.Valueof('2014-01-01');
t.End_Date__c=Date.Valueof('2014-03-01');
insert t;
Reward__c rwrd= new Reward__c();
// rwrd.Name='test';
rwrd.Rward_Amount__c=5000;
rwrd.Active__c =true;
rwrd.User__c = userinfo.getUserId();
rwrd.Time_Based_Mgmnt__c = t.id;
insert rwrd;
*/
//cls_RewardFeedSent.rewardfeedsend('0067F00000E9VxaQAF');
//rwrd.Active__c =false;
//update rwrd;
ID sfdcOptyID = '0067F00000E9VxaQAF';
cls_OppFilessent.OppFilesend(sfdcOptyID);
}
}
But i am getting error as
System.QueryException: List has no rows for assignment to SObject
Class.cls_OppFilessent.OppFilesend: line 5, column 1
Class.test_OppFilessent.m1: line 26, column 1
please help
I want to create a simple XMl file with few fields...
Below is my code for the same
global class cls_OppFilessent{
public static string OppFilesend(ID Optid)
{
Opportunity rew=new Opportunity();
rew =[SELECT id,Name,Booking_Date__c,Type,Trade_In__c,VW_Delivery_Date__c FROM Opportunity where id='0067F00000E9VxaQAF'];
Dom.Document doc = new Dom.Document();
XmlStreamWriter w = new XmlStreamWriter();
w.writeStartDocument(null, '1.0');
w.writeStartElement(null, 'Opportunities', null); //this will be ROOT of <Opportunities> in XML
w.writeStartElement(null, 'Opportunity', null);//Element <Opportunity>
w.writeStartElement(null, 'Id', null);//Element </Id>
w.writeCharacters(rew.Id);
w.writeEndElement();//Element </Id>
//Element <BookingDate>
w.writeStartElement(null, 'BookingDate', null);
//w.writeEndElement();
if(rew.Booking_Date__c!=null)
{
//w.writeStartElement(null, 'BookingDate', null);
w.writeCharacters(String.valueOf(rew.Booking_Date__c));
}
w.writeEndElement();
w.writeStartElement(null, 'Name', null);
if(rew.Name!=null)
{
w.writeCharacters(rew.Name);
}
w.writeEndElement();
//Element <TradeIN>
w.writeStartElement(null, 'TradeIN', null);
if(rew.Trade_In__c!=null)
{
w.writeCharacters(String.valueOf(rew.Trade_In__c));
}
w.writeEndElement();
w.writeStartElement(null, 'Type', null);
if(rew.Type!=null)
{
w.writeCharacters(rew.Type);
}
w.writeEndElement();
//Element <DeliveryDate>
w.writeStartElement(null, 'DeliveryDate', null);
if(rew.VW_Delivery_Date__c!=null)
{
w.writeCharacters(String.valueOf(rew.VW_Delivery_Date__c));
}
w.writeEndElement();
w.writeEndDocument();//this will be ROOT CLOSE of </Opportunities> in XML
string xmlOutput = w.getXmlString();
system.debug('XML is xmlOutput '+xmlOutput );
w.close();
doc.Load(xmlOutput);
string xmldata = doc.toXmlString();
system.debug('XML is '+xmldata);
/*
Blob csvBlob;
try{
csvBlob = Blob.valueOf(xmldata);
}catch(exception e){
csvBlob = Blob.valueOf('Some Text');
}
if(rew.Active__c==true){
return 'Reward feed already sent';
}else{
Attachment attachment = new Attachment();
attachment.Body = csvBlob ;
attachment.Name = 'Rewardfile.xml';
attachment.ParentId = rew.id;
insert attachment;
rew.Active__c=true;
Update rew;
return 'XML: '+xmldata;
}*/
return 'XML: '+xmldata; // Pranav - temporary return - remove this once BLOB code is tested
}
}
Test Class:
@isTest
public class test_OppFilessent{
public static testMethod void m1(){
/*Time_Based_Mgmnt__c t = new Time_Based_Mgmnt__c ();
t.Name='Q1';
t.Start_Date__c=Date.Valueof('2014-01-01');
t.End_Date__c=Date.Valueof('2014-03-01');
insert t;
Reward__c rwrd= new Reward__c();
// rwrd.Name='test';
rwrd.Rward_Amount__c=5000;
rwrd.Active__c =true;
rwrd.User__c = userinfo.getUserId();
rwrd.Time_Based_Mgmnt__c = t.id;
insert rwrd;
*/
//cls_RewardFeedSent.rewardfeedsend('0067F00000E9VxaQAF');
//rwrd.Active__c =false;
//update rwrd;
ID sfdcOptyID = '0067F00000E9VxaQAF';
cls_OppFilessent.OppFilesend(sfdcOptyID);
}
}
But i am getting error as
System.QueryException: List has no rows for assignment to SObject
Class.cls_OppFilessent.OppFilesend: line 5, column 1
Class.test_OppFilessent.m1: line 26, column 1
please help
There is no test opportunity data created that's why the soql query is not returning any records and you are getting this error. Why don't you just create an opportunity record in the testMethod and insert it and then pass it's ID in the method . Also in the class do not use hardcoded Id ever.
Thanks
All Answers
There is no test opportunity data created that's why the soql query is not returning any records and you are getting this error. Why don't you just create an opportunity record in the testMethod and insert it and then pass it's ID in the method . Also in the class do not use hardcoded Id ever.
Thanks
Test Class
Thanks both.. your suggestions helped me... now while debugging i am getting data in XMl format... now i want to send the XML file using apex code .. how can i generate XML files in apex class like using blob function or something else.