You need to sign in to do that
Don't have an account?
jhansisridhar_2011
Help to increase code coverage !!!
Hi all,
Please help me to increase code coverage in test class, I have written a apexclass,
public void storeProductsTxt(){
System.debug('********** Store Products in XML **********');
XmlStreamWriter w = new XmlStreamWriter();
w.writeStartDocument(null, '1.0');
w.writeStartElement(null, 'Products', null);
String output = '';
for (Product2 p : [Select p.Description, p.Family, p.Id, p.IsActive, p.Name, p.PS_Product_SKU__c, p.Strategic_Value__c, p.Unit_of_Measure__c,
p.Maximum_Order_Qty__c, p.Multiple_Order_Qty__c,
(Select Name, Quantity_in_Stock__c, Ship_From__c From Inventory__r)
from Product2 p where isActive = true limit 10]){
output = '<Product><Id>' + p.Id + '</Id><Name>' + p.Name + '</Name><Description>' + p.Description + '</Description><PSProductSKU>' + p.PS_Product_SKU__c + '</PS Product SKU><UOM>' + p.Unit_of_Measure__c + '</UOM><MAX>' + String.valueOf(p.Maximum_Order_Qty__c) + '</MAX><Multiple>' + String.valueOf(p.Multiple_Order_Qty__c) + '</Multiple><Inventories>';
for (Inventory__c inv : p.Inventory__r){
output += '<Inventory><WH>' + inv.Ship_From__c + '</WH><Stock>' + String.valueOf(inv.Quantity_In_Stock__c) + '</Stock></Inventory>';
}
output += '</Inventories></Product>';
w.writeCData(output);
//System.debug('p: ' + p);
//output = p + '\n';
//b = Blob.valueOf(output);
}
w.writeEndElement();
w.writeEndDocument();
String xmlOutput = w.getXmlString();
System.debug('*** XML: ' + xmlOutput);
w.close();
//System.debug('--> output: ' + output);
//Blob b = Blob.valueof(xmlOutput);
Blob b = Blob.valueof('text');
List<Attachment> list_attachments = [Select Body from Attachment where Name = 'ProductInfo.xml' and ParentId = '001T000000F7raXIAR' limit 1000];
if (list_attachments.size() > 0){
delete list_attachments;
}
Attachment a = new Attachment();
a.Name = 'ProductInfo.xml';
a.Body = b;
a.ParentId = '001T000000F7raXIAR';
a.ContentType = 'text/xml';
a.IsPrivate = false;
insert a;
}
getting error message as invalid cross reference id .
Thanks in advance.
Hi,
Generally you get cross reference id error; if you are setting up a wrong id in lookup field.
eg. If you place ContactID instead of AccountId on a field.
Also; I can see that you have used the hard-coded ID's in your test method. Its good practice to create your own records in test method and use them as the ids in 2 different instances may vary.
Hi Kulkarni,
Thanks for reply, I realized the problem nd made necessary updates, it's working fine now.
I have written class where am using xmlstreamwriter , am not able to write a test class to cover 75% for below code
please help to write test class.
Thanks in Advance.