• DustinLH
  • NEWBIE
  • 30 Points
  • Member since 2010

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 10
    Questions
  • 34
    Replies

I am getting an error with Too Many DML Statements for a small trigger that I wrote. I know that it has to do with the insert being inside the loop, but I cannot figure out how to correctly write this. I am hoping someone can take a look since it is so small.

 

trigger ReferralChatterFollow on Referral__c (after insert) {

    for(Referral__c r: Trigger.New) {
                    EntitySubscription follow = new EntitySubscription (
                        parentId = r.id,
                        subscriberId = UserInfo.getUserId());
                    insert follow;
    }
}

 

I have the following trigger that takes the user that is in the Assigned field and makes them the record owner once the record is created. Also, if the owner is changed on the record, it will update the Assigned field. This is working perfectly for me, but I am having an issue with code coverage for my other triggers, etc. 

 

I would like to tweek the trigger to say that if the Assigend field is blank, then make the record owner the current user (i.e. normal behavior) and populate the Assigned field with the current user (or maybe it would be easier to populate the Assigned field with the current user so that it assigns ownership that way). This way, I will not get all of these exceptions thrown. I still want the 2nd part of the trigger to work (when owner is changed update the Assigned). 

 

Can someone please help me tweek this trigger?

 

trigger ReferralAssignOwner on Referral__c(before insert, before update) {
  for(Referral__c r:Trigger.new)                                         // For each record
    if(Trigger.isInsert)                                                 // If we're inserting
      r.OwnerId = r.Assigned__c;                                         // Assign ownership from Assigned__c
    else                                                                 // Otherwise (an update)
      if(r.OwnerId != Trigger.oldMap.get(r.id).OwnerId)                  // If ownership has changed
        r.Assigned__c = r.OwnerId;                                       // Place the new owner in Assigned__c
      else                                                               // Otherwise (owner not changed, is an update)
        if(r.Assigned__c != Trigger.oldMap.get(r.id).Assigned__c)        // If the Assigned__c field changed
          r.OwnerId = r.Assigned__c;                                     // Assigned ownership from Assigned__c
}

 

I am getting a system.assertEquals error on the following test class. Error:

System.AssertException: Assertion Failed: Expected: null, Actual: 00580000001k6g1AAAClass.testReferralAssignOwner.test: line 11, column 1

 Test Class:

@isTest
private class testReferralAssignOwner {
  static testMethod void test() {
    List<User> testUsers = [select id from user where isactive = true limit 2];
    Account a = new Account(Name='test');
    insert a;
    List<Referral__c> refs = new List<Referral__c>();
    refs.add(new Referral__c(Client_Name__c=a.id,Phone__c='12345',Assigned__c=testUsers[0].Id));
    refs.add(new Referral__c(Client_name__c=a.id,Phone__c='12345',Assigned__c=testUsers[1].Id));
    insert refs;
    system.assertEquals(refs[0].OwnerId,testUsers[0].Id); // OwnerId should equal Assigned__c;
    system.assertEquals(refs[1].OwnerId,testUsers[1].Id); // OwnerId should equal Assigned__c;
    refs[0].OwnerId = testUsers[1].Id;
    refs[1].Assigned__c = testUsers[0].Id;
    update refs;
    system.assertEquals(refs[0].Assigned__c,testUsers[1].Id); // Assigned__c should equal OwnerId now
    system.assertEquals(refs[1].OwnerId,testUsers[0].Id); // OwnerId should equal Assigned__c now
  }  
}

 It says that it is expecitng null and getting a user ID instead. It should be expecting the user ID, so I am not sure what the problem is. Any help is greatly appreciated!

Does anyone have any code that takes a custom user lookup field and changes the owner of the record to match and vice versa (changing owner updates user lookup field)?

 

I have a use case where I need to force the user to identify the owner of the record during the creation of the record and not rely on their memory to change the owner after the fact.

I have a custom button that executes OnClick Javascript and it works fine in IE and in Firefox, but in Chrome I get the following error message:

 

A problem with the OnClick JavaScript for this button or link was encountered:

 

{faultcode:'soapenv.Client', faultstring:'Missing entity type information. sObject requires a separate 'type' field be sent.',}

 

It is odd to me because I do not really understand the message and it works in the other two browsers, so it shouldn't be a mapping problem. The code takes a custom object (called referral) and maps fields from that to a new Lead that it creates. It also updates the referral status and ties the Lead to it. The code is below. Can anyone help me figure out the issue? Thanks!

 

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

    if ("{!Referral__c.Status__c}" == "Converted")
    {
        alert("Referral {!Referral__c.Name} has already been converted to a Lead. To view the Lead, please see the Related Lead field on the Referral.");
    } else
    {
        var Lead = new sforce.SObject("Lead");
        Lead.Referrer_Branch__c = "{!JSENCODE(Referral__c.Branch_Referred_By__c)}";
        Lead.Business_or_Individual__c = "{!Referral__c.Business_Individual__c}";
        Lead.Client_Type__c = "Prospect";
        Lead.Company = "{!JSENCODE(Referral__c.Company_Name__c)}";
        Lead.Referrer_Department__c = "{!JSENCODE(Referral__c.Department_Referred_By__c)}";
        Lead.Email = "{!JSENCODE(Referral__c.Referral_Email__c)}";
        Lead.FirstName = "{!JSENCODE(Referral__c.Referral_First_Name__c)}";
        Lead.LastName = "{!JSENCODE(Referral__c.Referral_Last_Name__c)}";
        Lead.LeadSource = "Referral";
        Lead.Status = "Open - Not Contacted";
        Lead.Phone = "{!JSENCODE(Referral__c.Phone__c)}";
        Lead.RecordTypeId = "01280000000EtDm";
        Lead.Referral_Address__c = "{!JSENCODE(Referral__c.Referral_Address__c)}";
        Lead.Referral_Comments__c = "{!JSENCODE(Referral__c.Comments__c)}";
        Lead.Referral_Company__c = "{!JSENCODE(Referral__c.Company_Name__c)}";
        Lead.Referral_Contact_Method__c = "{!Referral__c.Contact_Method__c}";
        Lead.Referral_Creator__c = "{!JSENCODE(Referral__c.Record_Creator__c)}";
        Lead.Referral_Email__c = "{!JSENCODE(Referral__c.Referral_Email__c)}";
        Lead.Referral_First_Name__c = "{!JSENCODE(Referral__c.Referral_First_Name__c)}";
        Lead.Referral_ID__c = "{!JSENCODE(Referral__c.Id)}";
        Lead.Referral_Last_Name__c = "{!JSENCODE(Referral__c.Referral_Last_Name__c)}";
        Lead.Referral_Phone__c = "{!JSENCODE(Referral__c.Phone__c)}";
        Lead.Referral_Preferred_Time__c = "{!Referral__c.Preferred_Time__c}";
        Lead.Referral_Product__c = "{!Referral__c.Product__c}";
        Lead.Referral_Record_Type__c = "{!Referral__c.RecordType}";
        Lead.Secondary_Name__c = "{!JSENCODE(Referral__c.Secondary_Name__c)}";
        Lead.Secondary_Relationship__c = "{!Referral__c.Secondary_Relationship__c}";
        Lead.What_brought_Referral_into_the_bank__c = "{!JSENCODE(Referral__c.What_brought_Referral_into_the_bank__c)}";

        var refResult = sforce.connection.create([Lead]);

        if (refResult[0].getBoolean("success")) {
            // now update the referral to reflect that it was successfully updated
            var currentReferral = new sforce.SObject("Referral__c");
            currentReferral.Id = "{!Referral__c.Id}";
            currentReferral.Status__c = "Converted";
            currentReferral.Related_Lead__c = refResult[0].id;
            var result = sforce.connection.update([currentReferral]);
            
            // redirect to newly created lead
            var redirectLocation = refResult[0].id;
            window.location = redirectLocation;
    } else {
        alert("Failed to convert Referral. " + refResult[0]);
    }
}

 

I modified the code from a trigger I found on here and created the following trigger. I deployed it to production via a change set, but am only getting 63% coverage. Can someone assist with a test class? I am not familiar enough with apex yet to figure this out.  Thanks!

 

trigger ConvertProspectReferral_Trigger on Lead (after update) {

  Map<Id, Lead> leadMap = new Map<Id,Lead>();
  Lead parent;
 
  for (Integer i = 0; i < Trigger.new.size(); i++){
    if (Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false) {
      leadMap.put( Trigger.new[i].Id, Trigger.new[i]);
    }
  }
   
  if( leadMap.size() > 0 ) {
      Set<Id> leadIds = leadMap.keySet();
      List<Referral__c> allChildren =
        [select Id, Related_Opportunity__c, Client_Name__c, Related_Lead__c, RecordTypeId from Referral__c where Related_Lead__c in :leadIds];      
 
 System.debug(allChildren);
   
      for ( Referral__c child : allChildren ) {
        if ( leadMap.containsKey( child.Related_Lead__c ) ) {
           // lookup the parent lead
           parent = leadMap.get( child.Related_Lead__c );
           // update the fields on the child object
           child.related_opportunity__c = parent.ConvertedOpportunityId;
           child.client_name__c = parent.ConvertedAccountId;
           child.recordtypeid = '01280000000ErjI';
        }
      }

System.debug(allChildren);

    //try {
      update allChildren;
   // } catch( Exception e ) {
         // could put something here to notify on error
         // otherwise it fails silently
   // }
     
  }
}

 

I have searched high and low for someone who has done this and have come up empty handed. I find it hard to believe this is not a common issue.

 

As you know, if an Opportunity is added to a Contact, the Contact is automatically added as a Contact Role on that Opportunity. It does not make them the Primary Contact Role or define the Role, but it does add them.

 

If an Opportunity is added to a Person Account, the Contact/Person Account is not automatically added as a Contact Role on the Opportunity. When the Opportunity is created there are no Contacts listed.

 

What I would like to do is to have a trigger that automatically adds the Person Account as a Contact to the Contact Roles when an Opportunity is created under them. I would like for them to be made Primary and a role of Decision Maker. 

 

It seems to me that adding Contact Roles automatically would be more common, but I guess not. Has anyone found a solution to this that they can share? Thank you everyone!

 

Dusty

We have enabled delegated authentication single sign-on for our org. We have created the web service call and are able to successfully login automatically using that URL and checking against our AD credentials. Everything works great, until the timeout occurs. We have our timeout session set to 30 minutes, as we are a financial institution with strict security policies. Here is the issue in detail:

Initially, we set the logout URL to be the SSO web service, with the intent of the logout automatically logging the user back in. The 30 minute timeout warning dialog displays. If you select Continue Working, you are fine and your session continues as normal. If you missed the dialog, the session would automatically re-login in the same window that the dialog warning was in. This is problematic as the user now has two windows open, the original SFDC session in their browser and then the renewed session in the smaller browser window. This dialog window did not have a toolbar or header on it, since it was just a warning, so the new session also does not have a header or toolbar, rendering it useless for our needs.

We tried redirecting the logout URL to a new page that requires interaction from the user, but it still causes problems with that new window. So, I tried disabling the timeout warning dialog box from session settings, but then it does not re-authenticate the user at all. After 30 minutes, their session is expired, they are not warned and when they go to work on something they are redirected the the login URL and not automatically logged back in.

Has anyone had any luck with this? We want the user to stay logged in all day through single sign-on or be looped back into SSO automatically. Since SFDC does not have separate timeout settings for in network vs out of network we are lost. Any help or suggestions would be greatly appreciated.

All,

 

We currently have a trigger written that is pretty simple. It is designed to update the Status field of a custom object called Referral. We have a custom button that is designed to convert a Referral record to a Lead record. When this occurs, the Referral ID is copied to the Lead. The trigger is designed to mark the Referral record Status field "Converted" when a Lead is created with that Referral ID on it.

 

This works well, but it allows for unlimited conversion of Referrals to Leads. I want to add something to the trigger code that will prevent the Lead from being created if the Referral Status is already "Converted". This way, when a user tries to convert a Referral to a Lead, they will get an error or something that prevents them from completing the conversion.

 

Is something like this possible? I am pasting our current code below:

 

trigger LeadToReferralStatusUpdate on Lead (after insert, after update)
{
  set<Id> rIds = new set<Id>();
 
  for(Lead l:trigger.new)
  {
    if(l.Referral_ID__c != null)
    {
      rIds.add(l.Referral_ID__c);
    }
  }
 
  Referral__c[] updateReferrals = new Referral__c[0];
 
  for(Referral__c r:[select id, status__c from Referral__c where Id in:rIds])
  {
    r.status__c = 'Converted';
    updateReferrals.add(r);
  }
 
  update updateReferrals;

}

I know that it can be annoying to see the questions from those of us who are new to this, but any help is appreciated. I am trying to accomplish, what I believe to be a rather simple apex trigger. I have spent over 9 hours today alone trying to locate an example that is similar to mine and have found nothing remotely close.

 

I am trying to accomplish a field update on a custom object triggered by an update on a record from the same object. We have a custom object called deposit accounts. I want the trigger to fire when a deposit account record's field of status is changed to "Accepted". Once that happens, I want the trigger to search all deposit accounts for ones that have a matching name and type field and update the status on those records as well.

 

I thought this would be simple since I am not going cross object and am only trying to update 1 field on multiple records within this same object. I have tried writing this at least 10 times and fail at each corner. I am under a time crunch at work and any help that you provide will be awesome!

I am getting an error with Too Many DML Statements for a small trigger that I wrote. I know that it has to do with the insert being inside the loop, but I cannot figure out how to correctly write this. I am hoping someone can take a look since it is so small.

 

trigger ReferralChatterFollow on Referral__c (after insert) {

    for(Referral__c r: Trigger.New) {
                    EntitySubscription follow = new EntitySubscription (
                        parentId = r.id,
                        subscriberId = UserInfo.getUserId());
                    insert follow;
    }
}

 

I have the following trigger that takes the user that is in the Assigned field and makes them the record owner once the record is created. Also, if the owner is changed on the record, it will update the Assigned field. This is working perfectly for me, but I am having an issue with code coverage for my other triggers, etc. 

 

I would like to tweek the trigger to say that if the Assigend field is blank, then make the record owner the current user (i.e. normal behavior) and populate the Assigned field with the current user (or maybe it would be easier to populate the Assigned field with the current user so that it assigns ownership that way). This way, I will not get all of these exceptions thrown. I still want the 2nd part of the trigger to work (when owner is changed update the Assigned). 

 

Can someone please help me tweek this trigger?

 

trigger ReferralAssignOwner on Referral__c(before insert, before update) {
  for(Referral__c r:Trigger.new)                                         // For each record
    if(Trigger.isInsert)                                                 // If we're inserting
      r.OwnerId = r.Assigned__c;                                         // Assign ownership from Assigned__c
    else                                                                 // Otherwise (an update)
      if(r.OwnerId != Trigger.oldMap.get(r.id).OwnerId)                  // If ownership has changed
        r.Assigned__c = r.OwnerId;                                       // Place the new owner in Assigned__c
      else                                                               // Otherwise (owner not changed, is an update)
        if(r.Assigned__c != Trigger.oldMap.get(r.id).Assigned__c)        // If the Assigned__c field changed
          r.OwnerId = r.Assigned__c;                                     // Assigned ownership from Assigned__c
}

 

I am getting a system.assertEquals error on the following test class. Error:

System.AssertException: Assertion Failed: Expected: null, Actual: 00580000001k6g1AAAClass.testReferralAssignOwner.test: line 11, column 1

 Test Class:

@isTest
private class testReferralAssignOwner {
  static testMethod void test() {
    List<User> testUsers = [select id from user where isactive = true limit 2];
    Account a = new Account(Name='test');
    insert a;
    List<Referral__c> refs = new List<Referral__c>();
    refs.add(new Referral__c(Client_Name__c=a.id,Phone__c='12345',Assigned__c=testUsers[0].Id));
    refs.add(new Referral__c(Client_name__c=a.id,Phone__c='12345',Assigned__c=testUsers[1].Id));
    insert refs;
    system.assertEquals(refs[0].OwnerId,testUsers[0].Id); // OwnerId should equal Assigned__c;
    system.assertEquals(refs[1].OwnerId,testUsers[1].Id); // OwnerId should equal Assigned__c;
    refs[0].OwnerId = testUsers[1].Id;
    refs[1].Assigned__c = testUsers[0].Id;
    update refs;
    system.assertEquals(refs[0].Assigned__c,testUsers[1].Id); // Assigned__c should equal OwnerId now
    system.assertEquals(refs[1].OwnerId,testUsers[0].Id); // OwnerId should equal Assigned__c now
  }  
}

 It says that it is expecitng null and getting a user ID instead. It should be expecting the user ID, so I am not sure what the problem is. Any help is greatly appreciated!

Does anyone have any code that takes a custom user lookup field and changes the owner of the record to match and vice versa (changing owner updates user lookup field)?

 

I have a use case where I need to force the user to identify the owner of the record during the creation of the record and not rely on their memory to change the owner after the fact.

I modified the code from a trigger I found on here and created the following trigger. I deployed it to production via a change set, but am only getting 63% coverage. Can someone assist with a test class? I am not familiar enough with apex yet to figure this out.  Thanks!

 

trigger ConvertProspectReferral_Trigger on Lead (after update) {

  Map<Id, Lead> leadMap = new Map<Id,Lead>();
  Lead parent;
 
  for (Integer i = 0; i < Trigger.new.size(); i++){
    if (Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false) {
      leadMap.put( Trigger.new[i].Id, Trigger.new[i]);
    }
  }
   
  if( leadMap.size() > 0 ) {
      Set<Id> leadIds = leadMap.keySet();
      List<Referral__c> allChildren =
        [select Id, Related_Opportunity__c, Client_Name__c, Related_Lead__c, RecordTypeId from Referral__c where Related_Lead__c in :leadIds];      
 
 System.debug(allChildren);
   
      for ( Referral__c child : allChildren ) {
        if ( leadMap.containsKey( child.Related_Lead__c ) ) {
           // lookup the parent lead
           parent = leadMap.get( child.Related_Lead__c );
           // update the fields on the child object
           child.related_opportunity__c = parent.ConvertedOpportunityId;
           child.client_name__c = parent.ConvertedAccountId;
           child.recordtypeid = '01280000000ErjI';
        }
      }

System.debug(allChildren);

    //try {
      update allChildren;
   // } catch( Exception e ) {
         // could put something here to notify on error
         // otherwise it fails silently
   // }
     
  }
}