function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
dquirkedquirke 

Custom S Control Modification

Hi All,

 

I am trying to modify a Custom S control to look at two separate objects as validation before a sales order is created. The original part of the code was as follows:

 

function success(qr) { 
//Create SO Header 
var it = new sforce.QueryResultIterator(qr); 
var OppId = "{!Opportunity.Id}"; 
var AccountId = "{!Account.Id}"; 
var records = qr.getArray("records"); 
while(it.hasNext()){ 
var record = it.next(); 

if (record.PBSI__hasSO__c == "1") 

window.parent.location.href="/" + OppId; 
failure("This opportunity has already been converted to a sales order!"); 

else 

var salesorder = new sforce.SObject("PBSI__PBSI_Sales_Order__c"); 
salesorder.PBSI__Customer__c = record.AccountId; 
salesorder.PBSI__Lead_Source__c = record.LeadSource; 
salesorder.PBSI__Created_From__c = record.Name; 
salesorder.PBSI__Opportunity__c = record.Id; 
salesorder.PBSI__Customer_Purchase_Order__c = record.PBSI__Customer_PO__c; 
salesorder.PBSI__Order_Discount__c=record.PBSI__Discount__c; 

result = sforce.connection.create([salesorder]); 

salesorder.id = result[0].id; 

 

This works fine. However, I am trying to add in a section to get it to look at the "account status" field on the account and if it has a value of either "On Hold" or "Gone Legal" it will also fail to create a sales order.

 

I came up with the following:

 

function success(qr) { 
//Create SO Header 
var it = new sforce.QueryResultIterator(qr); 
var OppId = "{!Opportunity.Id}"; 
var AccountId = "{!Account.Id}"; 
var records = qr.getArray("records"); 
while(it.hasNext()){ 
var record = it.next(); 

if (record.PBSI__hasSO__c == "1") 

window.parent.location.href="/" + OppId; 
failure("This opportunity has already been converted to a sales order!"); 

if (record.c2g__CODACreditStatus__c == "On Hold") 

window.parent.location.href="/" + AccountId; 
failure("Oi Dingbat, This account is On Hold!"); 


else 

var salesorder = new sforce.SObject("PBSI__PBSI_Sales_Order__c"); 
salesorder.PBSI__Customer__c = record.AccountId; 
salesorder.PBSI__Lead_Source__c = record.LeadSource; 
salesorder.PBSI__Created_From__c = record.Name; 
salesorder.PBSI__Opportunity__c = record.Id; 
salesorder.PBSI__Customer_Purchase_Order__c = record.PBSI__Customer_PO__c; 
salesorder.PBSI__Order_Discount__c=record.PBSI__Discount__c; 

result = sforce.connection.create([salesorder]); 
salesorder.id = result[0].id; 

 

As you can see I defined the "AccountId" variable but I am not a javascript programmer so I've obviously gone wrong somewhere.

 

Any help much appreciated.

 

All the best


David Quirke

 

 

indy_sfdcindy_sfdc

Hi David

 

I am not sure what the actual question is here, not sure what the qr (query) looks like being passed in (does it have the values you are using in your if statements).  I would also look at using if...elseif....else construct of the if statement.

 

What are you seeing, is this code failing or is it just not working as expected?

 

 

dquirkedquirke

Hi Randy, 

 

Basically, all I am trying to do is add a bit of extra code in as follows:

 

if (record.c2g__CODACreditStatus__c == "On Hold":smileywink: 

window.parent.location.href="/" + AccountId; 
failure("Oi Dingbat, This account is On Hold!":smileywink:

 

The error message about the dinbat is just a joke for the moment and will obviously be changed to something more serious. When this part of the code is not in it works perfectly. When I add the above it does not work in that this part of the code I entered has no effect.

 

What I would like to do is get the code to look at the "Credit Status" field on the relevant Account record. If that status is set to "On Hold" then stop the process of creating a sales order. 

 

However, I must be coding it incorrectly.

 

The full code is below. You can see the part I tried to enter about 1/3 of the way down but it has no effect. 

 

<!DOCTYPE HTML PUBLIC 
"-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 

<html> 

<head> 
<script type="text/javascript" src="/js/functions.js"></script> 
<script src="/soap/ajax/13.0/connection.js"></script> 

<script> 
function initPage() { 
var OppId = "{!Opportunity.Id}"; 

var qr = sforce.connection.query("Select PBSI__Customer_PO__c,PBSI__Discount__c, PBSI__hasSO__c, Id, Name, CreatedDate, LeadSource, AccountId from Opportunity where Id = '" + OppId + "'",{onSuccess : success, onFailure : failure});



function success(qr) { 
//Create SO Header 
var it = new sforce.QueryResultIterator(qr); 
var OppId = "{!Opportunity.Id}"; 
var AccountId = "{!Account.Id}"; 
var records = qr.getArray("records"); 
while(it.hasNext()){ 
var record = it.next(); 

if (record.PBSI__hasSO__c == "1") 

window.parent.location.href="/" + OppId; 
failure("This opportunity has already been converted to a sales order!"); 

if (record.c2g__CODACreditStatus__c == "On Hold") 

window.parent.location.href="/" + AccountId; 
failure("Oi Dingbat, This account is On Hold!"); 


else 

var salesorder = new sforce.SObject("PBSI__PBSI_Sales_Order__c"); 
salesorder.PBSI__Customer__c = record.AccountId; 
salesorder.PBSI__Lead_Source__c = record.LeadSource; 
salesorder.PBSI__Created_From__c = record.Name; 
salesorder.PBSI__Opportunity__c = record.Id; 
salesorder.PBSI__Customer_Purchase_Order__c = record.PBSI__Customer_PO__c; 
salesorder.PBSI__Order_Discount__c=record.PBSI__Discount__c; 

result = sforce.connection.create([salesorder]); 
salesorder.id = result[0].id; 



//Create Transaction Log 
var log = new sforce.SObject("PBSI__Transaction_Log__c"); 
log.PBSI__Type__c = "Opp2SO"; 
log.PBSI__Main_Object_Name__c = OppId; 
log.PBSI__Main_Object_Type__c = "Opportunity"; 
log.PBSI__Secondary_Object_Name__c = result[0].id; 
log.PBSI__Secondary_Object_Type__c = "Sales Order"; 
logresult = sforce.connection.create([log]); 
//Create SO Lines 

var OppId = "{!Opportunity.Id}"; 

var lineresult = sforce.connection.query("Select Id, PBSI__Item__c, PBSI__Quantity__c,PBSI__Unit_of_Measure__c, PBSI__Sale_Price__c,PBSI__Price4__c,PBSI__Discount__c from PBSI__PBSI_Opportunity_Line__c where PBSI__Opportunity__c = '" + OppId + "'"); 


var nolines =0; 



if (lineresult.size > 0) 

linecount=lineresult.getArray("records"); 
var itline = new sforce.QueryResultIterator(lineresult); 


while(itline.hasNext()) 


var record = itline.next(); 
var opplineCheck = new sforce.SObject("PBSI__PBSI_Opportunity_Line__c"); 
opplineCheck.Id = record.Id; 

opplineCheck.PBSI__Line_Status__c = "Closed"; 
resultOpplineCheck = sforce.connection.update([opplineCheck]); 
var solines = new sforce.SObject("PBSI__PBSI_Sales_Order_Line__c"); 

solines.PBSI__Unit_of_Measure__c=record.PBSI__Unit_of_Measure__c; 
solines.PBSI__Item__c = record.PBSI__Item__c; 
solines.PBSI__Quantity_Needed__c = record.PBSI__Quantity__c; 
solines.PBSI__Price__c = record.PBSI__Sale_Price__c; 
solines.PBSI__Price4__c=record.PBSI__Price4__c; 
solines.PBSI__Discount__c=record.PBSI__Discount__c; 
solines.PBSI__Sales_Order__c = result[0].id; 


alert(sforce.connection.create([solines])); 


nolines=linecount.length; 


var Opp = new sforce.SObject("Opportunity"); 
Opp.Id = OppId; 
Opp.StageName = "Closed Won"; 
Opp.PBSI__hasSO__c = "1"; 
Opp.PBSI__Sales_Order__c=result[0].id; 
resultOpp = sforce.connection.update([Opp]); 

var resultSO = sforce.connection.query("Select name from PBSI__PBSI_Sales_Order__c where id='" + salesorder.id + "'"); 
soName = resultSO.getArray("records")[0].Name; 

if (result[0].getBoolean("success")) { 
alert("New Sales Order created with Name " + soName + " and " + nolines + " lines"); 
window.parent.location.href="/" + salesorder.id; 
} else { 
alert("Failed to create Sales Order! " + salesorder.id); 





function failure(error) { 
alert("Warning: " + error); 

</script> 
</head> 

<body onload="initPage();"> 
</body> 

</html> 

indy_sfdcindy_sfdc

A few things I see in the code.

 

It looks like you are using the query results with the variable qr, but the query result does not include the status field that you are later trying to check.   here is the query:

 

var qr = sforce.connection.query("Select PBSI__Customer_PO__c,PBSI__Discount__c, PBSI__hasSO__c, Id, Name, CreatedDate, LeadSource, AccountId from Opportunity where Id = '" + OppId + "'",{onSuccess : success, onFailure : failure});

 

but in your if statement you are looking for

 

if (record.c2g__CODACreditStatus__c == "On Hold&quot:smileywink: 

 

And this field does nto exist in the query. So it can never actually get into your failure code or it will fail trying to find the field in the resultset.  Thsi is probably your issue but I did not test.

 

I would guess you should also look at using if....elseif...else vs multiple if (doubt this is your error but would be a little cleaner)   http://www.w3schools.com/js/js_if_else.asp

 

As you continue to enhance your SControls you should think about moving to Visualforce

 

Make sure you keep a copy of your original code before you start making changes, at worst you can always get back to where you started.


Good luck and happy coding!