I have built a Trigger on the opportunity, my query looks at the Opportunity and it's child Service_Contract__r and validates the the child size is not greater than 0.
for(Opportunity opp:Trigger.new){ // For Loop that removes Opportunities that already had PO In and only has updated Opportunitues with PO In
if(Trigger.oldMap.get(opp.id).stageName != 'PO In (100%)' && opp.stageName == 'PO In (100%)' && opp.recordTypeId == oppRecordID)
oppIds.add(opp.id);
}
List<Opportunity> oppsFromDb = [ SELECT
id,
(SELECT id FROM Service_Contracts__r WHERE isDeleted = FALSE),
FROM
Opportunity
WHERE
id IN:oppIds
];
if(Trigger.isBefore){ // Error checking, will verify that a contract child does not exist, will also verify that if any variables are null.
for(Opportunity verifyOpp: oppsFromDB){
if(verifyOpp.service_Contracts__r.size() > 0)
trigger.newMap.get(verifyOpp.Id).addError('A Service Contract already exists, cannot have more than one contract '+ verifyOpp.service_Contracts__r.size()+ ' '+ verifyOpp.service_Contracts__r.get(0).id);
In the live server, after deleting a Service contract and going through the process again it gives me the addError that the size > 0 (it says there is 1 that exists and I pulled it's id and when I tried to look at it on SF it said it did not exist.
Do I need to do something like a deconstructor and delete all my lists after they are done being used? I don't understand how it is still getting a size greater than 0. when I do an 'Execute Anonymous' the size comes up as 0 ... Any thoughts?
(Small note, it ran fine in my sandboxes. Only errors on the live.)
UPDATE Still not working, I tried to do oppsFromDb.clear() (and so on with the other lists) at the end of the trigger. Still no luck, very puzzling to me. As Well from the trigger I pulled up the field of isDeleted and it returned FALSE, however I still cannot see this Service Contract.
Here is the entire Trigger:
trigger trig_Opportunity_CreateServContract on Opportunity (before update, after update) {
//Get the record type required
ID oppRecordID; //the Opportunity Hyperstream record ID
ID sCRecordID; //the Service Contract Hyperstream record ID
list<RecordType> recHyperStream = [ SELECT
description, id, sObjectType, businessProcessId
FROM
RecordType
WHERE
developerName = 'someValue'
];
for(RecordType r: recHyperStream){ //for loop created to find which Record type is for the opportunity and Service Contract
if(r.sObjectType=='Opportunity')
oppRecordID= r.id;
else if(r.sObjectType == 'ServiceContract') //only two options, if the SobjectType is not Opportunity then it is ServiceContract!
sCRecordID = r.id;
}
List<id> oppIds = new List<id>(); //Get the Id of all new opportunity updates
List<ServiceContract> sContract = new List<ServiceContract>(); // List that will contain all the new Service Contracts that need to be added
for(Opportunity opp:Trigger.new){ // For Loop that removes Opportunities that already had PO In and only has updated Opportunitues with PO In
if(Trigger.oldMap.get(opp.id).stageName != 'PO In (100%)' && opp.stageName == 'PO In (100%)' && opp.recordTypeId == oppRecordID)
oppIds.add(opp.id);
}
List<Opportunity> oppsFromDb = [ SELECT
id, stageName, name, accountid, owner__c, end_user_contact__c, recordTypeId, ownerId,Opportunity_Contact__c,
(SELECT id FROM Service_Contracts__r WHERE isDeleted = FALSE),
(SELECT id From quotes WHERE IsSyncing= TRUE)
FROM
Opportunity
WHERE
id IN:oppIds
];
if(Trigger.isBefore){ // Error checking, will verify that a contract child does not exist, will also verify that if any variables are null.
for(Opportunity verifyOpp: oppsFromDB){
if(verifyOpp.service_Contracts__r.size() > 0)
trigger.newMap.get(verifyOpp.Id).addError('A Service Contract already exists, cannot have more than one contract '+ verifyOpp.service_Contracts__r.size()+ ' '+ verifyOpp.service_Contracts__r.get(0).id);
else if(verifyOpp.quotes.size() == 0 || verifyOpp.quotes.size()== null)
trigger.newMap.get(verifyOpp.Id).addError('A quote is either not synced or does not exist, please verify that the quote is correct.');
else if(verifyOpp.quotes.get(0).Agreement_Term__c == '' ||verifyOpp.quotes.get(0).Agreement_Term__c ==null) //Verify if Agreement Term is not null in Synced Quote
trigger.newMap.get(verifyOpp.Id).addError('The service agreement is not set in the quote, please set it before changing the stage.');
else if(verifyOpp.quotes.get(0).Commencement_Date__c == null) //Verify if Commencement Date is not null
trigger.newMap.get(verifyOpp.Id).addError('The service commencement date has not been chosen, please pick a commencement date.');
//Need to add more error checking.
}
}
if(Trigger.isAfter){
for(Opportunity goodOpps: oppsFromDb){
SContract.add(new ServiceContract( name = goodOpps.name, accountId = goodOpps.accountId,
recordTypeId = sCRecordID, opportunity__c = goodOpps.id,Agreement_Term__c = goodOpps.quotes.get(0).Agreement_Term__c ,
ownerId = goodOpps.OwnerId, Quote__c = goodOpps.quotes.get(0).id, ContactId = goodOpps.Opportunity_Contact__c,
Commencement_Date__c = goodOpps.quotes.get(0).Commencement_Date__c, Set_Up_Fee_Totals__c = goodOpps.quotes.get(0).Set_Up_Fee_Totals__c,
Data_Transfer_Overage_Rate__c = goodOpps.quotes.get(0).Data_Transfer_Overage__c, Entitled_Transcoding__c= goodOpps.quotes.get(0).Entitled_Transcoding__c, Transcoding_Overage_Rate__c= goodOpps.quotes.get(0).Transcoding_Overage__c,
Entitled_Media_Storage__c= goodOpps.quotes.get(0).Entitled_Media_Storage__c, Entitled_Media_Reqest__c= goodOpps.quotes.get(0).Entitled_Media_Reqest__c,
Entitled_Media_Object__c= goodOpps.quotes.get(0).Entitled_Media_Object__c, Entitled_Data_Transfer__c= goodOpps.quotes.get(0).Entitled_Data_Transfer__c, Equipment_Totals__c= goodOpps.quotes.get(0).Equipment_Totals__c,
Monthly_Recurring_Totals__c= goodOpps.quotes.get(0).Monthly_Recurring_Totals__c, Media_Storage_Overage_Rate__c= goodOpps.quotes.get(0).Media_Storage_Overage__c,
Media_Request_Overage_Rate__c= goodOpps.quotes.get(0).Media_Request_Storage_Overage__c, Media_Object_Overage_Rate__c= goodOpps.quotes.get(0).Media_Object_Overage__c));
}
insert sContract; //add all the Service Contracts
//clearing lists
oppIds.clear();
sContract.clear();
oppsFromDb.clear();
}
}// END OF TRIGGER
