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
QMEQME 

List Button: Unable to update a field value for multiple records

Hi,

 

I'm unable to update the Status(PIMS__Status__c) field of a custom Incentive object (PIMS__Incentive__c) with the value "Complete"

 

i am able to retrieve the record IDs for the Incentive Object sucessfully but am unable to retrieve the value of the Status field for the record id in the loop. 

how do i get the value for a field besides the id on a custom object? am i supposed to use a select statement or the get method.  i don't seem to be using the get method correctly.

 

 

--------------------------

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

var userList = {!GETRECORDIDS($ObjectType.PIMS__Incentive__c )}
var updatedList = [];

if (userList[0] == null) {
         alert("Please select at least one record.") }
else {
          var answer = confirm("Are you sure?");          
          if (answer==true) {
                  
                  for (var n=0; n<userList.length; n++) {
                          var i = new sforce.SObject("PIMS__Incentive__c");                                              
                          i.id = userList[n];

                           var cs = i.get("PIMS__Status__c");
                          alert("ID =  " + i.id + i.PIMS__Status__C + cs );    
                      
                        
//                        only update records where current status is Pending Payment
                          if (i.PIMS__Status__c == "Pending Payment") {
                                alert("You have selected " + userList.length + " records");
                                 i.PIMS__Status__c = "Complete";
                                 updatedList.push(i);                    
                          }
                  }               
                 result = sforce.connection.update(updatedList);
                 window.location.reload();
                 alert("Status set to Complete for all records with current status Pending Payment")
          }
}

Best Answer chosen by Admin (Salesforce Developers) 
b-Forceb-Force

Here is updated script for you

 

 

{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")} 
var userList = {!GETRECORDIDS($ObjectType.PIMS__Incentive__c )};

/*Following code will arrange in proper format  */
var SelectedIds=''; 
for(var i=0;i < userList.length; i++)
{
SelectedIds+="'"+userList[i]+"',";
}
SelectedIds=SelectedIds.substring(0,SelectedIds.length - 1);
/*arrange in proper format Ends */


var updatedList = [];

if (userList[0] == null) 
{ 
         alert("Please select at least one record.");
} 
else
{ 
          var answer = confirm("Are you sure?");          
          if (answer==true) 
	{	{
	/*Formated Query*/
	var strQuery="Select PIMS__Status__c,Id from PIMS__Incentive__c where ID  in ("+SelectedIds+")"; 
	var result = sforce.connection.query(strQuery);
	var records = result.getArray("records");
	/*Formated Query  Ends */
               for (var n=0; n<records.length; n++) 
		{ 
			if (records[n].get("PIMS__Status__c") == "Pending Payment")
			{
                        var i = new sforce.SObject("PIMS__Incentive__c");                                              
                        i.id = records[n].id;
			i.PIMS__Status__c = "Complete";                      
			updatedList.push(i);
			}
                  }               
                 var upresult = sforce.connection.update(updatedList); 
	if (upresult[0].getBoolean("success"))
	{
                 window.location.reload(); 
	}
	else
	{
		alert("Error occurred in updating Lead :" + upresult[0]);

	}
	}
}

 

 

Hope this will help you,

 

Thanks,

Bala

All Answers

b-Forceb-Force

you are trying this exercise in some incorrect way.

As  var cs = i.get("PIMS__Status__c") this will always returns NULL,

your code will never enter into your loop , where you are filling the List to update

 

When we play with some List button , we are having only Record Id not whole record (Which is available in case of a Page Button)

 

I will post updated script

 

Thanks,

Bala 

 

 

b-Forceb-Force

Here is updated script for you

 

 

{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")} 
var userList = {!GETRECORDIDS($ObjectType.PIMS__Incentive__c )};

/*Following code will arrange in proper format  */
var SelectedIds=''; 
for(var i=0;i < userList.length; i++)
{
SelectedIds+="'"+userList[i]+"',";
}
SelectedIds=SelectedIds.substring(0,SelectedIds.length - 1);
/*arrange in proper format Ends */


var updatedList = [];

if (userList[0] == null) 
{ 
         alert("Please select at least one record.");
} 
else
{ 
          var answer = confirm("Are you sure?");          
          if (answer==true) 
	{	{
	/*Formated Query*/
	var strQuery="Select PIMS__Status__c,Id from PIMS__Incentive__c where ID  in ("+SelectedIds+")"; 
	var result = sforce.connection.query(strQuery);
	var records = result.getArray("records");
	/*Formated Query  Ends */
               for (var n=0; n<records.length; n++) 
		{ 
			if (records[n].get("PIMS__Status__c") == "Pending Payment")
			{
                        var i = new sforce.SObject("PIMS__Incentive__c");                                              
                        i.id = records[n].id;
			i.PIMS__Status__c = "Complete";                      
			updatedList.push(i);
			}
                  }               
                 var upresult = sforce.connection.update(updatedList); 
	if (upresult[0].getBoolean("success"))
	{
                 window.location.reload(); 
	}
	else
	{
		alert("Error occurred in updating Lead :" + upresult[0]);

	}
	}
}

 

 

Hope this will help you,

 

Thanks,

Bala

This was selected as the best answer
QMEQME

Thanks Bala for your unwavering assistance  !!!!!  :smileyvery-happy:

b-Forceb-Force

you are welcome.....:)

 

 

DevWannabeDevWannabe
b-force...
Is it possible to update multiple fields instead of just one?