• CaitlinGM
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 12
    Questions
  • 18
    Replies

I need to write over an existing, active trigger in production with a new, updated version, and do the same for a test class. What is the best way to do this? If I create a change set with the new version of the trigger and test and deploy them while the first version is still active, will it write over the first version? Or do I need to remove the old versions first?

 

I'm more comfortable using change sets, but can use Eclipse if needed to do this.

 

Thanks.

I need to send an email to a contact associated with a record (through a lookup field) one week before a date in the record's "End Date" field. 

 

I can't use workflow for this because the email doesn't go to a set contact, but rather it depends on the field contents. 

 

I can write an apex trigger to send the email, but how do I incorporate the time-dependent aspect? 

 

Would it work if I set a workflow rule to check a checkbox field a week before the deadline, and write the trigger to be an "on update" trigger and say that the field must be checked? Would the workflow-induced field update set off the trigger? 

 

I'm trying to have a field on my master object (Opportunity, field is called AFPAK_Student_Names__c) reflect the a field on all detail objects (Student_Assignment__c) meeting a certain criteria (Student_Assignment__c.status = Active) at any given time. Kind of like a text roll-up summary field. 

 

This trigger works just fine to add student names to the field when I add new Student Assignments with Status Active. However, what if I delete one or change the status? I want the field to reflect the change. 

 

In that case, is there a way to use Apex to test if the text field contains certain text (Student Name from the now non-active Student Assignment) and then remove just that? 

 

trigger StudentAssignmentClassUpdate on Student_Assignment__c (after insert) { 

if (trigger.isInsert){
Map<Id,Id> AssignmentToClassMap = new Map<Id,Id>();
for(Student_Assignment__c A : trigger.new)
           AssignmentToClassMap.put(A.Class__c,A.Id);
 
List<Opportunity> OppsToUpdate = new List<Opportunity>();

      
    for (Opportunity TestOpp: [SELECT Id, AFPAK_Student_Names__c FROM Opportunity WHERE Id IN:  AssignmentToClassMap.keySet()])
    {
        Id TestOppId = AssignmentToClassMap.get(TestOpp.Id);
        Student_Assignment__c StudAss = trigger.newMap.get(TestOppId);
        
               
//CASE 1: if inserting a Student Assignment with status Active, add student name to AFPAK student names field        
if (StudAss.status__c=='Active'){
TestOpp.AFPAK_Student_Names__c =TestOpp.AFPAK_Student_Names__c+' '+[SELECT Name From Contact Where Id = :StudAss.Student_Name__c limit 1].Name;
OppsToUpdate.add(TestOpp);
}
}
if(OppsToUpdate != null && !OppsToUpdate.isEmpty())
Database.update(OppsToUpdate);
}
}

 

I have a complex trigger on Opportunities that is behaving exactly as I would like, but my test coverage is only at 88%. The trigger is after insert, after update, and after delete, and it's the after delete portion of the code that the test is not affecting. Here is a shortened version of the trigger showing just the after delete case. How would I go about testing this portion? I'm getting confused since it seems to be more complex than the after insert and after update cases. I tried to just create an opportunity and the related contact and then delete the opportunity and update the contact in my test, but I must need to do something else as well. Thanks for any insight. 

 

In a nutshell, what the trigger does is:  a contact is related to an opportunity by a lookup field on opportunity. When the opportunity is deleted, if its stage is Active, it decrements a counter field on the related contact (instructor). 

 

trigger InsertUpdateDelete on Opportunity (after insert, after update, after delete) {

if(trigger.isDelete){
  {
 Map<Id,Id> instructorsToOppsMap3 = new Map<Id,Id>();
 for(Opportunity A : system.trigger.old)
 	             instructorsToOppsMap3.put(A.InstructorName__c,A.Id);
	   
List<Contact> contactsToUpdate3 = new List<Contact>();
 	        
for (Contact Instructor: [SELECT Id,Active_Count__c FROM Contact WHERE Id IN:  instructorsToOppsMap3.keySet()])
    {
	          Id oppId = instructorsToOppsMap3.get(Instructor.Id);
 	          Opportunity opp = system.trigger.oldMap.get(oppId);
 	          
	        if (opp.StageName=='Active'){
 	              Instructor.Active_Count__c=Instructor.Active_Count__c - 1;
 	  
 	               contactsToUpdate3.add(instructor);
	         }
	  if(contactsToUpdate3 != null && !contactsToUpdate3.isEmpty())
 	          Database.update(contactsToUpdate3);
 	  }
 	  } 

 

Is it possible to use a custom button to create a new record? I'd like to put the button on a custom object (Task Order__c) and have it create a new Opportunity, pulling information from the Task Order into the Opportunity fields. I would also like to be able to specify the record type of the new Opportunity. I have cobbled this together based on some examples I found of other buttons. Am I on the right track? Thanks in advance.

 

Here is what I am trying:

 

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")}
var newopportunity= new sforce.SObject("Opportunity");
newopportunity.Name = "my new opportunity";
newopportunity.Account="{!Task_Order__c.Account__c}";
newopportunity.StageName="Active";
newopportunity.CloseDate=new Date();
newopportunity.Student_Name__c="TBD";
var result = sforce.connection.create([newopportunity]);
window.location.reload();

 

Sorry if this is a really basic question, but: I have a trigger, I wrote a test for it, and it is displaying 100% coverage in my Sandbox. I want to deploy it to production, but my overall average coverage is only 72%. I can't deploy unless everything is at 75%, right? The problem is, the code without sufficient coverage is part of managed packages--installed apps. Is it really the case that I have to fix or get rid of other people's mistakes in order to be able to deploy my trigger?

 

I also just downloaded Eclipse with the Force.com IDE. Is that a better way to go to deploy my trigger?

 

Thanks for the help.  

I received some helpful feedback here to write a trigger on opportunity to update a field on a related contact, linked through a lookup field. I now understand how to do a simple field update on a picklist field on a related object.

 

I now need to write a revised version to actually increment or decrement a counter field (Active_Count__c) on the related contact. If Opp Stage = Active, Active_Count__c needs to be the previous value + 1. If Opp Stage is not Active, Active_Count__c needs to be the previous value -1. I'm not sure how to express the previous value or the addition/subtraction. I have question marks at those places in the code. 

 

Here is what I have so far: 

 

trigger OpportunityRelatedContact2 on Opportunity (after insert, after update) { 
{
Map<Id,Id> instructorsToOppsMap = new Map<Id,Id>();
for(Opportunity A : trigger.new)
           instructorsToOppsMap.put(A.InstructorName__c,A.Id);
 
List<Contact> contactsToUpdate = new List<Contact>{};
      
    for (Contact Instructor: [SELECT Id,Active_Count__c FROM Contact WHERE Id IN:  instructorsToOppsMap.keySet()])
    {
        Id oppId = instructorsToOppsMap.get(Instructor.Id);
        Opportunity opp = trigger.newMap.get(oppId);
            if (opp.StageName=='Active'){
            Instructor.Active_Count__c= ???Active Count value + 1 ;
       }else {
       Instructor.Active_Count__c= ???Active Count value - 1;
             contactsToUpdate.add(instructor);
         }
}
if(contactsToUpdate != null && !contactsToUpdate.isEmpty())
        Database.update(contactsToUpdate);
}
}

 

Thanks for the input!

 

I posted a related question a little while back and got some good ideas, but I'm still struggling with writing my first trigger. It should be simple enough: 

 

Opportunity has a Contact related through a lookup field on Opportunity. When Opportunity stage is Active, I want related contact status (Instructor_Status__c; it's a picklist) to change to Employed. 

When Opportunity Stage is not Active, I want related contact status to change to Available. 

 

If it matters, Opportunity:Contact is One:Many. 

 

I have so far: 

 

trigger OpportunityRelatedContact on Opportunity (after insert, after update) { 
{
list<Contact> Listofinstructors = new list<Contact>();

for (Opportunity A : trigger.new)
{

Contact instructor = new Contact(Id = A.InstructorName__c);
listofinstructors.add(Instructor);

if(A.StageName=='Active'){
instructor.Instructor_status__c='Employed';}
}
}
}

 

It's obviously not there yet, and I don't know if I have the right approach. Any help is appreciated. 

Hi, 

 

I'm new to Apex, and want to create what I think is a pretty simple cross-object field update trigger. 

 

An Opportunity has an Instructor Name lookup to a Contact record. When I change the opportunity record to Active, I want a custom field to update the instructor employment status to Employed on the Contact record. Likewise, when Opportunity stage moves to Ended, I want Contact status to go to Available. 

 

Any ideas on how I can accomplish this? Thanks very much for your input.