2012-09-10 88 views
4

我已經更新了我的問題。 我真正想知道的是: 如何將csv數據轉換爲netsuite? 好吧,我似乎使用csv導入工具來創建映射並使用此調用來導入csv nlapiSubmitCSVImport(nlobjCSVImport)。如何將csv數據轉換爲netsuite?

現在我的問題是:如何迭代對象?! 這讓我半途而廢 - 我得到的CSV數據,但我似乎無法找出我如何遍歷它來操縱日期。當然,這是一個預定腳本的全部要點。

這真的讓我發瘋。

@Robert H 我可以想象爲什麼你想從CSV導入數據的一百萬個理由。帳單,例如。關於任何公司保存的數據的各種報告,我不想將它保存在文件櫃中,也不是我真的想保留文件。我只想要數據。我想操縱它,我想輸入它。

+0

歡迎SO! Javascript適合哪些流程?您是否希望Netsuite定期生成數據,然後在客戶端使用Javascript來獲取並查看報表中的數據? –

+0

@JaredFarrish Netsuite爲用戶編寫的腳本提供服務器端javascript。 – Craig

+0

爲什麼要通過代碼進行CSV導入,並且需要遍歷CSV文件本身?將CSV文件編入代碼可能更好,然後從那裏操作數據(假設您的CSV位於文件櫃中) –

回答

0

同伴NetSuite用戶在這裏,我一直在使用SuiteScripts一段時間,但從來沒有看到nlobjCSVImport對象,也沒有看到nlapiSubmitCSVImport ..我在文檔中看到,它顯示,但沒有描述細節的頁面,關心分享哪裏你從那裏得到了文檔?

隨着CSVImport對象的文檔,我可能會提供一些更多的幫助。

P.S.我嘗試將此消息發佈爲評論,但「添加評論」鏈接因某些原因未顯示。還是新的SOF

3

解決步驟:

  1. 上傳CSV文件,我們必須使用Suitelet腳本。

    (注:file - 此字段類型僅適用於Suitelets並出現Suitelet頁面的主選項卡上的文件上傳控件設置字段類型的文件添加到頁面。)

    var fileField = form.addField('custpage_file', 'file', 'Select CSV File'); 
    var id = nlapiSubmitFile(file); 
    
  2. 我們準備調用Restlet腳本並將文件ID傳遞給它。

    var recordObj = new Object(); 
    recordObj.fileId = fileId; 
    
    // Format input for Restlets for the JSON content type 
    var recordText = JSON.stringify(recordObj);//stringifying JSON 
    
    // Setting up the URL of the Restlet 
    var url = 'https://rest.na1.netsuite.com/app/site/hosting/restlet.nl?script=108&deploy=1'; 
    
    // Setting up the headers for passing the credentials 
    var headers = new Array(); 
    
    headers['Content-Type'] = 'application/json';  
    headers['Authorization'] = 'NLAuth [email protected], nlauth_signature=*password*, nlauth_account=TSTDRV****, nlauth_role=3'; 
    

    (注:nlapiCreateCSVImport:此API僅支持軟件包安裝腳本,調度腳本和RESTlets)

  3. 我們稱之爲的Restlet使用nlapiRequestURL

    // Calling Restlet 
    var output = nlapiRequestURL(url, recordText, headers, null, "POST"); 
    
  4. 創建使用在設置>導入/導出>導入CSV記錄中可用的導入CSV記錄進行映射。

  5. Restlet腳本內部從Restlet參數中獲取文件ID。使用nlapiCreateCSVImport() API並使用步驟3中創建的映射id設置其映射。使用setPrimaryFile()函數設置CSV文件。

    var primaryFile = nlapiLoadFile(datain.fileId); 
    var job = nlapiCreateCSVImport(); 
    job.setMapping(mappingFileId); // Set the mapping 
    
    // Set File 
    job.setPrimaryFile(primaryFile.getValue()); // Fetches the content of the file and sets it. 
    
  6. 使用nlapiSubmitCSVImport()提交。

    nlapiSubmitCSVImport(job); // We are done 
    

還有另一種方法,我們可以解決這個問題,雖然都不理想我也不會建議。(因爲它消耗了大量的API,如果你有大量的在您的CSV文件中的記錄。)

比方說,我們不希望使用nlapiCreateCSVImport API,讓我們從步驟繼續4.

  1. 只要像我們以前那樣獲取文件Id,加載文件並獲取其內容即可。

    var fileContent = primaryFile.getValue(); 
    
  2. 拆分文件的行,然後拆分單詞並將值存儲到單獨的數組中。

    var splitLine = fileContent.split("\n"); // Splitting the file on the basis of lines. 
    
    for (var lines = 1,count=0; lines < splitLine.length; lines++) 
    { 
        var words = (splitLine[lines]).split(","); // words stores all the words on a line 
    
        for (var word = 0; word < words.length; word++) 
        { 
         nlapiLogExecution("DEBUG", "Words:",words[word]); 
        } 
    } 
    

    注意:確保您的CSV文件中沒有額外的空白行。

  3. 最後從上面創建的數組中創建記錄並設置字段值。

    var myRec = nlapiCreateRecord('cashsale'); // Here you create the record of your choice 
    
    myRec.setFieldValue('entity', arrCustomerId[i]); // For example, arrCustomerId is an array of customer ID. 
    
    var submitRec = nlapiSubmitRecord(myRec); // and we are done