2

What's best way to update records based on record type using custom button.

Below code partially works and fields updates inside 'If block' doesn't work. What I'm missing here ?

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

var records = {!GETRECORDIDS($ObjectType.Case)}; 
var newRecords = []; 

if (records[0] == null) 
{ 
alert("Please select at least one row") 
} 
else 
{ 
for (var n=0; n<records.length; n++) { 
var c = new sforce.SObject("Case"); 
c.id = records[n]; 
c.Status = "Canceled"; 
if('{!Case.RecordTypeId}' == '012d0000000xFShAAM'){
c.Type= "Other"; 
c.Sub_Type__c= "Other"; 
}
else if('{!Case.RecordTypeId}' == '012d0000000P2ZrAAK'){
c.CanceledReason__c="Spam";
}
c.ownerid='{!$User.Id}'; 
newRecords.push(c); 
} 

result = sforce.connection.update(newRecords); 
window.location.reload(); 
}
1

I'm assuming this is on a List view of cases, so you are not going to have access to the RecordTypeId of those records unless you query for it. Below is an example that should work for what you are trying to do here.

EDIT: Updated the code to not use that terrible query in the for loop.

{!REQUIRESCRIPT("/soap/ajax/34.0/connection.js")} 
var records = {!GETRECORDIDS($ObjectType.Contact)};
var newRecords = [];
var query; 
var result;
var queryResult;
if (records.length) {
      query="Select Id,RecordTypeId from Case where Id IN "+ "('" + records.toString().replace(/,/g, "','") + "')";
      queryResult=sforce.connection.query(query);
      CaseWithRecordType = queryResult.getArray("records"); 
      for (var n=0; n<CaseWithRecordType.length; n++){
        var c = new sforce.SObject("Case"); 
        c.Id = CaseWithRecordType[n].Id; 
        c.Status = "Canceled";
        if(CaseWithRecordType[n].RecordTypeId == '012d0000000xFShAAM'){
          c.Type= "Other"; 
          c.Sub_Type__c= "Other"; 
        }
        else if(CaseWithRecordType[n].RecordTypeId == '012d0000000P2ZrAAK'){
          c.CanceledReason__c="Spam";
        }
        c.ownerid='{!$User.Id}'; 
        newRecords.push(c);
      }
      result = sforce.connection.update(newRecords); 
      window.location.reload(); 
} else {
    alert('Select one or more Cases');
}
  • Hi, yes its on case list view... it does work, but its taking a while to process records.. I tested with 100 records... guessing it wont go further. I'm limiting to 100.. any idea to move the query out of for loop ? – Muthu Samy Feb 25 '16 at 19:10
  • Yeah, wasn't sure how many you'd be processing at a time. I'll have to update this later after work. Not hard at all to move it out, was just a bit lazy throwing it in the loop. – cricketlang Feb 25 '16 at 19:22
  • @MuthuSamy i just decided to update it before i left for lunch. This should work better for you. I should have just posted it in the first place but was kinda lazy. – cricketlang Feb 25 '16 at 19:41
  • That works great \m/ Thanks a lot lazy man :P – Muthu Samy Feb 25 '16 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.