We usually have a requirement where we get data in the CSV file and then we need to read to data from CSV file. Below is a code snippet that allows you to read from an attached CSV file in Siebel.
For this example I have taken Account Attachment BC but you can take any BC you want. Here is how it works.
- You receive a CSV file.
- You attach it to an attachment BC (Accounts in example below)
- Read that file.
- Do whatever you want to do with data.
Here is the code.
function ReadCSVFile ()
{
var oBusObj = TheApplication().GetBusObject(”Account”);
var oAccAttBC = oBusObj.GetBusComp(”Account Attachment”);
var LVal;
var cntr;
cntr = 1;
oAccAttBC.ActivateField(”AccntFileName”);
oAccAttBC.ClearToQuery();
oAccAttBC.SetViewMode(3);
oAccAttBC.SetSearchSpec(”Id”,’1-4FBYH’); // Row Id of Attachment Record
oAccAttBC.ExecuteQuery();
if(oAccAttBC.FirstRecord())
{
var temppath = oAccAttBC.InvokeMethod(”GetFile”, “AccntFileName”);
// because the path is returned as “Success,Path” so remove success
var pos = temppath.indexOf(”,”);
var path = temppath.substring(pos+1);
//path is without the status
var oFile = Clib.fopen(path,”r”);
if( oFile == null )
{ TheApplication().RaiseErrorText(path); }
var string = (Clib.fgets(2000,oFile)); //usually the first line is hearder so ignore it
while(!Clib.feof(oFile))
{
string = (Clib.fgets(2000,oFile));
LVal = string.split(”,”);
//LVal is the now an array populated with data
//Now put your logic to do whatever you want to do with data
//for example
if(LVal[0] == “Test”)
{
//do something
}
cntr++; // just to count number of lines that have been processed
}
}
}


3 Comments at "Code Snippet – Reading CSV file"
try
{
var csvFileName = “RG_Import.csv”;
var fp = Clib.fopen(csvFileName, “r”);
boRG = TheApplication().GetBusObject(”Risk Group”);
bcRG = boRG.GetBusComp(”Risk Group”);
bcRG.ActivateField(”Name”);
bcRG.ActivateField(”RM Position Name”);//AE Position
bcRG.ActivateField(”CEM Position Name”);//CEM Position
var line = Clib.fgets(fp)
for(var line = Clib.fgets(fp); line != null; line = Clib.fgets(fp))
{
var cells = line.split(”,”);
var sRGName = cells[0];
var sAEPostn = cells[1];
var sCEMPostn = cells[2];
var sCompPostn = cells[3];
var sCollatPostn = cells[4];
if (sRGName != “RGName”)
{
try
{
if(sRGName != “”)
{
bcRG.NewRecord(NewAfter);
bcRG.SetFieldValue(”Name”,sRGName)
bcPickRM = bcRG.GetPicklistBusComp(”RM Position Name”);
bcPickCEM = bcRG.GetPicklistBusComp(”CEM Position Name”);
bcPickRM.ClearToQuery();
bcPickRM.SetSearchSpec(”Name”, sAEPostn);
bcPickRM.ExecuteQuery(ForwardBackward);
if (bcPickRM.FirstRecord())
{
bcPickRM.Pick();
}
bcPickCEM.ClearToQuery();
bcPickCEM.SetSearchSpec(”Id”, sCEMPostn);
bcPickCEM.ExecuteQuery(ForwardBackward);
if (bcPickCEM.FirstRecord())
{
bcPickCEM.Pick();
bcRG.WriteRecord();
}
}
}
catch (e)
{
TheApplication().RaiseErrorText(e + ” - ” + sRGName);
}
}
}
}
Thanx neel…i was looking for something like this only and landed on ur website:)
is it possible to read an excel file also???
looks good
Comment Now!