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
sumpritsumprit 

How to update a field based off Record Type in an SControl?

Hi there,

I need to update a checkbox on Object A , based off its related list which is Object B and also which is of LOANER record type.
Object A = CDM RMA (custom object)
Object B = RMA Add ON
Field to be updated = Loaner_Issued.
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<script type="text/javascript" src="/js/functions.js"></script>
<script src="/soap/ajax/10.0/connection.js"></script>
<script language="JavaScript">

// Global Values

var isChanged = false; // set to true if the checkbox is placed on Loan Issued checkbox

// List checkbox fields here to compare the final values with
// CDM RMA is the object name and the Loaner_Issued is the customer checkbox.

var loanerIssued = {! CDM_RMA__c.Loaner_Issued__c};

// Function created to updated the checkbox.

function RmaAddon()
{

// Create temp account for updating

var updateCDM_RMA__c = new sforce.SObject("CDM_RMA__c");

// Update each CDM RMA record based on the ID.
  updateCDM_RMA__c.ID = '{!CDM_RMA__c.Id}';

// Get the RMA Add on lists attached to the CDM RMA object.
// Tried this too.(" SELECT Id, (SELECT Id, FROM R00N30000001Xi2lEAC WHERE RecordType.Name = 'Loaner') FROM CDM_RMA__c ");

var getprods = sforce.connection.query("SELECT r.Id, r.Name, CDM_RMA__r.Name FROM RMA_Add_On__c r WHERE CDM_RMA__r.Name = '{!CDM_RMA__c.Name}'");

var records = getprods.getArray ("records");

// Here I am checking that the RMA Add-On Type is Loaner based record type and if yes then place
// checkmark on the Loan Issued checkbox.
 
for(var i=0;i<records.length;i++){
dynaBean = records[i];

if(dynaBean.$Recordtype.Name == 'Loaner'){ // Record Type
updateCDM_RMA__c.Loaner_Issued__c = 1;                  // Checkbox is the checked.
if(loanerIssued == 0) isChanged = true;                  // variable name is TRUE becuase the checkbox value is changed from False to True
}
}

// If the CDM RMA record has been changed then save the new RMA Add on type for LOANER RecordType and save the record.


if(isChanged){
var saveResult = sforce.connection.update([updateCDM_RMA__c]);
if(saveResult[0].success){
window.parent.parent.location.href = '/{! CDM_RMA__c.Name }';
} else {
alert('There was an error when saving CDM RMA Record.');
}
}
}
</script>
</head>
<body>
<script>RmaAddon();</script>
</body>
</html>
////////////////////////////////////////////////////////////////////////

Here is the problem:-

1.> Either the SOQL is not correct
2.> I am unable to tell how to update a field based off Record Type
3.> Error =
dynaBean.$Recordtype has no properties
if(dynaBean.Recordtype.Name == 'Loaner'){ // Record Type

Thanks.

jrotensteinjrotenstein
Mmm. It's a bit confusing with all of your custom records. I'd suggest breaking your problem into chunks and seeing how you go.

SOQL
You can test your SOQL using external utilities, such as those mentioned on this page.

Recordtype
I normally hard-code the recordtype ID into my query, eg "... and recordtype = '012200000000NWm'  "

When to execute
I'm having difficulty trying to figure out exactly what you're attempting to do. Do you just want something displayed on-screen as a result of your criteria, or do you actually need the field updated on the record itself?

If you are putting this in an S-control that is added to the Page Layout, then the code only gets executed (and t
e field updated) when somebody views the page.

If you only want to show the result on-screen without having to store the status of the checkbox, you can do it by displaying your own checkbox instead of trying to store it on a Salesforce object.

More information would be helpful.
sumpritsumprit
Thank you for the reply, however I managed to fix this.

thanks again
NewToSFNewToSF

How did you solve it? I am having the same problem.

thanks.

sumpritsumprit
Here is the function which is doing the actual job:-

function RmaAddon()
{

// Create temp account for updating

var updateCDM_RMA__c = new sforce.SObject("CDM_RMA__c");

// Update each CDM RMA record based on the ID.
updateCDM_RMA__c.ID = '{!CDM_RMA__c.Id}';

// Get the RMA Add on lists attached to the CDM RMA object.
// Notice how the value is based off Record type and ID.

var getrmas = sforce.connection.query ("SELECT Id, Name, CDM_RMA__c, CDM_RMA__r.Name FROM RMA_Add_On__c WHERE Recordtype.Name = 'Loaner' AND CDM_RMA__c = '{!CDM_RMA__c.Id}'");

var records = getrmas.getArray ("records");

// Next, we will first reset the value of the existing CDM RMA record, field Loaner_Issued to False.

updateCDM_RMA__c.Loaner_Issued__c = false;

// If there are Loaners available

if (records.length > 0)
{
newLoanValue = true;
}

// Next update the value of the CDM RECORD to the new value, and the checkbox will be checked.

updateCDM_RMA__c.Loaner_Issued__c = newLoanValue;
if (loanerIssued != newLoanValue) isChanged = true; // checkbox value is changed and this is the same variable defined above.


// If the CDM RMA record has been changed then save the new RMA Add on type for LOANER RecordType and save the record.


if(isChanged)
{
var saveResult = sforce.connection.update([updateCDM_RMA__c]);
if(saveResult[0].success)
{
window.parent.parent.location.href = '/{! CDM_RMA__c.Id }';
}
else {
alert('There was an error when saving CDM RMA Record.');
}
}
}
</script>
</head>
<body onload = "RmaAddon();">