Salesforce CPQ provides the Quote Calculator Plugin (QCP), which helps developers to add additional functionality to the quote line editor in Salesforce CPQ using custom JavaScript code.
Salesforce CPQ offers a way to call an Apex method through an optional parameter “Connection” from JSForce Library, JSForce is a third-party library that provides a unified way to perform queries, execute Apex REST calls, use theMetadata API, or make HTTP requests remotely.
The JSforce Library offers multiple APIs for performing various operations in Salesforce. You can perform all these operations from Salesforce CPQ’s QCP, without the need for establishing explicit authentication, This can be accomplished by using an optional argument “conn” in all QCP methods.
Take a look at the example below where we call an Apex method to send an email to the sales rep manager from the “onAfterCalculate” method in QCP
QCP method – onAfterCalculate
export function onAfterCalculate(quote, lines, conn) {
try {
let body = { "salesRep" : quote.record.SBQQ__SalesRep__c, "quoteName": quote.record.Name};
conn.apex.post("/ApexCallFromQCP/", body ,
(err, res) => { console.log("response: ", res); }
);
} catch (e) {
console.error(e);
}
return Promise.resolve();
}
In the above method, we use the optional argument ‘conn‘ to invoke our below custom ApexRest API, ‘ApexCallFromQCP,’ using JSForce’s #apex-rest functionality.
@RestResource(urlMapping='/ApexCallFromQCP/*')
global class MyTestApexRest {
@HttpPost
global static String sendEmailFromQCP() {
try{
String reqBody = RestContext.request.requestBody.toString();
QCPRequest req = (QCPRequest)JSON.deserialize(reqBody, QCPRequest.class);
User saleRep = [SELECT Id, Name, Manager.Name, Manager.email
FROM User WHERE Id =: req.salesRep];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[]{saleRep.Manager.email});
mail.setSubject('Email Sent from QCP');
String body = 'Hello '+ saleRep?.Manager?.Name +'\n '+ saleRep.Name + ' is working on '+req.quoteName;
mail.setPlainTextBody(body);
Messaging.SendEmailResult[] results = Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});
if (results[0].isSuccess())
return 'Email sent successfully';
else
return 'Failed to send email: ' + results[0].getErrors()[0].getMessage();
}catch (Exception e) {
return 'An error occurred: ' + e.getMessage();
}
}
public class QCPRequest {
public String salesRep;
public String quoteName;
}
}
Resources –
Salesforce CPQ Plugins
Javascript Quote Calculator Plugin
JSforce Document