2014年4月24日 星期四

Domino Notes 7.0 呼叫SAP RFC來取得資產說明

上一篇做了一個簡單的RFC, 這一篇是紀錄如何呼叫SAP RFC來取得資產說明

1.首先要做一個Java agent, 接受doc.NoteID,並將結果用
session.setEnvironmentVar("AgentResult",resultMsg);來傳回Lotusscript

範例:
import lotus.domino.*;
import java.util.*;
import com.sap.mw.jco.*;
public class SAPApprove extends AgentBase {
private Session session;
private AgentContext agentContext;
private Database db;
private String sapIP,sapClient,sapAccount,sapPasswd,sapLang,sapSysNo;
private JCO.Function function;
private JCO.Client client ;
private Log log;
private boolean isError=false;
private String errMsg="";
private String resultCode;
private String resultMsg ;
private Document doc;

public void init(Session session,AgentContext agentContext) throws NotesException{
try {
this.session=session;
this.agentContext=agentContext;
this.db=agentContext.getCurrentDatabase();
Document profile=db.getProfileDocument("SystemProfile",null);
sapIP=profile.getItemValueString("SAPIP");
sapClient=profile.getItemValueString("SAPClient");
sapAccount=profile.getItemValueString("SAPAccount");
sapPasswd=profile.getItemValueString("SAPPasswd");
sapLang=profile.getItemValueString("SAPCode");
sapSysNo=profile.getItemValueString("SAPSysNO");
String logPath=profile.getItemValueString("LogPath");
log = session.createLog( db.getTitle()+" 's Agent "+agentContext.getCurrentAgent().getName()+" on " + db.getServer());
log.openNotesLog(db.getServer(),logPath);

Agent agent = agentContext.getCurrentAgent();
doc = db.getDocumentByID(agent.getParameterDocID());

System.out.println("Load Profile OK.");
}catch(Exception e) {
e.printStackTrace();
try{
session.setEnvironmentVar("AgentResult","載入SAP 設定時,發生錯誤 !!");
}catch ( Exception ex){}
}
}


public void processApprove() {
boolean isok;
Item notifyitem;
try{
System.out.println("Begin JCO Connection....");
log.logAction("Begin JCO Connection....");
client = JCO.createClient(sapClient, sapAccount, sapPasswd, sapLang, sapIP, sapSysNo);
client.connect();
JCO.Repository repository = new JCO.Repository( "Notes", client );
JCO.Attributes attr = client.getAttributes();
IFunctionTemplate ftemplate = repository.getFunctionTemplate( "Z_RFC_ASSETCHECK" );
function = new JCO.Function( ftemplate );
System.out.println("Begin Load Not Procsess Documents....");
log.logAction("Begin Load Not Procsess Documents....");
if (doc != null) {
String sn=doc.getItemValueString("ANLN1").replaceAll("\\s+", "");
String sn2=doc.getItemValueString("ANLN2").replaceAll("\\s+", "");
System.out.println("Begin Process Asset No: " + sn + "-" + sn2);
log.logAction("Begin Process Asset No: " + sn + "-" + sn2);
function.getImportParameterList().setValue(sn,"IANLN1");
function.getImportParameterList().setValue(sn2,"IANLN2");
client.execute( function );
resultCode = (String)function.getExportParameterList().getValue("STATUS");
resultMsg = (String)function.getExportParameterList().getValue("MESSAGE");
if (! (resultCode.equals("") | resultCode.equals("O")) ){
System.out.println("Return Code :"+resultCode);
System.out.println("Return Message :"+resultMsg);
log.logError(1,"Error Code : "+resultCode);
log.logError(1,"Error Message : "+sn+ "-" + sn2+" - "+resultMsg);
isError=true;
errMsg="Notes 與 SAP 溝通作業時發生錯誤";
session.setEnvironmentVar("AgentResult",resultMsg);
}else{
log.logAction("Z_RFC_ASSETCHECK SN: " + sn + "-" + sn2);
isError=false;
session.setEnvironmentVar("AgentResult",resultMsg);
}
}
}catch(java.lang.NoClassDefFoundError e){
e.printStackTrace();
try{
session.setEnvironmentVar("AgentResult","SAP 元件未安裝!!");
}catch ( Exception ex){}
}
catch(Exception e) {
e.printStackTrace();
try{
log.logError(0,e.getLocalizedMessage());
session.setEnvironmentVar("AgentResult","代理程式發生不明錯誤!!");
}catch(Exception err){}
}finally{
try{
  JCO.releaseClient(client);
log.close();
}catch(Exception err){}
}
}

public void NotesMain() {

try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
SAPApprove sapobj=new SAPApprove();
sapobj.init(session,agentContext);

Agent agent = agentContext.getCurrentAgent();

sapobj.processApprove();

} catch(Exception e) {
e.printStackTrace();
}
}

2.Lotusscript呼叫範例:

Sub Click(Source As Button)
If uidoc.EditMode Then
uidoc.Save
Dim agent As NotesAgent
Dim m_agnrtn As String
Set agent=db.GetAgent("SAPAssetCheck")
If (agent.Run(doc.NoteID)><0) Then
Messagebox "執行 SAP 溝通之代理程式,執行錯誤!!",MB_ICONSTOP,"執行錯誤!!"
Exit Sub
End If
m_agnrtn=session.GetEnvironmentString("AgentResult")
If m_agnrtn="SAP 元件未安裝!!" Then
Messagebox "您尚未安裝與SAP溝通之必要元件,若要使用此功能,請連絡相關人員,謝謝!!",MB_ICONSTOP,"元件未安裝"
Exit Sub
End If
Call uidoc.FieldSetText("ANLATXT50", m_agnrtn )
End If
End Sub

沒有留言:

張貼留言