Tuesday, March 19, 2019

Create an Opportunity Coming from a Different Record with a Default Sales Rep or Partners using Workflow Action Scripts

To create an Opportunity coming from a different record with a default Sale Rep or Partners via Workflow, we can use Workflow Actions Scripts. The Create Record action from a workflow would not work if Team Selling or Multi-Partner Management feature (Setup> Company> Enable Features> CRM tab) is enabled since the Sales Rep and Partners would be a sublist.

Here's a sample script that can create an Opportunity with a default Sales Rep or Partners. The workflow action script is used on a workflow of Record type, "Custom Record".

function createOpportunity(){

 var opp = nlapiCreateRecord('opportunity');

 // custrecord34 is the internal id of the custom field within the custom record that contains the company or customer
 opp.setFieldValue('entity', nlapiGetFieldValue('custrecord34'));

 // Add a sales rep
 opp.selectNewLineItem('salesteam');
 // custrecord35 is the internal id of the custom field within the custom record that contains the sales rep id
 opp.setCurrentLineItemValue('salesteam', 'employee',nlapiGetFieldValue('custrecord35'));
 // "-2" is the internal id for sales rep
 opp.setCurrentLineItemValue('salesteam', 'salesrole','-2');
 opp.setCurrentLineItemValue('salesteam', 'isprimary','T');
 opp.commitLineItem('salesteam');

 opp.selectNewLineItem('partners');
 // custrecord36 is the internal id of the custom field within the custom record that contains the partner id
 opp.setCurrentLineItemValue('partners', 'partner',nlapiGetFieldValue('custrecord36'));
 opp.commitLineItem('partners');

 var id = nlapiSubmitRecord(opp, true);
 return id;

}

Note:  If you would like to link the Opportunity on the record that it came from, we would need to set the "Return Type" parameter to "List/Record" and "List/Record" field to "Opportunity" (Customization> Scripting > Scripts > SuiteScript Record > Parameters tab).

No comments:

Post a Comment