2

I'm trying to create multiple records of an object called Guarantor__c, a child of Opportunity and Contact, on button click. All of the Guarantor records should relate to the Opportunity on the page where the button is. The records are all of the Contacts of the Opportunity's Account with the Guarantor record type. The SOQL below is pretty straightforward. This runs without an error, but doesn't enter any records. Any ideas?

{!REQUIRESCRIPT('/soap/ajax/27.0/connection.js')} 
var url = parent.location.href; 
var updateRecords = []; 
var principalContacts = sforce.connection.query("Select Id From Contact where AccountId ='{!Account.Id}' and RecordTypeId ='012A0000000nr4BIAQ'"); 
var principalContactsArray = principalContacts.getArray("records"); 
if (principalContactsArray.length < 1){ 
    alert("There are no guarantors for this opportunity. Go back to the Account and enter the guarantors.")  
}else{ 
    for (eachPrincipalContact in principalContactsArray){ 
        var newGuarantor = new sforce.SObject("Guarantor__c"); 
        newGuarantor.COntact__ = eachPrincipalContact; 
        newGuarantor.Opportunity__c ="{!Opportunity.Id}"; 
        updateRecords.push(newGuarantor); 
        sforce.connection.update(updateRecords); 
    } 
}
2
  • What is the error? Apr 22, 2014 at 10:06
  • There was no error. It just refreshed the page and didn't insert the records. I ended up doing it with APEX.
    – Matt Santy
    Apr 22, 2014 at 14:29

1 Answer 1

1

Looks like you are querying Contacts, and creating these Guarantor records, but you are trying to update the new Guarantor records instead of inserting them.

The new records in your updateRecords list are never inserted into SF. You should be using a create call, not an update. You are trying to update objects that don't yet exist.

Change

sforce.connection.update(updateRecords); 

To

sforce.connection.create(updateRecords); 

You also should move the DML statement outside of the loop.

6
  • When I switch to sforce.connection.insert, it says that there is no insert method.
    – Matt Santy
    Oct 23, 2013 at 17:54
  • Im sorry. The call is not insert, its Create. Mixed up my toolkit terminology. See updated code. Oct 23, 2013 at 18:07
  • That didn't do it either. I think the issue has to do with the for loop. When I say, for (eachPrincipalContact in principalContactsArray){ alert(""+eachPrincipalContact+"");, I get 0, then 1. Whereas I was expecting the unique IDs from the SOQL query.
    – Matt Santy
    Oct 23, 2013 at 18:10
  • try eachPrincipalContact.get("Id") Oct 23, 2013 at 18:19
  • Has no method get!
    – Matt Santy
    Oct 23, 2013 at 18:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .