从执行jscript列表按钮创建多个相关对象

时间:2013-10-23 14:01:03

标签: javascript soap salesforce apex-code

我正在尝试按下按钮,创建一个名为Guarantor__c的对象的多个记录,这是一个机会和联系的孩子。所有担保人记录都应与按钮所在页面上的“机会”相关。记录是具有担保人记录类型的机会账户的所有联系人。下面的SOQL非常简单。这运行没有错误,但不输入任何记录。有什么想法吗?

{!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.create(updateRecords); 
    } 
}

1 个答案:

答案 0 :(得分:0)

sforce.connection.create(updateRecords);

应放在for循环之外。像

for (eachPrincipalContact in principalContactsArray){ 
    var newGuarantor = new sforce.SObject("Guarantor__c"); 
    newGuarantor.COntact__ = eachPrincipalContact; 
    newGuarantor.Opportunity__c ="{!Opportunity.Id}"; 
    updateRecords.push(newGuarantor);        
}
sforce.connection.create(updateRecords);