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
Angel118Angel118 

Updating a Object from two other Objects

Hi Everyone,
                      I need to populate values in a Object from two other objects.
when i tried from one  it works fine, but when i added the second object fields its not updating.

I think because u can use update only once on a Object,(Tell me if I am wrong).
So I tried merging the result set of the two arrays into one, and then updating it.

But the merged Array does not capture the value of the second array and accepts as "Undefined"

This is what I did.

Code:
var BookDetails  = sforce.connection.query("select Available_Copies__c,Author_Name__c,Number_of_Pages__c from Book__c where Name = '{!Book_Placement__c.Book__c}'");

var AccountDetails = sforce.connection.query("select Site,Website,AccountNumber from Account where Name = '{!Book_Placement__c.Account__c}'");


var recArray = BookDetails.getArray("records");
alert(recArray [0].Available_Copies__c + "/" +recArray [0].Author_Name__c + "/" +recArray [0].Number_of_Pages__c);

var secArray = AccountDetails.getArray("records");

alert(secArray [0].Site+ "/" +secArray [0].Website+ "/" +secArray [0].AccountNumber);

var myArray = recArray.concat(secArray);

alert(myArray [0].Available_Copies__c + "/" +myArray [0].Author_Name__c + "/" +myArray [0].Number_of_Pages__c + "/" + myArray [0].Site +myArray [0].Website+ "/" +myArray [0].AccountNumber);

 Can anybody suggest on this..

Thanks in Advance
Angel

DevAngelDevAngel
I'm not sure what you are trying to accomplish here.  Are you trying to join two unrelated objects together?

If you concat two arrays of different objects, you will not be able to access the properties as you have shown.  Array element[0] may be an account and array element[1] may be the custom field.  concatenating arrays does not merge the two types of objects.

Please describe the schema and the relationship between the Account and custom object and we might find a single query will work.



Cheers
Angel118Angel118
This was my intent in here to populate values from two different objects in one S-Control.


Angel118Angel118
My Intent was to update the Object from two different objects values in one go !!
please suggest !!
DevAngelDevAngel
But I don't know what that means exactly.

If each query is returning exactly one record, you could populate a single javascript object like this

Code:
var newObj = {};
newObj.account = ...getArray("records")[0];
newObj.custom = ...getArray("records")[0];

alert(newObj.account.Id + ' is the account id.');
alert(newObj.custom.Id + ' is the custom object id.');

 

Angel118Angel118
Hi Dev,
             Thanks for that help, I think I am not able to make u understand what I need.

There are 2 custom Objects - Books & Books Placed

Books Placed is an intermediate of Books and Account. It contains two look ups one from Account(Name) and other from Book (Book Name).  Now I have some more fields in Books Placed which need to come from Account and Books.

Fields from Account - Site, Website and Account Number
Books - Author Name, Number of Pages & Available Copies

so whenever one clicks one create new book placement - he selects the account and books only. and on save the this record will open  in view mode so I have overrided it on view mode, to get values from the respective account and Books.

My query returns the right values but I am only able to update values form only one object either Account or Books...

I will paste the code also, here so that u can understand it better,,,,

<script type="text/javascript" src="/soap/ajax/9.0/connection.js" > </script>

<script type="text/javascript" >

 

alert("{!Book_Placement__c.Id}  ");

 

var BookDetails  = sforce.connection.query("select Name,Available_Copies__c,Author_Name__c,Number_of_Pages__c from Book__c where Name = '{!Book_Placement__c.Book__c}'");

alert(BookDetails);

 

 

if(BookDetails.size>0)

{

var recArray = BookDetails.getArray("records");

alert(recArray [0].Name + "/"+ recArray [0].Available_Copies__c + "/" +recArray [0].Author_Name__c + "/" +recArray [0].Number_of_Pages__c);

 

var BookRec =new sforce.SObject("Book_Placement__c");

BookRec.Id="{!Book_Placement__c.Id}";

BookRec.Available_Copies__c=recArray [0].Available_Copies__c;

BookRec.Author_Name__c=recArray [0].Author_Name__c;

BookRec.Number_of_Pages__c=recArray [0].Number_of_Pages__c;

sforce.connection.update([BookRec]);

 

if(sforce.connection.update([BookRec])[0].getBoolean("success"))

else

alert("Problem in Updating the Book Details to Book Placement");

}

 

var AccountDetails  = sforce.connection.query("select Name,Site,Website,AccountNumber from Account where Name = '{!Book_Placement__c.Account__c}'");

alert(AccountDetails);

 

if(AccountDetails.size>0)

{

var recArray = AccountDetails.getArray("records");

 

alert(recArray [0].Name + "/"+ recArray [0].Site+ "/" +recArray [0].Website+ "/" +recArray [0].AccountNumber);

 

var AccRec =new sforce.SObject("Book_Placement__c");

AccRec.Id="{!Book_Placement__c.Id}";

AccRec.Site=recArray [0].Site;

AccRec.Website=recArray [0].Website;

AccRec.AccountNumber=recArray [0].AccountNumber;

sforce.connection.update([AccRec]);

 

if(sforce.connection.update([BookRec])[0].getBoolean("success"))

else

alert("Problem in Updating the Account Details to Book Placement");

 

}

window.parent.location.href="{!URLFOR( $Action.Book_Placement__c.View, Book_Placement__c.Id,null,true)}";

</script>


DevAngelDevAngel
Hi Angel118,

Ok, I see what you want to do.

The account query will never return anything because you are using an Id as a criteria for the Account.Name field.  The account name is, well, the name of the account, not an Id.

Here's what I see from your code:

var BookDetails  = sforce.connection.query("select  Name, Available_Copies__c, Author_Name__c, Number_of_Pages__c from Book__c where Name = '{!Book_Placement__c.Book__c}'");

I think this should be:

var BookDetails = sforce.connection.query("select Name, Available_Copies__c, Author_Name__c, Number_of_Pages__c From Book__c where Id = '{!Book_Placement__c.Book__c}'");

The Name field is not an Id, and Book_Placement__c.Book__c is an Id.

You have the same problem with the account query:

var AccountDetails  = sforce.connection.query("select Name, Site, Website, AccountNumber from Account where Name = '{!Book_Placement__c.Account__c}'");

Should be:

var AccountDetails  = sforce.connection.query("select Name, Site, Website, AccountNumber from Account where Id = '{!Book_Placement__c.Account__c}'");

The other thing I would change is run a single update, not two updates.

Here is a stab at what the code should look like.  I did not remove any code, just commented and shuffled the logic a bit.

Code:
<script type="text/javascript" src="/soap/ajax/9.0/connection.js" > </script>
<script type="text/javascript" >

alert("{!Book_Placement__c.Id}  ");

//Get both related records (could probably use a relationship query on Book_Placement__c for this
var BookDetails  = sforce.connection.query("select Name,Available_Copies__c,Author_Name__c,Number_of_Pages__c from Book__c where Id = '{!Book_Placement__c.Book__c}'");
alert(BookDetails);
var AccountDetails  = sforce.connection.query("select Name,Site,Website,AccountNumber from Account where Id = '{!Book_Placement__c.Account__c}'");
alert(AccountDetails);

//Create the new SObject that we will update
var BookRec =new sforce.SObject("Book_Placement__c");
//Set the id using a merge field
BookRec.Id="{!Book_Placement__c.Id}"; 

//This checks that BOTH queries returned values, this should always be true if the lookups were
//required on the "new" screen.
if(BookDetails.size > 0 && AccountDetails.size > 0) {
 //Grab the query records
 var bookArray = BookDetails.getArray("records");
 alert(bookArray[0].Name + "/" + bookArray[0].Available_Copies__c + "/" + bookArray[0].Author_Name__c + "/" + bookArray[0].Number_of_Pages__c);

 //var BookRec =new sforce.SObject("Book_Placement__c");
 //BookRec.Id="{!Book_Placement__c.Id}"; 
 //Set the values that we want from the Book Detail on the placement record
 BookRec.Available_Copies__c = recArray[0].Available_Copies__c;
 BookRec.Author_Name__c = recArray[0].Author_Name__c;
 BookRec.Number_of_Pages__c = recArray[0].Number_of_Pages__c;

 //Grab the account query records
 var acctArray = AccountDetails.getArray("records");
 alert(acctArray[0].Name + "/" + acctArray[0].Site + "/" + acctArray[0].Website+ "/" + acctArray[0].AccountNumber);
 
 //Set the values that we want from the Account Detail on the placement record
 BookRec.Site = acctArray[0].Site;
 BookRec.Website = acctArray[0].Website;
 BookRec.AccountNumber = acctArray[0].AccountNumber;
 
 //Update the placement record
 sforce.connection.update([BookRec]);

 //Check for errors, if none, redirect
 if(sforce.connection.update([BookRec])[0].getBoolean("success")) {
  window.parent.location.href="{!URLFOR( $Action.Book_Placement__c.View, Book_Placement__c.Id,null,true)}";
 } else {
  alert("Problem in Updating the Book Details to Book Placement");
 }
}

//var AccountDetails  = sforce.connection.query("select Name,Site,Website,AccountNumber from Account where Name = '{!Book_Placement__c.Account__c}'");
//alert(AccountDetails);

//if(AccountDetails.size>0) {
// var recArray = AccountDetails.getArray("records");
//
// alert(recArray [0].Name + "/"+ recArray [0].Site+ "/" +recArray [0].Website+ "/" +recArray [0].AccountNumber);
// 
// var AccRec =new sforce.SObject("Book_Placement__c");
// AccRec.Id="{!Book_Placement__c.Id}";
// AccRec.Site=recArray [0].Site;
// AccRec.Website=recArray [0].Website;
// AccRec.AccountNumber=recArray [0].AccountNumber;
//
// sforce.connection.update([AccRec]);
//
// if(sforce.connection.update([BookRec])[0].getBoolean("success")) {
// } else {
//  alert("Problem in Updating the Account Details to Book Placement");
// }
//}

//window.parent.location.href="{!URLFOR( $Action.Book_Placement__c.View, Book_Placement__c.Id,null,true)}";

</script>

 


Message Edited by DevAngel on 09-28-2007 12:16 PM

Angel118Angel118
Thanks Dev for your effort but now the query does not even dont return anything. and the neither the record is updated.

My previous code was working fine but it was just that I was only able to update the values only from one Object,

The second query returned the values as i needed but was not able to populate the same in the same S-Control.

what i think is that we cannot use -  sforce.connection.update(XXXX]); twice in a S-Control (Correct me if i am wrong).

Angel 118

Angel118Angel118
Dev,
         Can u suggest anything else on this,

Can Anyone else who can guide me on this ?

Thanks in Advance..

Angel..
Angel118Angel118
Hi everyone...
Can anyone help on this !!!!
Angel118Angel118
Thanks a Lot Dev for your Help...

Its Done....

Angel