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
The KnightThe Knight 

can we avoid sforce.connection.login() in javascript in VF page.

Hi,
   I am using connection.js in visualforce page, to insert data into SObject. The functionality is working fine.The problem is I have to provide user name and password in the  following statement.sforce.connection.login("username","password") . Is there a way to avoid this statement in the javascript. please find the code below.
Code:
<apex:page controller="ReplenishableLocations2">
<script src="/soap/ajax/13.0/connection.js"></script> 
<script>
/* This method is used to select 
       or deselect all checkboxes*/
function selectordeselect(field,box){
        if(box.checked){
          for(i=0;i<field.length;i++){
            field[i].checked = true;
          }
        }else if(!box.checked){

            for(i=0;i<field.length;i++)
               field[i].checked = false;
         }
}


/* this method reads the values and
       stores them in the SObject*/
   function store(){
        items = document.getElementsByName("itemsname");
        itemArray = new Array();
        j=0;
        for(i=0;i<items.length;i++){
          if(items[i].checked){
            itemArray[j] = items[i].value;
            j = j+1;
          }        
        }        
        locations = document.getElementsByName("locationsname");
        locationArray = new Array();
        k =0;
        for(l=0;l<locations.length;l++){
          if(locations[l].checked){
            locationArray[k] = locations[l].value;
            //alert(locationArray[k]);
            k = k+1;
          }        
        } 
        var itemlocations = new Array();
        var count = 0;
        var twoD = 0;
         itemlocations[0] = new Array();
         for(m=0;m<itemArray.length;m++){
           for(n=0;n<locationArray.length;n++){                  
                  var itemloc = new sforce.SObject("Item_location__c");
                  itemloc.Item_Name__c = itemArray[m];
                  itemloc.Location_Name__c = locationArray[n];
                  itemloc.ItemLoc_ExId__c = itemArray[m]+"at"+locationArray[n];
                  itemlocations[twoD].push(itemloc);
                 // alert(itemloc);
                  count = count + 1;
                  if(count==200){
                    twoD = twoD + 1; 
                      itemlocations[twoD] = new Array();
                    count = 0;                
                  }                  
            }
        } 
        
       var callCompleted = false;
    try {//the login statement needs to be avoided?
         sforce.connection.login("admin@applabde364.com", "mypasswordxxxxx");
           for(var iter=0;iter<itemlocations.length;iter++){
          // alert(iter+ "====" + itemlocations.length);
           var itemloc = new Array();
            itemloc  =  itemlocations[iter];
           // alert(itemloc);
           //alert("Item locations"+itemlocations[iter]);      
             var result = sforce.connection.create(itemlocations[iter]);
             callCompleted = true;
              for (var i=0; i<result.length; i++) {
                 if (result[i].getBoolean("success")) {
                    log("new Item_Location__c created with id " + result[i].id);
                 } else {
                   log("failed to create Item_Location__c " + result[i]);
                 }
              }
           }
   } catch(error) {
     alert("Failed to insert Item Location with error: " + error);
   }

} 
function log(message) {
    alert(message);
}   
</script>

<apex:form >
<apex:pageBlock id="pageblock">
<input type="checkbox" name="masteritem" id="masterItem" onclick="selectordeselect(document.getElementsByName('itemsname'),document.getElementById('masterItem'));">SelectAllItems<br/>
<apex:pageblockSection title="Items" id="pageblocksec1">
<apex:datatable value="{!items}" var="iter">
<apex:column >
<input type="checkbox" value="{!iter.Item_Name__c}" name="itemsname">{!iter.Item_Name__c}
</apex:column>
</apex:datatable>
</apex:pageblockSection>
<input type="checkbox" name="masterLocation" id="masterLocation" onclick="selectordeselect(document.getElementsByName('locationsname'),document.getElementById('masterLocation'));">SelectAllLocations<br/>

<apex:pageblockSection title="Locations" id="pageblocksec2">
<apex:datatable value="{!locations}" var="iterations">
<apex:column >
<input type="checkbox" value="{!iterations.Location_Name__c}" name="locationsname">{!iterations.Location_Name__c}
</apex:column>
</apex:datatable>
</apex:pageblockSection>
<apex:commandButton onclick="store();" value="Submit" rerender="outblock"></apex:commandButton>
</apex:pageBlock>
</apex:form> 
</apex:page>

 The controller:
Code:
public class ReplenishableLocations2 {

String message =  null;

Public List<Item__c> getItems(){
List<Item__c> items = new List<Item__c>();
items = [select Item_Name__c from Item__c order by Item_Name__c];
if(items == null){
  this.setmessage('Currently there are no Items to Display, Please add items');
}
return items;

}
public List<Location__c> getLocations(){
List<Location__C> locations = new List<Location__c>();
locations = [select Location_Name__c from Location__c order by Location_Name__c];
if(locations==null){
  this.setMessage('There are no Locations added to display, Please add Locations');
}
return locations;
}


public void setmessage(String s){
  this.message = s;
}
public String getMessage(){
  return this.message;
}



}

 
Here I am invoking the javascript on "onclick" event. The username and password are hardcoded in the javascript. I am not supposed to do the hardcoding, neither the user will enter the user id and password as he will be already logged in.
ShamSham
you can try setting this before calling any method to  force the connection.js to use your current sessionID

sforce.connection.sessionId = "{!$Api.Session_ID}";
The KnightThe Knight

Thanks a lot Sham. Its working fine. 

 

Rohit2006Rohit2006
Hey!! Thanks man.Even I was facing the same problem for many days.Thanks a lot:smileyhappy: