trigger AutoCreateRecordsTest on Opportunity (before Insert, Before Update) {
for(Opportunity O: trigger.new){
if(o.isWon == true && o.HasOpportunityLineItem == true){
String opptyId = o.Id;
OpportunityLineItem[] OLI = [Select UnitPrice, Quantity, PricebookEntry.Product2Id, TotalPrice, Name, PricebookEntry.Product2.Name, Description, Country__c, CurrencyIsoCode
From OpportunityLineItem
where OpportunityId = :opptyId];
{
Case c = new Case();
for(OpportunityLineItem ol: OLI){
if(ol.PricebookEntry.Product2.Name == 'De-Registration'){
c.AccountId = o.AccountId;
c.Country__c = ol.Country__c;
c.CurrencyIsoCode = ol.CurrencyIsoCode;
c.Related_Opportunity__c = o.Id;
c.Subject = ol.Name + ' ' + ol.Country__c;
c.Type = 'De-Registration';
c.origin = 'Client';
c.Priority = 'Medium';
c.Status = 'New Service';
c.Due_Date__c = o.CloseDate;
c.Amount__c = ol.TotalPrice;
}
List<Case> FindParentCase = [Select Id, Country__c, RecordTypeID, AccountId FROM Case WHERE Country__c = :c.Country__c AND AccountId = :c.AccountId AND RecordTypeId = '01220000000FpC8'];
for(case ca: FindParentCase){
c.ParentId = ca.Id;
}
}
insert c;}
{
Case c1 = new Case();
for(OpportunityLineItem ol: OLI){
if(ol.PricebookEntry.Product2.Name == 'Change of Company Details'){
c1.AccountId = o.AccountId;
c1.Country__c = ol.Country__c;
c1.CurrencyIsoCode = ol.CurrencyIsoCode;
c1.Related_Opportunity__c = o.Id;
c1.Subject = ol.Name + ' ' + ol.Country__c;
c1.Type = 'Change of Company Details';
c1.origin = 'Client';
c1.Priority = 'Medium';
c1.Status = 'New Service';
c1.Due_Date__c = o.CloseDate;
c1.Amount__c = ol.TotalPrice;
}
List<Case> FindParentCase = [Select Id, Country__c, RecordTypeID, AccountId FROM Case WHERE Country__c = :c1.Country__c AND AccountId = :c1.AccountId AND RecordTypeId = '01220000000FpC8'];
for(case ca: FindParentCase){
c1.ParentId = ca.Id;
}
}
insert c1;
}
{
Case c2 = new Case();
for(OpportunityLineItem ol: OLI){
if(ol.PricebookEntry.Product2.Name == 'Compliance Hours'){
c2.AccountId = o.AccountId;
c2.Country__c = ol.Country__c;
c2.CurrencyIsoCode = ol.CurrencyIsoCode;
c2.Related_Opportunity__c = o.Id;
c2.Subject = o.Name;
c2.Type = 'Compliance Hours';
c2.origin = 'Client';
c2.Priority = 'Medium';
c2.Status = 'New Service';
c2.Due_Date__c = o.CloseDate;
c2.Amount__c = ol.TotalPrice;
}
List<Case> FindParentCase = [Select Id, Country__c, RecordTypeID, AccountId FROM Case WHERE Country__c = :c2.Country__c AND AccountId = :c2.AccountId AND RecordTypeId = '01220000000FpC8'];
for(case ca: FindParentCase){
if(ol.PricebookEntry.Product2.Name == 'Compliance Hours'){
c2.ParentId = ca.Id;
}
}
}
insert c2;
}
}
}
}
trigger AutoCreateRecordsTest on Opportunity (before Insert, Before Update) {
for(Opportunity O: trigger.new){
if(o.isWon == true && o.HasOpportunityLineItem == true){
String opptyId = o.Id;
OpportunityLineItem[] OLI = [Select UnitPrice, Quantity, PricebookEntry.Product2Id, TotalPrice, Name, PricebookEntry.Product2.Name, Description, Country__c, CurrencyIsoCode
From OpportunityLineItem
where OpportunityId = :opptyId];
{
Case c = new Case();
for(OpportunityLineItem ol: OLI){
if(ol.PricebookEntry.Product2.Name == 'De-Registration'){
c.AccountId = o.AccountId;
c.Country__c = ol.Country__c;
c.CurrencyIsoCode = ol.CurrencyIsoCode;
c.Related_Opportunity__c = o.Id;
c.Subject = ol.Name + ' ' + ol.Country__c;
c.Type = 'De-Registration';
c.origin = 'Client';
c.Priority = 'Medium';
c.Status = 'New Service';
c.Due_Date__c = o.CloseDate;
c.Amount__c = ol.TotalPrice;
}
List<Case> FindParentCase = [Select Id, Country__c, RecordTypeID, AccountId FROM Case WHERE Country__c = :c.Country__c AND AccountId = :c.AccountId AND RecordTypeId = '01220000000FpC8'];
for(case ca: FindParentCase){
c.ParentId = ca.Id;
}
}
insert c;
}
{
Case c1 = new Case();
for(OpportunityLineItem ol: OLI){
if(ol.PricebookEntry.Product2.Name == 'Change of Company Details'){
c1.AccountId = o.AccountId;
c1.Country__c = ol.Country__c;
c1.CurrencyIsoCode = ol.CurrencyIsoCode;
c1.Related_Opportunity__c = o.Id;
c1.Subject = ol.Name + ' ' + ol.Country__c;
c1.Type = 'Change of Company Details';
c1.origin = 'Client';
c1.Priority = 'Medium';
c1.Status = 'New Service';
c1.Due_Date__c = o.CloseDate;
c1.Amount__c = ol.TotalPrice;
}
List<Case> FindParentCase = [Select Id, Country__c, RecordTypeID, AccountId FROM Case WHERE Country__c = :c1.Country__c AND AccountId = :c1.AccountId AND RecordTypeId = '01220000000FpC8'];
for(case ca: FindParentCase){
c1.ParentId = ca.Id;
}
}
insert c1;
}
{
Case c2 = new Case();
for(OpportunityLineItem ol: OLI){
if(ol.PricebookEntry.Product2.Name == 'Compliance Hours'){
c2.AccountId = o.AccountId;
c2.Country__c = ol.Country__c;
c2.CurrencyIsoCode = ol.CurrencyIsoCode;
c2.Related_Opportunity__c = o.Id;
c2.Subject = o.Name;
c2.Type = 'Compliance Hours';
c2.origin = 'Client';
c2.Priority = 'Medium';
c2.Status = 'New Service';
c2.Due_Date__c = o.CloseDate;
c2.Amount__c = ol.TotalPrice;
}
List<Case> FindParentCase = [Select Id, Country__c, RecordTypeID, AccountId FROM Case WHERE Country__c = :c2.Country__c AND AccountId = :c2.AccountId AND RecordTypeId = '01220000000FpC8'];
for(case ca: FindParentCase){
if(ol.PricebookEntry.Product2.Name == 'Compliance Hours'){
c2.ParentId = ca.Id;
}
}
}
insert c2;
}
}
}
}