• Mike Rethage
  • NEWBIE
  • 10 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 7
    Replies
Hello,

I have written the following code and have done extensive sandbox testing.  I have several triggers that are very similar but go off of different fields in a contract.  I have a test written for lines 1-12 but can't seem to find the best methodology to writing from line 14 down.  Any help is appreciated.

Code:
trigger Contract_CreateTV_New on Contract (after update){
    System.debug('Contract_CreateTV_New triggered');
    
    Set<Id> activatedContractIds = new Set<Id>();

    for (Contract each : trigger.new) {
        if (each.Status == 'In Progress' && each.RecordTypeId == [Select Id From RecordType WHERE Name = 'Touchtown Contract'].Id && trigger.oldMap.get(each.Id).Status != 'In Progress')
        {
            System.debug('Contract ' + each.Id + ' transitioned into IN PROGRESS status.');
            activatedContractIds.add(each.Id);
            
            if (each.TV_Systems__c != null) {
                                
                for (Integer i = 0; i < each.TV_Systems__c; i++) {
                    Equipment__c newEQPT = new Equipment__c (
                        Account__c = each.AccountId,
                        Name = [Select Name From Account WHERE Id =: each.AccountId].Name,
                        RecordTypeId = [Select Id From RecordType WHERE Name = 'TV+'].Id,
                        Contract__c = each.Id,
                        Product_Type__c = 'TV+',
                        Warranty__c = 'Original (Lifestyle)'
                    );
                    
                    insert newEQPT;
                     List<ContractContactRole> contactRoles = [select ContractId, ContactId from ContractContactRole where Role = 'Install Contact' and ContractId in :activatedContractIds];
    
                    for (ContractContactRole eachRole : contactRoles) {Contract eachContract = trigger.newMap.get(eachRole.ContractId);
                    
                    Junction_Equipment_Contact__c newJunct = new Junction_Equipment_Contact__c(
                        Equipment__c = newEQPT.Id,
                        Contact__c = eachRole.ContactId
                    );
                    
                    insert newJunct;
                    
                    System.debug('Added DS: ' + newEQPT.Id);
                }
            }            
        }
    }
}
}

Test:
@isTest(seealldata=true)
private class TEST_EquipmentCreation {

    static testmethod void TEST_EquipmentCreation()
        
    {
        Account Acct = new Account();
        acct.Name = 'Test Account';
            insert Acct;
                
        Contact Contact = new Contact();
        Contact.FirstName = 'Mike';
        Contact.LastName = 'Test';
        Contact.AccountId = acct.Id;
        Contact.Email = 'test@test.com';
        insert Contact;
        
        Contract newContract = new Contract();
        newcontract.Name = 'test contract';
        newcontract.AccountId = acct.Id;
        newcontract.Status = 'draft';
        newcontract.StartDate = system.today();
        newcontract.RecordTypeId = [Select Id From RecordType WHERE Name = 'Touchtown Contract'].id;
        insert newcontract;
        
        ContractContactRole CCR = new contractContactRole();
        CCR.ContractId = newcontract.Id;
        CCR.ContactId = contact.Id;
        CCR.Role = 'Install Contact';
            insert CCR;
       

        newcontract = [SELECT Status FROM Contract WHERE ID=: newcontract.Id];
        newcontract.status = 'In Progress';
        newcontract.ActivatedDate = system.today();
        update newcontract;
        
     
        newcontract.TV_Systems__c=1;
        update newcontract;
        newcontract.DS_Systems__c=1;
        update newcontract;
       	newcontract.Calendars2__c=1;
        update newcontract;
        newcontract.TTRA_Licenses2__c=500;
        newcontract.Resident_Apps_Level__c = 'Premium';
        newcontract.Web_Shows__c =1;
        newcontract.TTRA_Licenses2__c =500;
        update newcontract;
        
           		for (Integer i = 0; i<newcontract.DS_Systems__c; i++) {
                    Equipment__c newEQPT = new Equipment__c (
                        Account__c = acct.id,
                        Name = acct.Name,
                        RecordTypeId = [Select Id From RecordType WHERE Name = 'Digital Signs'].Id,
                        Contract__c = newcontract.Id,
                        Product_Type__c = 'Digital Sign',
                        Warranty__c = 'Original (Lifestyle)'
                    );
                    
                    insert newEQPT;
                    
                    //List<ContractContactRole> contactRoles = [select ContractId, ContactId from ContractContactRole where Role = 'Install Contact'];
    
                    //for (ContractContactRole eachRole : contactRoles) {Contract eachContract = trigger.newMap.get(eachRole.ContractId);
                    
                    //Junction_Equipment_Contact__c newJunct = new Junction_Equipment_Contact__c(
                        //Equipment__c = newEQPT.Id,
                        //Contact__c = eachRole.ContactId
                    //);
                    
                    //insert newJunct;
                    
                   // System.debug('Added DS: ' + newEQPT.Id);
                 														}     
                //}
        
    } 
    

}

 
I've seen a lot of frustration around sending automated emails to various contact roles both in Contracts and Opportunities.  It seems like one of the leading soluions is to create a custom field and populate it via apex with the appropriate contact role.  But what about if you have multiple contact roles that fit the criteria?  For example, I may have multiple "Install Contacts" on a contract that I would want to get the same email.  How can I automate this?  Via a Trigger?

Thanks!
Hello,

Our business process demands automatic equipment record creation when a contract is activated.  When the contract is entered the user would enter numeric values for Product 1, Product 2 etc.  I want the flow to look-up those numeric values and create the number of new equipment records specified.  I.e Product1 =16 it would create 16 new equipment records under that account.  Additionally I would want information such as the Install Contact from the contracts Contact Role to be populated in all the equipment records.

Is an Apex trigger the best way to accomplish this?
Thanks!
I created a custom button for the "leads" page so that our sales team can have a one-click follow-up created for various lengths of time.  The below button is for a 1 week follow-up and works just fine on the leads page:

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 

var myTask = new sforce.SObject("Task"); 
myTask.WhoId = "{!Lead.Id}"; 
myTask.Subject = "{!Lead.LastName} 1 Month Follow-up"; 
myTask.status = "Not Started"
myTask.ActivityDate = "{!SUBSTITUTE(TEXT(TODAY()+30), "/", "-")}"
sforce.connection.create([myTask]); 
window.location.reload();

I would like to do something similar on our Opportunity and Case pages. I would expect that I could just swap out Lead.ID for Opportunity.ID and Lead.Lastname for the Opportunity.Subject and it should work.  Unfortunatly, I'm just getting a page refresh and no task.  Please help!
Hello,

I have written the following code and have done extensive sandbox testing.  I have several triggers that are very similar but go off of different fields in a contract.  I have a test written for lines 1-12 but can't seem to find the best methodology to writing from line 14 down.  Any help is appreciated.

Code:
trigger Contract_CreateTV_New on Contract (after update){
    System.debug('Contract_CreateTV_New triggered');
    
    Set<Id> activatedContractIds = new Set<Id>();

    for (Contract each : trigger.new) {
        if (each.Status == 'In Progress' && each.RecordTypeId == [Select Id From RecordType WHERE Name = 'Touchtown Contract'].Id && trigger.oldMap.get(each.Id).Status != 'In Progress')
        {
            System.debug('Contract ' + each.Id + ' transitioned into IN PROGRESS status.');
            activatedContractIds.add(each.Id);
            
            if (each.TV_Systems__c != null) {
                                
                for (Integer i = 0; i < each.TV_Systems__c; i++) {
                    Equipment__c newEQPT = new Equipment__c (
                        Account__c = each.AccountId,
                        Name = [Select Name From Account WHERE Id =: each.AccountId].Name,
                        RecordTypeId = [Select Id From RecordType WHERE Name = 'TV+'].Id,
                        Contract__c = each.Id,
                        Product_Type__c = 'TV+',
                        Warranty__c = 'Original (Lifestyle)'
                    );
                    
                    insert newEQPT;
                     List<ContractContactRole> contactRoles = [select ContractId, ContactId from ContractContactRole where Role = 'Install Contact' and ContractId in :activatedContractIds];
    
                    for (ContractContactRole eachRole : contactRoles) {Contract eachContract = trigger.newMap.get(eachRole.ContractId);
                    
                    Junction_Equipment_Contact__c newJunct = new Junction_Equipment_Contact__c(
                        Equipment__c = newEQPT.Id,
                        Contact__c = eachRole.ContactId
                    );
                    
                    insert newJunct;
                    
                    System.debug('Added DS: ' + newEQPT.Id);
                }
            }            
        }
    }
}
}

Test:
@isTest(seealldata=true)
private class TEST_EquipmentCreation {

    static testmethod void TEST_EquipmentCreation()
        
    {
        Account Acct = new Account();
        acct.Name = 'Test Account';
            insert Acct;
                
        Contact Contact = new Contact();
        Contact.FirstName = 'Mike';
        Contact.LastName = 'Test';
        Contact.AccountId = acct.Id;
        Contact.Email = 'test@test.com';
        insert Contact;
        
        Contract newContract = new Contract();
        newcontract.Name = 'test contract';
        newcontract.AccountId = acct.Id;
        newcontract.Status = 'draft';
        newcontract.StartDate = system.today();
        newcontract.RecordTypeId = [Select Id From RecordType WHERE Name = 'Touchtown Contract'].id;
        insert newcontract;
        
        ContractContactRole CCR = new contractContactRole();
        CCR.ContractId = newcontract.Id;
        CCR.ContactId = contact.Id;
        CCR.Role = 'Install Contact';
            insert CCR;
       

        newcontract = [SELECT Status FROM Contract WHERE ID=: newcontract.Id];
        newcontract.status = 'In Progress';
        newcontract.ActivatedDate = system.today();
        update newcontract;
        
     
        newcontract.TV_Systems__c=1;
        update newcontract;
        newcontract.DS_Systems__c=1;
        update newcontract;
       	newcontract.Calendars2__c=1;
        update newcontract;
        newcontract.TTRA_Licenses2__c=500;
        newcontract.Resident_Apps_Level__c = 'Premium';
        newcontract.Web_Shows__c =1;
        newcontract.TTRA_Licenses2__c =500;
        update newcontract;
        
           		for (Integer i = 0; i<newcontract.DS_Systems__c; i++) {
                    Equipment__c newEQPT = new Equipment__c (
                        Account__c = acct.id,
                        Name = acct.Name,
                        RecordTypeId = [Select Id From RecordType WHERE Name = 'Digital Signs'].Id,
                        Contract__c = newcontract.Id,
                        Product_Type__c = 'Digital Sign',
                        Warranty__c = 'Original (Lifestyle)'
                    );
                    
                    insert newEQPT;
                    
                    //List<ContractContactRole> contactRoles = [select ContractId, ContactId from ContractContactRole where Role = 'Install Contact'];
    
                    //for (ContractContactRole eachRole : contactRoles) {Contract eachContract = trigger.newMap.get(eachRole.ContractId);
                    
                    //Junction_Equipment_Contact__c newJunct = new Junction_Equipment_Contact__c(
                        //Equipment__c = newEQPT.Id,
                        //Contact__c = eachRole.ContactId
                    //);
                    
                    //insert newJunct;
                    
                   // System.debug('Added DS: ' + newEQPT.Id);
                 														}     
                //}
        
    } 
    

}

 
I've seen a lot of frustration around sending automated emails to various contact roles both in Contracts and Opportunities.  It seems like one of the leading soluions is to create a custom field and populate it via apex with the appropriate contact role.  But what about if you have multiple contact roles that fit the criteria?  For example, I may have multiple "Install Contacts" on a contract that I would want to get the same email.  How can I automate this?  Via a Trigger?

Thanks!
Hello,

Our business process demands automatic equipment record creation when a contract is activated.  When the contract is entered the user would enter numeric values for Product 1, Product 2 etc.  I want the flow to look-up those numeric values and create the number of new equipment records specified.  I.e Product1 =16 it would create 16 new equipment records under that account.  Additionally I would want information such as the Install Contact from the contracts Contact Role to be populated in all the equipment records.

Is an Apex trigger the best way to accomplish this?
Thanks!