i need help. i create custom button to delete and create multiple object in one button. but i need to check it first, is there any paid payment in there? if yes, system will give error, but if no, system will delete all existing record and create them again. here is my code:
var PaymentCheck = sforce.connection.query("SELECT Id FROM Payment__c WHERE Opportunity__c = '{!Opportunity.Id}' AND Paid__c = true");
var records = PaymentCheck.getArray("records");
alert(records.length); //-----------i got the value----------------
if(records.length>0){
alert('Opportunity already has paid payment');
}
else{
var paymentDelete = sforce.connection.query("SELECT Id FROM Payment__c WHERE Opportunity__c = '{!Opportunity.Id}'",{onSuccess:success,onFailure:failure});
function success(paymentDelete){
var recordToDelete = paymentDelete.getArray("recordToDelete");
alert(recordToDelete.length);//-----------i didn't got proper value--------
if(recordToDelete.length>0){
var idsForDeletion = [];
for (var i=0; i<recordToDelete.length; i++) {
idsForDeletion.push(recordToDelete[i].Id);
}
sforce.connection.deleteIds(idsForDeletion);
}
//Identify Oppty Object
var oppty = new sforce.SObject("Opportunity");
oppty.id = "{!Opportunity.Id}";
oppty.StudentId = "{!Opportunity.Student_NameId__c}";
if("{!Opportunity.Early_Bird_IDR__c}" != "") {oppty.EarlyBirdIDR= '{!TEXT(Opportunity.Early_Bird_IDR__c)}';}
if("{!Opportunity.Deposit_SSS__c}" != "") {oppty.Deposit = '{!TEXT(Opportunity.Deposit_SSS__c)}';}
if("{!Opportunity.Final_Amount_SU__c}" != "") {oppty.FinalAmount = '{!TEXT(Opportunity.Final_Amount_SU__c)}';}
if (oppty.EarlyBirdIDR == "") {
alert("Please Fill Early Bird (IDR) Before Generate Payment");
}
else if (oppty.Deposit == "") {
alert("Please Fill Deposit Before Generate payment");
}
else {
var FirstPayment = new sforce.SObject("Payment__c");
var NextPayment = new sforce.SObject("Payment__c");
//Create Deposit payment
var DepositPayment = new sforce.SObject("Payment__c");
DepositPayment.Name = "Deposit Payment";
DepositPayment.Student_Name__c = oppty.StudentId;
DepositPayment.Opportunity__c = oppty.id;
DepositPayment.Payment_Type__c = "Deposit";
DepositPayment.Payment_Method__c = "Transfer";
DepositPayment.Payment_Amount_IDR_SA__c = oppty.Deposit;
sforce.connection.create([DepositPayment]);
FirstPayment.Name = "First Payment";
FirstPayment.Student_Name__c = oppty.StudentId;
FirstPayment.Opportunity__c = oppty.id;
FirstPayment.Payment_Type__c = "First Payment";
FirstPayment.Payment_Method__c = "Transfer";
FirstPayment.Payment_Amount_IDR_SA__c = oppty.FinalAmount/2;
sforce.connection.create([FirstPayment]);
NextPayment.Name = "Second Payment";
NextPayment.Student_Name__c = oppty.StudentId;
NextPayment.Opportunity__c = oppty.id;
NextPayment.Payment_Type__c = "Next Payment";
NextPayment.Payment_Method__c = "Transfer";
NextPayment.Payment_Amount_IDR_SA__c = (oppty.FinalAmount/10)*3;
sforce.connection.create([NextPayment]);
NextPayment.Name = "Third Payment";
NextPayment.Student_Name__c = oppty.StudentId;
NextPayment.Opportunity__c = oppty.id;
NextPayment.Payment_Type__c = "Next Payment";
NextPayment.Payment_Method__c = "Transfer";
NextPayment.Payment_Amount_IDR_SA__c = (oppty.FinalAmount/10)*2;
sforce.connection.create([NextPayment]);
window.location.reload();
}
}
function failure(error) {
alert('You cannot unenroll for: '+ '{!Opportunity.Name}' + '. You are not currently enrolled in this opportunity.');
}
}
Best regards,
Brian