Friday 10 July 2020

Salesforce - Open SAP from Salesforce

Scenario:

I got a requirement from one of my client that while using salesforce they want to open SAP. So they need a button on order screen which will pass few values from salesforce into SAP.Once user click the button in salesforce, SAP screen should popup on the salesforce screen with all the values of order auto populated in SAP. This will help the reps to enter order information into their SAP system. If SAP is not login then the SAP popup should still appear but with an error that SAP is not login.

Design:

After a lot of struggle we were able to make this happen but it has few down sides too. 
We created a javascript button in salesforce below is the code of it. This javascript query all the data from case object related to order and call a bat file which is on client centralized location. This centralized location is easily accessible by all the users from client side. Then this bat file call SAP.

Drawbacks

This solution will only work if salesforce is open in internet explorer as chrome and other browsers have different level of security for Active XObject which is not allowing to open another application.

We need a VBS file which listen to our request and enter all the values send by salesforce into fields.

VBS file and BAT file should be on same location not accessible by all users who want to use this functionality.

Code of button:


{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}

{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")}


var ContactInfo = sforce.connection.query("select Id,Contact.RecordTypeId,Contact.LastName,Contact.FirstName,Contact.Phone,Contact.Email,Contact.MailingCountryCode, Contact.MailingCity,Contact.MailingStreet, Contact.MailingStateCode, Contact.MailingPostalCode,Contact.MobilePhone from case where id = '{!Case.Id}'");


records = ContactInfo.getArray("records");


if(records[0].Contact.RecordTypeId != 'ABC9999999'){
alert('Order type not supported, this feature.Enter order manually');
}

else if( records[0].Contact.MailingCity && records[0].Contact.MailingStreet && records[0].Contact.MailingStateCode && (records[0].Contact.Phone || records[0].Contact.MobilePhone)&& records[0].Contact.MailingPostalCode && records[0].Contact.MailingCountryCode && records[0].Contact.FirstName){


var ConCity = records[0].Contact.MailingCity;
var ConStreet = records[0].Contact.MailingStreet;
var ConState = records[0].Contact.MailingStateCode;
var ConPostalCode = records[0].Contact.MailingPostalCode;
var ConCountry = records[0].Contact.MailingCountryCode;
var ConFirstName = records[0].Contact.FirstName;
var ConLastName = records[0].Contact.LastName;
var Name = ConFirstName +' ' +ConLastName;
var CaseID = '{!Case.CaseNumber}';
if(records[0].Contact.Email){
var ConEmail = records[0].Contact.Email;}
else
var ConEmail = '';
if(records[0].Contact.Phone){
var ConPhone = records[0].Contact.Phone;}
else
var ConPhone = '';
if(records[0].Contact.MobilePhone){
var ConMobile = records[0].Contact.MobilePhone;}
else
var ConMobile = '';
var PONumber = (ConPhone == ''?ConMobile:ConPhone) ;


var path = "\\\testlocation\\SFDC_test.bat " +'"'+ConPhone+'"'+' '+'"'+Name+'"'+' '+'"'+ConLastName+'"'+' '+'"'+ConStreet+'"'+ ' '+'"'+ConPostalCode+'"'+' '+'"'+ConCity+'"'+ ' ' +'"'+ConState+'"'+ ' '+'"'+ConCountry+'"'+' ' +'"'+ConEmail+'"'+ ' ' +'"'+CaseID+'"'+ ' ' +'"'+ConMobile+'"'+ ' ' +'"'+PONumber+'"';

MyObject = new ActiveXObject("WScript.Shell");


MyObject.Run(path);

}

else if( !records[0].Contact.MailingCity || !records[0].Contact.MailingStreet ||
!records[0].Contact.MailingStateCode || !records[0].Contact.MailingPostalCode || !records[0].Contact.MailingCountryCode){

alert('Please provide complete address on Contact');
}
else if( !records[0].Contact.Phone && !records[0].Contact.MobilePhone){

alert('Please provide Phone/Mobile Phone on Contact');
}
else if( !records[0].Contact.FirstName){

alert('Please provide First Name on Contact');
}
else{
alert('There is some error in the data');
}

Code of bat File

@echo off
 
SET p1="%~1" 
SET p2="%~2"
SET p3="%~3" 
SET p4="%~4"
SET p5="%~5" 
 
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
 
SET p6="%~0"
SET p7="%~1"
SET p8="%~2"
SET p9="%~3"
SET p10="%~4"
SET p11="%~5"
SET p12="%~6"
SET p13="%~7"
SET p14="%~8"
SET p15="%~9"
 
  
 
wscript "\\testlocation\demo.vbs" %p1% %p2% %p3% %p4% %p5% %p6% %p7% %p8% %p9% %p10% %p11% %p12% %p13% %p14% %p15%
 
Note: You may see more parameters in bat file. You can add or subtract as many parameters as per your need.

No comments:

Post a Comment