You need to sign in to do that
Don't have an account?
Angel118
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:
Can anybody suggest on this..
Thanks in Advance
Angel
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
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
please suggest !!
If each query is returning exactly one record, you could populate a single javascript object like this
Code:
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>
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:
Message Edited by DevAngel on 09-28-2007 12:16 PM
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
Can u suggest anything else on this,
Can Anyone else who can guide me on this ?
Thanks in Advance..
Angel..
Can anyone help on this !!!!
Its Done....
Angel