You need to sign in to do that
Don't have an account?
madimulia
APEX Trigger to Clone Expired Contract
Hi there, I am very new at writing APEX code. I am trying to write trigger to automatically clone a contract if it expired. So here is the code I currently have and apparently it does not work.
An expired contract should only be cloned once, and the new contract status will be set to "awaiting payment". Any help on this matter would be greatly appreaciated :)
trigger CloneExpiredContract on Contract (after update) {
// List of the contracts that have been passed into the trigger.
List<Contract> Contracts = new List<Contract>();
Set<Id> AccIds = new Set<Id>(); // set of unique associated Account Ids
Map<Id, Integer> ExpiredContractCnt = new Map<Id, Integer>(); // map of Account Ids and number of Active Contracts
Map<Id, Integer> AwaitingPaymentContractCnt = new Map<Id, Integer>(); // map of Account Ids and number of Draft Contracts
// Assign new Contract values
Contracts = Trigger.new;
// make list of unique Account Ids and build maps
for (Contract con : Contracts) {
ExpiredContractCnt.put(con.accountid,0);
AwaitingPaymentContractCnt.put(con.accountid,0);
AccIds.add(con.accountid);
}
// query Contract table for any expired SLA Contracts linked to Accounts
// iterate through results to count up expired Contracts per Account
for (Contract qra : [select id, accountid from contract where accountid in :ExpiredContractCnt.KeySet() and
recordtypeid = '0123000000003Tf' and status = 'Expired']) {
// increment counter in Account map for each Active contract
integer cnta = ExpiredContractCnt.get(qra.accountid);
integer cntb = 0;
ExpiredContractCnt.put(qra.accountid, cnta+1);
//Check to see if contract has been cloned before
for (Contract qrb : [select id, accountid from contract where accountid in :AwaitingPaymentContractCnt.KeySet() and
recordtypeid = '0123000000003Tf' and status = 'Awaiting Payment']) {
// increment counter in Account map for each Draft contract
cntb = AwaitingPaymentContractCnt.get(qrb.accountid);
AwaitingPaymentContractCnt.put(qrb.accountid, cntb+1);
}
if (cntb > 0) {
qra.clone();
}
}
}