WSDL Apex class
===========================================================================
//Generated by wsdl2apex
public class sapComDocumentSapSoapFunctionsMcS {
public class ZstApiSoCreate {
public String DocType;
public String CompCdeB;
public String SaleOrg;
public String DistChanl;
public String Division;
public String ReqDateH;
public String PurchDate;
public String PurchNoC;
public String ChildOrg;
public String PurchNoS;
public String PoDatS;
public String Material;
public String ParentOrg;
public String ChildOrd;
public String StoreLocation;
public String Plant;
public String Quantity;
public String PayTerms;
public String IncoTerm1;
public String IncoTerm2;
public String OrderReason;
public String Name1;
public String Name2;
public String House;
public String Stereet;
public String City;
public String PostalCode;
public String Mobile;
public String TeletexNo;
public String Salqtynum;
public String Salqtyden;
public String TrgQtyNo;
public String TrgqtyDen;
public String AgSearchterm;
public String WeSearchterm;
public String PoQuan;
public String EanUpc;
public String ReqQty;
private String[] DocType_type_info = new String[]{'DocType','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] CompCdeB_type_info = new String[]{'CompCdeB','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] SaleOrg_type_info = new String[]{'SaleOrg','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] DistChanl_type_info = new String[]{'DistChanl','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] Division_type_info = new String[]{'Division','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] ReqDateH_type_info = new String[]{'ReqDateH','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] PurchDate_type_info = new String[]{'PurchDate','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] PurchNoC_type_info = new String[]{'PurchNoC','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] ChildOrg_type_info = new String[]{'ChildOrg','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] PurchNoS_type_info = new String[]{'PurchNoS','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] PoDatS_type_info = new String[]{'PoDatS','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] Material_type_info = new String[]{'Material','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] ParentOrg_type_info = new String[]{'ParentOrg','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] ChildOrd_type_info = new String[]{'ChildOrd','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] StoreLocation_type_info = new String[]{'StoreLocation','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] Plant_type_info = new String[]{'Plant','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] Quantity_type_info = new String[]{'Quantity','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] PayTerms_type_info = new String[]{'PayTerms','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] IncoTerm1_type_info = new String[]{'IncoTerm1','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] IncoTerm2_type_info = new String[]{'IncoTerm2','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] OrderReason_type_info = new String[]{'OrderReason','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] Name1_type_info = new String[]{'Name1','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] Name2_type_info = new String[]{'Name2','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] House_type_info = new String[]{'House','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] Stereet_type_info = new String[]{'Stereet','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] City_type_info = new String[]{'City','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] PostalCode_type_info = new String[]{'PostalCode','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] Mobile_type_info = new String[]{'Mobile','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] TeletexNo_type_info = new String[]{'TeletexNo','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] Salqtynum_type_info = new String[]{'Salqtynum','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] Salqtyden_type_info = new String[]{'Salqtyden','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] TrgQtyNo_type_info = new String[]{'TrgQtyNo','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] TrgqtyDen_type_info = new String[]{'TrgqtyDen','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] AgSearchterm_type_info = new String[]{'AgSearchterm','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] WeSearchterm_type_info = new String[]{'WeSearchterm','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] PoQuan_type_info = new String[]{'PoQuan','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] EanUpc_type_info = new String[]{'EanUpc','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] ReqQty_type_info = new String[]{'ReqQty','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] apex_schema_type_info = new String[]{'urn:sap-com:document:sap:soap:functions:mc-style','false','true'};
private String[] field_order_type_info = new String[]{'DocType','CompCdeB','SaleOrg','DistChanl','Division','ReqDateH','PurchDate','PurchNoC','ChildOrg','PurchNoS','PoDatS','Material','ParentOrg','ChildOrd','StoreLocation','Plant','Quantity','PayTerms','IncoTerm1','IncoTerm2','OrderReason','Name1','Name2','House','Stereet','City','PostalCode','Mobile','TeletexNo','Salqtynum','Salqtyden','TrgQtyNo','TrgqtyDen','AgSearchterm','WeSearchterm','PoQuan','EanUpc','ReqQty'};
}
public class ZttApiSoCreate {
public sapComDocumentSapSoapFunctionsMcS.ZstApiSoCreate[] item;
private String[] item_type_info = new String[]{'item','urn:sap-com:document:sap:soap:functions:mc-style',null,'0','-1','false'};
private String[] apex_schema_type_info = new String[]{'urn:sap-com:document:sap:soap:functions:mc-style','false','true'};
private String[] field_order_type_info = new String[]{'item'};
}
public class ZstApiSoCondition {
public String ItemNo;
public String CondType;
public String Rate;
public String Currency_x;
public String ConditionValue;
public String CondValue;
private String[] ItemNo_type_info = new String[]{'ItemNo','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] CondType_type_info = new String[]{'CondType','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] Rate_type_info = new String[]{'Rate','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] Currency_x_type_info = new String[]{'Currency','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] ConditionValue_type_info = new String[]{'ConditionValue','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] CondValue_type_info = new String[]{'CondValue','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] apex_schema_type_info = new String[]{'urn:sap-com:document:sap:soap:functions:mc-style','false','true'};
private String[] field_order_type_info = new String[]{'ItemNo','CondType','Rate','Currency_x','ConditionValue','CondValue'};
}
public class ZttApiLog {
public sapComDocumentSapSoapFunctionsMcS.ZstApiLog[] item;
private String[] item_type_info = new String[]{'item','urn:sap-com:document:sap:soap:functions:mc-style',null,'0','-1','false'};
private String[] apex_schema_type_info = new String[]{'urn:sap-com:document:sap:soap:functions:mc-style','false','true'};
private String[] field_order_type_info = new String[]{'item'};
}
public class ZstApiSoPartner {
public String PartnerType;
public String PartnerCode;
private String[] PartnerType_type_info = new String[]{'PartnerType','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] PartnerCode_type_info = new String[]{'PartnerCode','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] apex_schema_type_info = new String[]{'urn:sap-com:document:sap:soap:functions:mc-style','false','true'};
private String[] field_order_type_info = new String[]{'PartnerType','PartnerCode'};
}
public class ZttApiSoCondition {
public sapComDocumentSapSoapFunctionsMcS.ZstApiSoCondition[] item;
private String[] item_type_info = new String[]{'item','urn:sap-com:document:sap:soap:functions:mc-style',null,'0','-1','false'};
private String[] apex_schema_type_info = new String[]{'urn:sap-com:document:sap:soap:functions:mc-style','false','true'};
private String[] field_order_type_info = new String[]{'item'};
}
public class ZsdApiSoCreate_element {
public sapComDocumentSapSoapFunctionsMcS.ZttApiSoPartner GiPartner;
public sapComDocumentSapSoapFunctionsMcS.ZttApiSoCondition GiSocond;
public sapComDocumentSapSoapFunctionsMcS.ZttApiSoCreate GiSodata;
public sapComDocumentSapSoapFunctionsMcS.ZttApiLog ItRet1;
private String[] GiPartner_type_info = new String[]{'GiPartner','urn:sap-com:document:sap:soap:functions:mc-style',null,'0','1','false'};
private String[] GiSocond_type_info = new String[]{'GiSocond','urn:sap-com:document:sap:soap:functions:mc-style',null,'0','1','false'};
private String[] GiSodata_type_info = new String[]{'GiSodata','urn:sap-com:document:sap:soap:functions:mc-style',null,'0','1','false'};
private String[] ItRet1_type_info = new String[]{'ItRet1','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] apex_schema_type_info = new String[]{'urn:sap-com:document:sap:soap:functions:mc-style','false','true'};
private String[] field_order_type_info = new String[]{'GiPartner','GiSocond','GiSodata','ItRet1'};
}
public class ZstApiLog {
public String Type_x;
public String Log;
private String[] Type_x_type_info = new String[]{'Type','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] Log_type_info = new String[]{'Log','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] apex_schema_type_info = new String[]{'urn:sap-com:document:sap:soap:functions:mc-style','false','true'};
private String[] field_order_type_info = new String[]{'Type_x','Log'};
}
public class ZsdApiSoCreateResponse_element {
public sapComDocumentSapSoapFunctionsMcS.ZttApiLog ItRet1;
public String Saledoc;
private String[] ItRet1_type_info = new String[]{'ItRet1','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] Saledoc_type_info = new String[]{'Saledoc','urn:sap-com:document:sap:soap:functions:mc-style',null,'1','1','false'};
private String[] apex_schema_type_info = new String[]{'urn:sap-com:document:sap:soap:functions:mc-style','false','true'};
private String[] field_order_type_info = new String[]{'ItRet1','Saledoc'};
}
public class ZttApiSoPartner {
public sapComDocumentSapSoapFunctionsMcS.ZstApiSoPartner[] item;
private String[] item_type_info = new String[]{'item','urn:sap-com:document:sap:soap:functions:mc-style',null,'0','-1','false'};
private String[] apex_schema_type_info = new String[]{'urn:sap-com:document:sap:soap:functions:mc-style','false','true'};
private String[] field_order_type_info = new String[]{'item'};
}
public class ZSD_API_SO_CREATE {
public String endpoint_x = 'https://abcdxxxx.cvbb.com:4444/sap/bc/srt/rfc/sap/zsd_api_so_create/800/zsd_api_so_create/zsd_api_so_create';
public Map<String,String> inputHttpHeaders_x;
public Map<String,String> outputHttpHeaders_x;
public String clientCertName_x;
public String clientCert_x;
public String clientCertPasswd_x;
public Integer timeout_x;
private String[] ns_map_type_info = new String[]{'urn:sap-com:document:sap:soap:functions:mc-style', 'sapComDocumentSapSoapFunctionsMcS', 'urn:sap-com:document:sap:rfc:functions', 'sapComDocumentSapRfcFunctions'};
public sapComDocumentSapSoapFunctionsMcS.ZsdApiSoCreateResponse_element ZsdApiSoCreate(sapComDocumentSapSoapFunctionsMcS.ZttApiSoPartner GiPartner,sapComDocumentSapSoapFunctionsMcS.ZttApiSoCondition GiSocond,sapComDocumentSapSoapFunctionsMcS.ZttApiSoCreate GiSodata,sapComDocumentSapSoapFunctionsMcS.ZttApiLog ItRet1) {
sapComDocumentSapSoapFunctionsMcS.ZsdApiSoCreate_element request_x = new sapComDocumentSapSoapFunctionsMcS.ZsdApiSoCreate_element();
request_x.GiPartner = GiPartner;
request_x.GiSocond = GiSocond;
request_x.GiSodata = GiSodata;
request_x.ItRet1 = ItRet1;
sapComDocumentSapSoapFunctionsMcS.ZsdApiSoCreateResponse_element response_x;
Map<String, sapComDocumentSapSoapFunctionsMcS.ZsdApiSoCreateResponse_element> response_map_x = new Map<String, sapComDocumentSapSoapFunctionsMcS.ZsdApiSoCreateResponse_element>();
response_map_x.put('response_x', response_x);
WebServiceCallout.invoke(
this,
request_x,
response_map_x,
new String[]{endpoint_x,
'urn:sap-com:document:sap:soap:functions:mc-style:ZSD_API_SO_CREATE:ZsdApiSoCreateRequest',
'urn:sap-com:document:sap:soap:functions:mc-style',
'ZsdApiSoCreate',
'urn:sap-com:document:sap:soap:functions:mc-style',
'ZsdApiSoCreateResponse',
'sapComDocumentSapSoapFunctionsMcS.ZsdApiSoCreateResponse_element'}
);
response_x = response_map_x.get('response_x');
return response_x;
}
}
}
==============================================================
Apex class:
/* Created by : ANKIT VERMA
* Created date: 11-03-2021
* Description: Create SO in Sap and get SO_Number.
*/
global class SO_Creation_On_SAP {
public list<order> OrderUpdateList;
@AuraEnabled
@future(callout=true)
webservice static void CallToSAP(Id orderId){
String ErrorMsg;
System.debug('Webservice Invoke');
System.debug('orderId == '+orderId);
if(orderId != null){
List<Order> orderLst = [SELECT id,Ship_to_Address_SAP_Code_Formula__c,Account_SAP_Code_Formula__c,CreatedDate, Sap_Sales_Order__c,Ship_To_Address__c, Account.GST__c,account.PAN_No__c, Account.id, EffectiveDate,
PO_Number_Ship_To__c, PO_Date_Ship_To_Party__c, Sales_organization__c, Distribution_Channel__c, Division__c,
Plant__c, Ship_to_Address__r.Street__c, Ship_to_Address__r.Street_2__c, Ship_to_Address__r.City__c,
Ship_to_Address__r.Zip_Postal_Code__c, Ship_to_Address__r.id, Ship_to_Address__r.Name,OwnerID,
(SELECT id, Price__c, Product_Code__c, Quantity__c, Distribution_Channel__c, Division__c, Product_Name__c,Plant__c FROM Order_Line_Items__r)
FROM Order
WHERE id =: orderId];
System.debug('orderLst == >> '+orderLst);
if(orderLst.size() > 0){
Order odr = orderLst[0];
List<Error_SAP__c> errorLog= new List<Error_SAP__c>();
if(odr.Sap_Sales_Order__c == '' || odr.Sap_Sales_Order__c == null){
String username = 'user';
String password = 'pass@321';
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'Basic '+ EncodingUtil.base64Encode(headerValue);
sapComDocumentSapSoapFunctionsMcS.ZSD_API_SO_CREATE am= new sapComDocumentSapSoapFunctionsMcS.ZSD_API_SO_CREATE ();
am.inputHttpHeaders_x = new Map<String,String>();
am.inputHttpHeaders_x.put('Authorization',authorizationHeader);
am.inputHttpHeaders_x.put('Content-Type', 'text/xml;charset=UTF-8;');
am.timeout_x = 100000;
List<sapComDocumentSapSoapFunctionsMcS.ZstApiSoCreate> SODetailList = new List<sapComDocumentSapSoapFunctionsMcS.ZstApiSoCreate>();
List<sapComDocumentSapSoapFunctionsMcS.ZstApiSoPartner> partnerDetailList = new List<sapComDocumentSapSoapFunctionsMcS.ZstApiSoPartner>();
List<sapComDocumentSapSoapFunctionsMcS.ZstApiSoCondition> conDetailList= new List<sapComDocumentSapSoapFunctionsMcS.ZstApiSoCondition>();
List<sapComDocumentSapSoapFunctionsMcS.ZstApiLog> apiLogDeailList= new List<sapComDocumentSapSoapFunctionsMcS.ZstApiLog>();
sapComDocumentSapSoapFunctionsMcS.ZsdApiSoCreateResponse_element resp = new sapComDocumentSapSoapFunctionsMcS.ZsdApiSoCreateResponse_element();
sapComDocumentSapSoapFunctionsMcS.ZttApiSoCondition conData = new sapComDocumentSapSoapFunctionsMcS.ZttApiSoCondition();
system.debug('orderLineItem size: '+odr.Order_Line_Items__r.size());
if(odr.Order_Line_Items__r.size() > 0){
integer i=000010;
for(Order_Line_Item__c line : odr.Order_Line_Items__r){
sapComDocumentSapSoapFunctionsMcS.ZstApiSoCreate SODetail = new sapComDocumentSapSoapFunctionsMcS.ZstApiSoCreate();
SODetail.DocType = 'ZSDF';
SODetail.CompCdeB= '1000';
SODetail.SaleOrg= '1000';
SODetail.DistChanl= line.Distribution_Channel__c;
If(line.Division__c!=null)
{
SODetail.Division= line.Division__c;
}
String dateFormatString = 'yyyyMMdd';
Date d = orderLst[0].EffectiveDate;
Datetime dt = Datetime.newInstance(d.year(), d.month(),d.day());
String deliverydate = dt.format(dateFormatString);
Datetime dCreatedDate = orderLst[0].CreatedDate;
Datetime dtCreatedDate = Datetime.newInstance(dCreatedDate.year(), dCreatedDate.month(),dCreatedDate.day());
String OrdCreatedDate = dtCreatedDate.format(dateFormatString);
System.debug('deliverydate'+deliverydate);
System.debug('OrdCreatedDate'+OrdCreatedDate);
SODetail.ReqDateH = deliverydate;
SODetail.PurchDate = OrdCreatedDate;
SODetail.PurchNoC= String.valueOf(odr.id).toUpperCase();
SODetail.PurchNoS= String.valueOf(odr.PO_Number_Ship_To__c).toUpperCase();
Datetime dPO_Date_Ship_To_Party = orderLst[0].PO_Date_Ship_To_Party__c;
Datetime dtPO_Date_Ship_To_Party = Datetime.newInstance(dPO_Date_Ship_To_Party.year(), dPO_Date_Ship_To_Party.month(),dPO_Date_Ship_To_Party.day());
String dttPO_Date_Ship_To_Party = dtPO_Date_Ship_To_Party.format(dateFormatString);
SODetail.PoDatS = dttPO_Date_Ship_To_Party;
SODetail.Material=line.Product_Name__c;
SODetail.AgSearchterm= String.valueof(odr.AccountId).toUpperCase();
SODetail.WeSearchterm = String.valueof(odr.Ship_To_Address__c).toUpperCase();
SODetail.EanUpc= String.valueOf(line.id).toUpperCase();
SODetail.ReqQty=String.valueOf(line.Quantity__c);
SODetail.Plant = line.Plant__c;
System.debug('SODetail == '+SODetail);
SODetailList.add(SODetail);
sapComDocumentSapSoapFunctionsMcS.ZstApiSoCondition conDetail= new sapComDocumentSapSoapFunctionsMcS.ZstApiSoCondition();
conDetail.ItemNo= string.valueOf(i);
conDetail.CondType = 'ZSPA';
conDetail.rate =string.valueOf(line.Price__c);
conDetail.Currency_x = 'INR';
conDetail.ConditionValue = '';
conDetail.CondValue = '';
conDetailList.add(conDetail);
i=i+10;
conData.item=conDetailList;
}
sapComDocumentSapSoapFunctionsMcS.ZttApiSoCreate Sodata = new sapComDocumentSapSoapFunctionsMcS.ZttApiSoCreate();
Sodata.item=SODetailList;//added GiSodata
sapComDocumentSapSoapFunctionsMcS.ZstApiSoPartner partnerDetail = new sapComDocumentSapSoapFunctionsMcS.ZstApiSoPartner();
partnerDetailList.add(partnerDetail);
sapComDocumentSapSoapFunctionsMcS.ZttApiSoPartner partnerData = new sapComDocumentSapSoapFunctionsMcS.ZttApiSoPartner();
partnerData.item = partnerDetailList;//added GiPartner
sapComDocumentSapSoapFunctionsMcS.ZstApiLog apiLogDeail= new sapComDocumentSapSoapFunctionsMcS.ZstApiLog();
apiLogDeail.Log = '';
apiLogDeail.Type_x = '';
apiLogDeailList.add(apiLogDeail);
sapComDocumentSapSoapFunctionsMcS.ZttApiLog logData = new sapComDocumentSapSoapFunctionsMcS.ZttApiLog();
logData.item = apiLogDeailList;
System.debug('partnerData == '+partnerData);
System.debug('conData == '+conData);
System.debug('Sodata == '+Sodata);
System.debug('logData == '+logData);
try{
resp = am.ZsdApiSoCreate(partnerData, conData, Sodata,logData);
System.debug('resp == >> '+resp);
System.debug('resp.Saledoc == >> '+resp.Saledoc);
sapComDocumentSapSoapFunctionsMcS.ZttApiLog logData1 = resp.ItRet1;
system.debug('logData1.item size is : '+(logData1.item).size());
System.debug('logData1.item == '+logData1.item);
string Emsg ='';
integer n=0;
if((logData1.item).size()>0 && resp.Saledoc =='')
{
for(sapComDocumentSapSoapFunctionsMcS.ZstApiLog a : logData1.item)
{
if(logData1.item[n].Log !='' && logData1.item[n].Log !=null)
{
Emsg = Emsg+ ' Log: '+logData1.item[n].Log+' Type : '+ logData1.item[n].Type_x;
}
n++;
}
if(Emsg=='' && resp.Saledoc =='')
{
Emsg='Please contact to the system adminstrator!!';
}
Error_SAP__c errSAP= new Error_SAP__c();
errSAP.Name= 'Order Creation Fail';
errSAP.Object_Name__c= 'Order';
errSAP.Error_Message__c= 'Failed reason: '+Emsg;
errSAP.Record_Id__c= orderId;
errorLog.add(errSAP);
system.debug('Error mEssage :'+Emsg);
}
if(resp.Saledoc != null && resp.Saledoc != '' ){
system.debug('Order successfully created in SAP and order no is : '+resp.Saledoc);
odr.Sap_Sales_Order__c = String.valueof(resp.Saledoc);// mapped order no with SF
update odr;
system.debug('Updated order: '+odr.Sap_Sales_Order__c);
}
}
// When web service fail
catch(Exception e){
Error_SAP__c err= new Error_SAP__c();
err.Name= 'Order Creation Web Service Fail';
err.Object_Name__c= 'Order';
err.Error_Message__c= 'Failed reason: '+e.getMessage();
system.debug('catch message: '+e.getMessage());
err.Record_Id__c= orderId;
errorLog.add(err);
}
// send email alert if sap order no. not generated.
if(errorLog.size()>0)
{
insert errorLog;
User Urs = [Select id,Email, Name,BU__c from User where id =: orderLst[0].OwnerId Limit 1];
Central_BU__c Cbu;
List<string> to = new List<string>();
List<String> ccLst = new List<String>();
String toName = '';
if(urs.BU__c != null){
Cbu= [Select Name,UserName__c from Central_BU__c where Name =: Urs.BU__c Limit 1];
}
User Urscentral;
system.debug('user name: '+Cbu );
if(Cbu != null)
{
if(cbu.UserName__c != Null)
{
Urscentral = [Select id,Email, Name,BU__c from User where Username =: Cbu.UserName__c Limit 1];
}
to = new string[] {urs.Email};
ccLst.add(Urscentral.Email);
}
toName = urs.Name;
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setToAddresses(to);
if(ccLst!=null){
email.setCcAddresses(ccLst);
}
email.setSubject('Error in Order Creation');
email.setHtmlBody('Hello '+toName+ ' , <br/><br/>'+errorLog[0].Error_Message__c + ' Order Creation Web Service Fail. <br/> Please check the record.');
try{
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
}catch(exception e){
system.debug('e'+e);
}
}
}
}
}
}
}
}
======================================================================
Test Class:
/* Created by : ANKIT VERMA
* Created date: 26-04-2021
* Description: test class for the Apex class "SO_Creation_On_SAP"
*/
@istest
Global class SO_Creation_On_SAP_Test implements webservicemock {
global void doInvoke(
Object stub,
Object request,
Map<String, Object> response,
String endpoint,
String soapAction,
String requestName,
String responseNS,
String responseName,
String responseType)
{
sapComDocumentSapSoapFunctionsMcS.ZstApiLog d1= new sapComDocumentSapSoapFunctionsMcS.ZstApiLog();
d1.Log = 'E';
d1.Type_x = 'Sales Doc type not Changed';
list<sapComDocumentSapSoapFunctionsMcS.ZstApiLog> dList= new list<sapComDocumentSapSoapFunctionsMcS.ZstApiLog>();
dList.add(d1);
sapComDocumentSapSoapFunctionsMcS.ZttApiLog logData = new sapComDocumentSapSoapFunctionsMcS.ZttApiLog();
logData.item = dList;
sapComDocumentSapSoapFunctionsMcS.ZsdApiSoCreateResponse_element o= new sapComDocumentSapSoapFunctionsMcS.ZsdApiSoCreateResponse_element();
o.Saledoc='';
o.ItRet1=logData;
response.put('response_x', o);
}
@TestSetup
public static void testData()
{
// Creating list type custom setting record
Central_BU__c cbu= new Central_BU__c();
cbu.name='Lighting';
cbu.UserName__c='ankit@testorg.com';
insert cbu;
Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
User usr = new User( Alias = 'kavya',Email='ankittest@gmail.com',
EmailEncodingKey='UTF-8', LastName='Testing',
ProfileId = p.Id, LanguageLocaleKey='en_US',
LocaleSidKey='en_US', TimeZoneSidKey='Asia/Kolkata',
BU__c='Lighting',
UserName='ankit@testorg.com');
insert usr;
// Creating account record
Account acc = new Account();
acc.Name = 'Tefst';
acc.BillingCity = 'Lkfo';
acc.BillingCountry ='Infdia';
acc.BillingStreet = '2f3';
acc.BillingState = 'UP';
acc.BillingPostalCode ='121s31';
insert acc;
// creating Forecasting record
Forecasting__c FC = new Forecasting__c();
FC.OwnerId = usr.id;
FC.Yearly__c = '2021';
FC.Monthly__c = 'APRIL';
FC.Total_Scheduled_Order_Amount__c = 10;
insert FC;
Integer currentYear = Date.Today().Year();
Integer currentMonth = Date.Today().Month()+1;
Integer currentDate = Date.Today().Day();
DateTime dt = Datetime.now();
// creating a Sh
Ship_to_Address__c shiptoaddrs = new Ship_to_Address__c();
shiptoaddrs.Account_Customer__c = acc.id;
shiptoaddrs.SAP_Code__c = '123456789';
shiptoaddrs.Country__c = 'India';
shiptoaddrs.City__c = 'Delhi';
shiptoaddrs.Phone__c='9877453234';
insert shiptoaddrs;
List<Order> odlist = new List<Order>();
Order od = new Order();
od.AccountId = acc.id;
od.EffectiveDate = Date.newInstance(currentYear, currentMonth, currentDate); //System.now();
od.Delivery_Date__c = system.today();
od.Status = 'Draft';
od.PO_Number_Ship_To__c = '1235ty';
od.PO_Date_Ship_To_Party__c = System.today()+5;
od.Delivery_Date__c = System.today();
od.Ship_To_Address__c = shiptoaddrs.id;
od.Forecasting__c=FC.id;
od.OwnerId=usr.id;
insert od;
Order_Line_Item__c oli = new Order_Line_Item__c();
oli.Order__c = od.id;
oli.Price__c = 100;
oli.Quantity__c = 100;
oli.Division__c='10';
oli.Distribution_Channel__c='30';
oli.Product_Name__c='Prod1010';
oli.Plant__c='9008';
insert oli;
}
@istest
public static void test1()
{
Test.startTest();
order o =[select id ,name,Sap_Sales_Order__c from order limit 1];
user u=[select id from user limit 1];
system.runAs(u)
{
Test.setMock(WebServiceMock.class, new Webservice_SO_Creation_response());
SO_Creation_On_SAP.CallToSAP(o.id);
// system.assertEquals( '123444',o.Sap_Sales_Order__c);
}
Test.stopTest();
}
// Test for negative response
@istest
public static void test2()
{
Test.startTest();
order o =[select id ,name,Sap_Sales_Order__c from order limit 1];
user u=[select id from user limit 1];
system.runAs(u)
{
Test.setMock(WebServiceMock.class, new SO_Creation_On_SAP_Test());
SO_Creation_On_SAP.CallToSAP(o.id);
system.assertEquals(o.Sap_Sales_Order__c, null);
}
Test.stopTest();
}
}
No comments:
Post a Comment