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
emuelasemuelas 

javascript List to check if a field on all selected records is the same

Hi,

 

I have  a javascript list button "Create Purchase order" on custom object Order_Lines__c(child object of Sales_Order__c) which creates a purchase order for the selected order lines.

The code goes like this :

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


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

/*Ensure atleast one order line is selected*/

if (records[0] == null)
{
alert("Please select at least one Order Line")
}

/*Create a purchase order for the selected records*/
else
{
for (var n=0; n<records.length; n++) {
var c = new sforce.SObject("Order_line__c");
c.id = records[n];
newRecords.push(c);
}
result = sforce.connection.update(newRecords);
/*Open a new Purchase Order form and prepopulate with values*/

window.location.href ="/a0I/e?retURL=%2Fa0I%2Fo"+
"&CF00NR0000000cvG6={!Sales_Order__c.Name}"+
"&CF00NR0000000cvEt={!Sales_Order__c.Default_Warehouse__c}" +
"&00NR0000000cvEU={!Sales_Order__c.Order_Date__c}
}

This is working fine.
Now i want to add another validation:The field supplier__c on the order lines object must have the same value for the selected records.


I have tried adding the validation as follows:

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

var records = {!GETRECORDIDS($ObjectType.Order_Line__c)};
var newRecords = [];
/*Check if records are selected*/
if (records[0] == null)
{
alert("Please select at least one Order Line");
}


 else{
          var myarray=new Array();
          for (var n=0; n<records.length; n++) {
var c = new sforce.SObject("Order_line__c");
c.id = records[n];
/*Copy the supplier field into an array for all the selected order lines*/
myarray[n]=c.supplier__c;
              }                                                             }
var t=array.length-1;
for (var d=0;d<t;d++)
{
if(array[d] <>array[d+1]){
alert("SOrry ,need same supplier for each order line");

}

else
{
/*Code for creating a new Purchase order*/
}
}

however this is not working and it gives a syntax error.

 

Please help with the syntax and the logic used  if its not ok.

davescardavescar

Since I don't know the specifics of your error, the only thing that jumps out at me is that you have a typo in your reference to the array you're using to store the suppliers.  You declare the array as "myarray", but you are referencing it as "array".  Straighten that out and see if it helps.

emuelasemuelas

Hi

 

I have modified the code as suggested

 

But when i click on the button i still get the syntax error

"A problem with the OnClick JavaScript for this button or link was encountered:

syntax error".

 

The modified code:

 

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

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

/*Check if records are selected*/

if (records[0] == null)
{
alert("Please select at least one Order Line");
}


 else{
         var myarray=new Array();
         for (var n=0; n<records.length; n++)
        {
                 var c = new sforce.SObject("Order_line__c");
                 c= records[n];

            /*Copy the supplier field into an array for all the selected order lines*/
            myarray[n]=c.supplier__c;
            }
                                                            }
    var t=myarray.length-1;

    for (var d=0;d<t;d++)
        {
             if(myarray[d] <>myarray[d+1])
            {
              alert("SOrry ,need same supplier for each order line");

            }
         else
            {
              alert("Replace with code for creating purchase order");
            }
    

        }

    }

 

 

 

Please help me with this....iam trying to check everything ....the curly brackets everything,just doesnt work!