+ Start a Discussion
milliskennymilliskenny 

Update the referring object with the ID of the new related object created

Hopefully this is an easy one.  In the code below, I create a custom object (Dept) that has a one to many relationship with the contact (one Dept to many contacts).  The s-control starts from the contact and creates the new Dept with the account_id and the address information from the contact.  I use the create function on the Dynabean to create the Dept, and then retrieve the Id of the new object so I can open the record in the modal window.
 
The issue arises when I try to update the Contact record with the id of the new Dept.  I get a javascript error referencing line 3653, with the error message of "exception thrown and not caught".
 
I am pretty new to all this stuff, so I looked at the js library file for AJAX 3.3 beta and the function around line 3653 reads:
 
Sforce.Dynabean.prototype.set = function(propName, value) {
 var fldDef = this.definition.fieldMap.getItem(propName);
 var cr = this.definition.childRelationships.getItem(propName.toLowerCase());
 var rn = this.definition.parentRelationships.getItem(propName.toLowerCase());
 if (fldDef == null && cr == null && rn == null) {
  throw "The property " + propName + " is not a valid field.";
 } else {     <----- This is line 3653
  //Validate type if possible
  //TODO validate other types
  if (fldDef != null) {
   if (fldDef.type == "date" || fldDef.type == "datetime") {
    if (Sforce.Util.dltypeof(value) != "date" && Sforce.Util.dltypeof(value) != "datetime" && value != null) {
     throw "Invalid type: You passed a " + Sforce.Util.dltypeof(value) + " but the field requires a " + fldDef.type + ".";
    }
   }
  }
 
It appears that it cannot find a reference to the field I want to write back to.  The field is of type "lookup" and should reference a Dept related object.
 
Here is the section of the code where I do the update.  Thanks in advance for any help!
 
//This is where I create the new Dept
var newDeptList = new Array();

var newDept = new Sforce.Dynabean("Department__c");
newDept.set("Id","");
newDept.set("Name", name + " - " + deptPrompt );
newDept.set("Account__c", "{!Account_Id}" );
newDept.set("Legal_Entity__c", legal_entity);
newDept.set("Address__c",mailing_street);
newDept.set("City__c",mailing_city);
newDept.set("StateProvince__c",mailing_state);
newDept.set("Country__c",mailing_country);
newDept.set("Postal_Code__c",mailing_postalcode);

newDeptList[0] = newDept;

// Create new Department
results = sforceClient.Create(newDeptList);

//If the first value in the result array is null then it's a fault.

if( results[0] != null &&
results[0].success == true )
 
//Now I go to get the Id of the new dept record I created
{
// Fetch department id
newDeptId = results[0].id;


//Update the existing contact with the new Dept ID
var contactUpdateList = new Array();

var contactUpdate = new Sforce.Dynabean("contact");
contactUpdate.set("Id","{!Contact_Id}");
 
//The next line is where it fails
contactUpdate.set("NewDepartment__c",newDeptId);

contactUpdateList[0] = contactUpdate;

//Update the Contact
var resultsCon = sforceClient.update(contactUpdateList);

contactUpdateList.save();
 

Message Edited by milliskenny on 10-03-2006 01:51 AM

ChitraChitra
Are you sure the Custom object is getting created..

suggestions:
----------------

1. Make sure custom obj ID is not null.
2. Try to update contact without the custom Object ref..
    I would start with checking the dynabean instantiation .. check if you have typed "Contact" or "contact" .. I am not sure if this is case sensitive..
    Make sure you are actually able to update the contact without the Custom object field..
3. check if the profile you are using . has permissions to update contact..

I am just throwing out ideas here.. Hope this helps...

Thanks,
Chitra



Ron HessRon Hess
check to see if you are initalizing the scontrol with the 6.0 or 7.0 API end point, you should use 7.0
if i recall , relations to std objects came in 7.0

also look into your enterprise WSDL to make sure you have the field spelled correctly, it should match something in there.
   
sean_at_33050sean_at_33050
For the benefit of others searching this thread:
When you get an "Exception thrown and not caught" error message, add code to "catch" it.
ie:
Code:
try {

....code here...
}

catch(errmsg){
   alert( errmsg.toString() );
}

 
This should display a popup box with a good bit of detail to help debug what is going wrong.
 
Sean