• Edward Scott 5
  • NEWBIE
  • 40 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 11
    Questions
  • 19
    Replies
Hi,

I am trying to write my first test class. The trigger is on the case object and it makes sure that the case owner who is always initally the creator gets switched to the person who the case is for. The trigger updates the owner from a custom field called "Change Requested For". This trigger only works on a specific record type.The trigger also updates the case when a chaeckbox called Updated in Salesforce is cheked to close. I will post my trigger code and the test code below. Any help or advice is greatly appreciated.
 
trigger updateOwner on Case (before insert, before update) {

  for (Case theCase:trigger.new)
        if (theCase.RecordTypeId == '012L00000000r8s'){
         if (theCase.Change_Requested_For__c != theCase.OwnerId){
         theCase.OwnerId = theCase.Change_Requested_For__c;
        }
        
    for (Case c:trigger.new)
        if (c.RecordTypeId == '012L00000000r8s'){
        if (c.Quota_Updated_in_Salesforce__c == True)
        c.Status = 'Closed';
        
        }    
        
        }

}


 
trigger updateOwner on Case (before insert, before update) {

  for (Case theCase:trigger.new)
        if (theCase.RecordTypeId == '012L00000000r8s'){
         if (theCase.Change_Requested_For__c != theCase.OwnerId){
         theCase.OwnerId = theCase.Change_Requested_For__c;
        }
        
    for (Case c:trigger.new)
        if (c.RecordTypeId == '012L00000000r8s'){
        if (c.Quota_Updated_in_Salesforce__c == True)
        c.Status = 'Closed';
        
        }    
        
        }

}
@isTest
private class updateOwnerTest {
    static testMethod void testUpdateOwner(){
        //Set up the Case Record.
       Case caseObj = new Case();
       caseObj.RecordTypeId = '012L00000000r8s';     
        
        test.startTest();
            caseObj.OwnerId = '005C0000005itk8';
            caseobj.Change_Requested_For__c = '005C0000003NsNj';
            caseObj.Due_Date__c = system.today();
            caseObj.Quota_Updated_in_Salesforce__c = True;
        test.stopTest()  ;     
            
    }

}
 
Hi,

I am trying to write a trigger that will update a custom field that I created on the opportunity object called case status. It is a picklist field that I want to mirror the status picklist field on cases. I also created a lookup field on opportunities called related_case. The related_case field ties the two objects together. Right now my trigger works so that after i update the field on cases I then have to press edit and save on the opportunity page to get the field to update. What I would like to happen is have the opportunity page update on refresh after changing the case field. The trigger is currently built on the opportunity and I am thinking maybe it needs to be built on the case instead. 

I am attaching the code here and any help or advice is greatly appreciated.
 
trigger updateCaseStatus on Opportunity(After Update) {

      set<id> setid =new set<id>();
    
    for(Opportunity Opp: Trigger.new){
        if(Opp.Related_Case__c != null)
        {
        setid.add(Opp.Related_Case__c);
        }
     Case cas = [select id,Status from Case where Id in: setid];
     
              for(Opportunity Opps : trigger.new)
       {
              
          Opps.Case_Status__c= cas.Status;
          opps.Related_case__c = cas.id;

             }

   }
   }




Thanks,

Edward
I am new to writing apex code but I have successfully written a few triggers so any advice on this would be greatly appreciated.

I am creating a case process where when one of my sales rep's quota changes during the year our accounting department will create a case that will then be sent to the rep's manager and after they approve it sent to the rep to view the new quota. The only problem I can see with this process is a rep's quota is very sensitive information. So what I would like to do is one of two things. The first soultion that came to mind is write a trigger that will blank a field if the person viewing it isn't the rep that the change is for (I created a field named "Change For"). The second is creating a second field that is only visible to a few profiles that holds the reps quota and if the person viewing the case is the person that the change is for then it will copy the value from the quota field and if not its blank or 0.

I am not sure if either of these are possible or if there is a better way to do this. Again any advice is much appreciated.

Thanks,

Edward
Hi Community,

I am trying to write a trigger that will update a field when the value of another field goes up or down by 10%. So I set the initial value of the field equal to the second field but from that point I would like the first field to stay the same until the value of the second field increases or decreases by 10 percent. 

I was able to get the field to copy but right now it is updating for every value change of the second field. Any help would be appreciated. I am posting the code below. 
 
trigger updateMK on Brief__c (before update, before insert) {

    

     
     
     for (Brief__c  brf : trigger.new){
     
        if (brf.Media_Kit_Needed__c == null){
        brf.Media_Kit_Needed__c = integer.valueof(brf.List_Size_Updated_Monthly__c);
     }
     
        else if (brf.Media_Kit_Needed__c <= (.10 * integer.valueof(brf.List_Size_Updated_Monthly__c)) + integer.valueof(brf.List_Size_Updated_Monthly__c)){
        brf.Media_Kit_Needed__c = integer.valueof(brf.List_Size_Updated_Monthly__c);
        }
        
        else if (brf.Media_Kit_Needed__c >= (.10 * integer.valueof(brf.List_Size_Updated_Monthly__c)) + integer.valueof(brf.List_Size_Updated_Monthly__c)){
        brf.Media_Kit_Needed__c = integer.valueof(brf.List_Size_Updated_Monthly__c);
        }
        
        else{
        brf.Media_Kit_Needed__c = brf.Media_Kit_Needed__c;
        
        }

     
     }

}

 
Hi,

I am working on a trigger to update the lead status from "Not Contacted/New" to "Contacted once a task has been marked complete. The trigger is working fine on the Lead but is throwing an exception when the record is a Contact. It works fine if I am just adding a task manually but if I use Salesforce for outlook to log an email I get an error message back that says "(Undelivered): 554 The apex class Messaging.EmailToSalesforceHandler failed due to: System.UnexpectedException: common.exception.ApiException: updateLeadStatus: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.updateLeadStatus: line 6, column 1"

Below is the trigger itself. I was wondering if anyone could give me some suggestions on how to fix it.

Thanks,

Edward

trigger updateLeadStatus on Task(after insert, after update){
    
    Set<Id> leadIds = new Set<Id>();
    
    for(Task tsk : trigger.new){
        if(String.valueOf(tsk.WhoId).startsWith('00Q') && tsk.type != 'Other' && tsk.status == 'Completed'){
            leadIds.add(tsk.WhoId);
        }
    }
    
    List<Lead> updateLeadList = new List<Lead>();
    for(Lead ld : [Select status from Lead where Id in :leadIds]){
        if (ld.status == 'Not Contacted/New'){
        ld.status = 'Contacted'; //Use any other value as per your requirement
        updateLeadList.add(ld);
        }
    }
    
    if(updateLeadList.size() > 0){
        update updateLeadList;
    }
}
Hi,

I was just able to get this trigger to work yesterday with the help of the community. It updates a field on the opportunity object from the opportunity team member page. It works fine whe I create an opportunity or edit one but it doesn't work when I use data loader. I am new to apex code and I saw an article about first creating a set in order to get the trigger to be a bulk trigger but I am not sure how to do it.  Here is my code I was wondering if anyone could help me. 
Trigger updateAdOps on Opportunity (after update, after insert) {

  // Get Ad Ops team members & put in a map of <OpportunityId,AdOpsTeamMemberId>
  map<id,id> mOtms = new map<id,id>();
  list<OpportunityTeamMember> otms = new list<OpportunityTeamMember>([
      SELECT id,opportunityId,TeamMemberRole,UserId 
      FROM OpportunityTeamMember
      WHERE opportunityId IN :trigger.newmap.keyset()
        AND TeamMemberRole = 'Ad Ops'
      ]);

  For(OpportunityTeamMember otm :otms) {
    mOtms.put(otm.OpportunityId,otm.UserId);
  }

  // Re-aquire the Opportinities and Update them with the Ad Ops team member
  list<Opportunity> opportunitiesToUpdate = new list<opportunity>([SELECT id,Ad_Ops_Contact1__c FROM   Opportunity WHERE Id IN :trigger.newmap.keyset()]);
  for(Opportunity o :opportunitiesToUpdate) {
    if(mOtms.containsKey(o.id)){
      o.Ad_Ops__c = mOtms.get(o.id);
    }
  }

  if (util_utilities.alreadyExecuted != true) {
    util_utilities.alreadyExecuted = true;
    if(opportunitiesToUpdate.size()>0) {
      update opportunitiesToUpdate;
    }
  }

}

 
Hi Community,

I have been trying to write a trigger that will update a field called Ad Ops Contact that I created on the Opportunity page. My first attempt was to write a trigger on Opportunity Team Member that updates the opportunity with the name of the person who is the Ad Ops contact but I could only get it to work when I actually updated the opportunity team and not when the opportunity team is added to the opportunity (which happens when the opportunity is created). So basically that trigger on the opportunityteammember worked on update but not on insert. So my second attempt after reading this link about related lists (https://developer.salesforce.com/forums/ForumsMain?state=id#!/feedtype=SINGLE_QUESTION_SEARCH_RESULT&id=906F0000000943LIAQ) was to write it on the opportunity itself. 
trigger updateAdOps on Opportunity (after update, after insert) {

    //Using set to be able to work with setOPP and setOTM sets
    set<Id> setOpp = new set<Id>();
    set<Id> setOTM = new set<Id>();
    
       //Loop through opportunity team members and grab users who have the role of 'Ad Ops'
    for (Opportunity oppTeam : trigger.new) { 
        setOpp.add(oppTeam.Id);
    } 
    
    
     //Create Map
    list<OpportunityTeamMember> lstOTM = new list<OpportunityTeamMember>([SELECT Id, UserId, OpportunityId, User.Name FROM OpportunityTeamMember WHERE Id in :setOTM AND (TeamMemberRole = 'Ad Ops') ]);

    Map<Id,Opportunity> mapOpps = new map<Id, Opportunity>([SELECT Id, Ad_Ops_Contact1__c FROM Opportunity Where Id IN :setOpp ]) ;          
    Opportunity tempOpp;
    
    
        //Load Values 
        for(OpportunityTeamMember otm : lstOTM ){
        tempOpp = mapOpps.get(otm.user.name); // Opportunity Team Member Id
        tempOpp.Ad_Ops_Contact1__c = otm.user.name; //otm.user.name is from user.name in the create map
    
    }
    insert mapOpps.values();  

}

I am getting an error that says Insert can't be done on this opperation. 

Any help would be appreciated.

Thanks,

Edward
Hi Community,

I am trying to write a trigger that will alert me when a value has increased or decreased by 10%. So currently we have a field called subscriber number and it keeps track of the number of people who receive a publication. I would like to copy that value to another field and hold it until the value of the original field goes up or down by 10%. 

Thanks for your help,

Edward
Hi,

I am attempting to write a trigger. We have an oject called brief and on that object is a field named "List Size". The List Size keeps track of how many people are reading that brief. The number is updated monthly and if the number goes up by 10 percent or down by 10 percent we update it in our presentations. So I created an additional number field on the object that I would like to first copy the value of "LIst Size" and then only update that value when the original value of List Size goes up or down by 10 percent. 

Thanks for your help,

Edward
Hi,

I am trying to wirte a trigger that will pull the Ad ops team member from opportunity teams to the opportunity page. I found code in the forum that I was able to tweak and make work but it the trigger is on the opporunityteam so it only updates the field when I edit the opportunity team. Since our opportunity teams are inserted automatically when the opportunity is created this is a problem. I was wondering if anyone has already solved this problem. 

Thanks in advance for your help,

Edward
I am trying to write a trigger that will update a picklist field on the lead object. So when a lead has an activity added such as a call or email I would like for the lead status picklist to be updated from Not Contacted/New to Contacted. Thank for your help.
Hi,

I am trying to write a trigger that will update a custom field that I created on the opportunity object called case status. It is a picklist field that I want to mirror the status picklist field on cases. I also created a lookup field on opportunities called related_case. The related_case field ties the two objects together. Right now my trigger works so that after i update the field on cases I then have to press edit and save on the opportunity page to get the field to update. What I would like to happen is have the opportunity page update on refresh after changing the case field. The trigger is currently built on the opportunity and I am thinking maybe it needs to be built on the case instead. 

I am attaching the code here and any help or advice is greatly appreciated.
 
trigger updateCaseStatus on Opportunity(After Update) {

      set<id> setid =new set<id>();
    
    for(Opportunity Opp: Trigger.new){
        if(Opp.Related_Case__c != null)
        {
        setid.add(Opp.Related_Case__c);
        }
     Case cas = [select id,Status from Case where Id in: setid];
     
              for(Opportunity Opps : trigger.new)
       {
              
          Opps.Case_Status__c= cas.Status;
          opps.Related_case__c = cas.id;

             }

   }
   }




Thanks,

Edward
Hi Community,

I am trying to write a trigger that will update a field when the value of another field goes up or down by 10%. So I set the initial value of the field equal to the second field but from that point I would like the first field to stay the same until the value of the second field increases or decreases by 10 percent. 

I was able to get the field to copy but right now it is updating for every value change of the second field. Any help would be appreciated. I am posting the code below. 
 
trigger updateMK on Brief__c (before update, before insert) {

    

     
     
     for (Brief__c  brf : trigger.new){
     
        if (brf.Media_Kit_Needed__c == null){
        brf.Media_Kit_Needed__c = integer.valueof(brf.List_Size_Updated_Monthly__c);
     }
     
        else if (brf.Media_Kit_Needed__c <= (.10 * integer.valueof(brf.List_Size_Updated_Monthly__c)) + integer.valueof(brf.List_Size_Updated_Monthly__c)){
        brf.Media_Kit_Needed__c = integer.valueof(brf.List_Size_Updated_Monthly__c);
        }
        
        else if (brf.Media_Kit_Needed__c >= (.10 * integer.valueof(brf.List_Size_Updated_Monthly__c)) + integer.valueof(brf.List_Size_Updated_Monthly__c)){
        brf.Media_Kit_Needed__c = integer.valueof(brf.List_Size_Updated_Monthly__c);
        }
        
        else{
        brf.Media_Kit_Needed__c = brf.Media_Kit_Needed__c;
        
        }

     
     }

}

 
Hi,

I was just able to get this trigger to work yesterday with the help of the community. It updates a field on the opportunity object from the opportunity team member page. It works fine whe I create an opportunity or edit one but it doesn't work when I use data loader. I am new to apex code and I saw an article about first creating a set in order to get the trigger to be a bulk trigger but I am not sure how to do it.  Here is my code I was wondering if anyone could help me. 
Trigger updateAdOps on Opportunity (after update, after insert) {

  // Get Ad Ops team members & put in a map of <OpportunityId,AdOpsTeamMemberId>
  map<id,id> mOtms = new map<id,id>();
  list<OpportunityTeamMember> otms = new list<OpportunityTeamMember>([
      SELECT id,opportunityId,TeamMemberRole,UserId 
      FROM OpportunityTeamMember
      WHERE opportunityId IN :trigger.newmap.keyset()
        AND TeamMemberRole = 'Ad Ops'
      ]);

  For(OpportunityTeamMember otm :otms) {
    mOtms.put(otm.OpportunityId,otm.UserId);
  }

  // Re-aquire the Opportinities and Update them with the Ad Ops team member
  list<Opportunity> opportunitiesToUpdate = new list<opportunity>([SELECT id,Ad_Ops_Contact1__c FROM   Opportunity WHERE Id IN :trigger.newmap.keyset()]);
  for(Opportunity o :opportunitiesToUpdate) {
    if(mOtms.containsKey(o.id)){
      o.Ad_Ops__c = mOtms.get(o.id);
    }
  }

  if (util_utilities.alreadyExecuted != true) {
    util_utilities.alreadyExecuted = true;
    if(opportunitiesToUpdate.size()>0) {
      update opportunitiesToUpdate;
    }
  }

}

 
Hi Community,

I have been trying to write a trigger that will update a field called Ad Ops Contact that I created on the Opportunity page. My first attempt was to write a trigger on Opportunity Team Member that updates the opportunity with the name of the person who is the Ad Ops contact but I could only get it to work when I actually updated the opportunity team and not when the opportunity team is added to the opportunity (which happens when the opportunity is created). So basically that trigger on the opportunityteammember worked on update but not on insert. So my second attempt after reading this link about related lists (https://developer.salesforce.com/forums/ForumsMain?state=id#!/feedtype=SINGLE_QUESTION_SEARCH_RESULT&id=906F0000000943LIAQ) was to write it on the opportunity itself. 
trigger updateAdOps on Opportunity (after update, after insert) {

    //Using set to be able to work with setOPP and setOTM sets
    set<Id> setOpp = new set<Id>();
    set<Id> setOTM = new set<Id>();
    
       //Loop through opportunity team members and grab users who have the role of 'Ad Ops'
    for (Opportunity oppTeam : trigger.new) { 
        setOpp.add(oppTeam.Id);
    } 
    
    
     //Create Map
    list<OpportunityTeamMember> lstOTM = new list<OpportunityTeamMember>([SELECT Id, UserId, OpportunityId, User.Name FROM OpportunityTeamMember WHERE Id in :setOTM AND (TeamMemberRole = 'Ad Ops') ]);

    Map<Id,Opportunity> mapOpps = new map<Id, Opportunity>([SELECT Id, Ad_Ops_Contact1__c FROM Opportunity Where Id IN :setOpp ]) ;          
    Opportunity tempOpp;
    
    
        //Load Values 
        for(OpportunityTeamMember otm : lstOTM ){
        tempOpp = mapOpps.get(otm.user.name); // Opportunity Team Member Id
        tempOpp.Ad_Ops_Contact1__c = otm.user.name; //otm.user.name is from user.name in the create map
    
    }
    insert mapOpps.values();  

}

I am getting an error that says Insert can't be done on this opperation. 

Any help would be appreciated.

Thanks,

Edward
Hi Community,

I am trying to write a trigger that will alert me when a value has increased or decreased by 10%. So currently we have a field called subscriber number and it keeps track of the number of people who receive a publication. I would like to copy that value to another field and hold it until the value of the original field goes up or down by 10%. 

Thanks for your help,

Edward
HI,
I have a requirement, Where I need to create a report to retreive case records for particluar record type say 'C' and It will have a account say 'A'. So based on the case close date value, I need to retieve list of new cases created after the case close date with record type 'C' that is related to Account 'A'.
  • November 05, 2015
  • Like
  • 0
Hi,

I am trying to wirte a trigger that will pull the Ad ops team member from opportunity teams to the opportunity page. I found code in the forum that I was able to tweak and make work but it the trigger is on the opporunityteam so it only updates the field when I edit the opportunity team. Since our opportunity teams are inserted automatically when the opportunity is created this is a problem. I was wondering if anyone has already solved this problem. 

Thanks in advance for your help,

Edward
I am trying to write a trigger that will update a picklist field on the lead object. So when a lead has an activity added such as a call or email I would like for the lead status picklist to be updated from Not Contacted/New to Contacted. Thank for your help.