• apsulli
  • NEWBIE
  • 0 Points
  • Member since 2013

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

This should be a relatively simple request I think, but I can't find another example that has the specifics I need.

 

I would like to update one object based on changes made to another object where one string is equivalent.  In this case, I have a custom object (Customobj__c) that has a trigger on it to update a case with fields from that custom object based on a field that they both have in common.  One of the fields I want to update is a multi-select picklist, too (Event_Types__c).  The primary weirdness I am encountering is that the multi-select picklist doesn't always update when I save the Custom Object record, but I also suspect my code isn't laid out efficiently.  Can anyone help?

 

Here's the code:

 

trigger OnCustobjCaseFields on Customobj__c (before insert, before update)
{
String objSharedField; String objRecordId; String objAccount; String objEventTypes; for(Customobj__c obj: Trigger.new) { if (obj.Shared_Field__c != Null) { objSharedField = obj.Shared_Field__c; objRecordId = obj.Id; objAccount = obj.Account__c; objEventTypes = obj.Event_Types__c; } } List<Case> cases = new List<Case>([SELECT Id, Shared_Field__c, AccountId, Expected_Event_Types__c FROM Case WHERE Shared_Field__c = :objSharedField]); List<Case> caseUpdateList = new List<Case>(); for(Case c: cases) { c.AccountId = objAccount; c.App__c = objRecordId; c.Expected_Event_Types__c = objEventTypes; caseUpdateList.add(c); } if(caseUpdateList.size() > 0) { update caseUpdateList; } }

 

  • September 30, 2013
  • Like
  • 0

I'm trying to write a trigger that fills in a custom lookup field on a case by looking for a custom object with an exact match in a text field.

 

Basically, what I would like to do is when the custom object (custobj) is created or saved, and the text field (Package ID) has a value, find all cases with the same value in an equivalent custom field, and add the custom object to a lookup field on the case.

 

All attempts at writing this run into the same issue which is how to form the SOQL query to pull the related cases to update.  The following query is obviously totally wrong, but hopefully it highlights where my problem is.  What I think I need to do is pull all cases that have the Package_ID__c field which matches a Package_ID__c field on the custom object.  Can anyone help clarify how to do this?

 

List<Case> cases = new List<Case>([SELECT Id, Package_ID__c FROM Case WHERE Package_ID__c = custobj.Package_ID__c);

Thanks!!

  • September 13, 2013
  • Like
  • 0

How do I write a test class to check whether a new Opportunity Contact Role was created from a custom field based off of an Opportunity insert or update?

 

I've written a Trigger (mostly repurposed code) to create an Opportunity Contact Role when an Opportunity is created or updated with a Primary Contact (custom Contact Lookup field).  I'm not sure how to write a test class for it, though.  The trigger seems to work well enough.

 

Here's what it does: If a new Opp is created with a Primary Contact (required field), a new OCR is created as a Primary Contact and with a Decision Maker Role.  If the Primary Contact is changed, the old OCR is deleted, then the recreated as an OCR with the Influencer Role, while the new Primary Contact is created as the Primary with a Decision Maker Role.  If the insert fails, a friendly error message is thrown.

 

trigger OnOpportunityPrimaryContactRole on Opportunity (after insert, after update) {    
    
    List<OpportunityContactRole> newCRList = new List<OpportunityContactRole>();
    List<OpportunityContactRole> oldCRList = new List<OpportunityContactRole>();
    Set<Id> OppId = new Set<Id>();
    Set<Id> ContactId = new Set<Id>();  
    
    for(Opportunity o: Trigger.new)
    {
        //Checks if the Opportunity is being inserted
        if(Trigger.isInsert)
        {
            if(o.Primary_Contact__c != null)
            {
                //Creates the new OCR
                newCRList.add(new OpportunityContactRole (ContactId=o.Primary_Contact__c, OpportunityId=o.Id, Role='Decision Maker',IsPrimary=TRUE));                                                                       
            }
        }
        else if(o.Primary_Contact__c != null && Trigger.oldMap.get(o.Id).Primary_Contact__c != null)
            	{
                	//Gets the Contact and Opportunity Id from the prior values and adds to this set              
                	Opportunity oldOppObj=Trigger.oldMap.get(o.Id);
                    OppId.add(OldoppObj.id);
                	ContactId.add(oldOppObj.Primary_Contact__c);
                    	//Selects old OCRs
        				if (OppId.size()>0) oldCRList=[Select Id from OpportunityContactRole where ContactId in : ContactId  and OpportunityId in : OppId];
      
        				//Deletes old OCRs
       					if (oldCRList.size()>0) delete oldCRList;
                    
                		if(oldOppObj.Primary_Contact__c != o.Primary_Contact__c)
                    	{
                        	newCRList.add(new OpportunityContactRole (ContactId=o.Primary_Contact__c, OpportunityId=o.Id, Role='Decision Maker',IsPrimary=TRUE));
                            newCRList.add(new OpportunityContactRole (ContactId=oldOppObj.Primary_Contact__c, OpportunityId=o.Id, Role='Influencer',IsPrimary=False));
                    	}
                }      
    }  
    try
    {
        //Inserts new OCRs
        if(newCRList.size()>0) insert newCRList;
    }    
    catch(Exception e)
    {
        System.debug(e);
        trigger.new[0].addError('Uh Oh...  A technical error has occurred creating the Opportunity Contact Role. Please email the SFDC admin or try again later.');
    }
}

 

 

Hi - the issue I am having is that when using validation on a junction object, it looks like everything should be right, but just doesn't work at all.

 

For the purposes of this, the primary objects I'm working with are a custom promotional event object and the standard account object.

 

The logic is that I want to check if:

 

1. On the promotional event, the Event State (custom picklist field) is "NY"

2. On the account, the account state is one of a list of values

 

If the criteria is met, an error should be returned

 

If anyone can help or suggest a better way, I'm all ears!

 

Here's my validation formula:

 

AND(ISPICKVAL(Promotional_Event__r.Event_State__c,"NY"),Account__r.BillingState = "AK", Account__r.BillingState = "AZ",Account__r.BillingState = "AR",Account__r.BillingState = "CA",Account__r.BillingState = "CO",Account__r.BillingState = "HI",Account__r.BillingState = "ID",Account__r.BillingState = "IA",Account__r.BillingState = "KS",Account__r.BillingState = "LA",Account__r.BillingState = "MN",Account__r.BillingState = "MO",Account__r.BillingState = "MT",Account__r.BillingState = "NE",Account__r.BillingState = "NV",Account__r.BillingState = "NM",Account__r.BillingState = "ND",Account__r.BillingState = "OK",Account__r.BillingState = "OR",Account__r.BillingState = "SD",Account__r.BillingState = "TX",Account__r.BillingState = "UT",Account__r.BillingState = "WA",Account__r.BillingState = "WY")

This should be a relatively simple request I think, but I can't find another example that has the specifics I need.

 

I would like to update one object based on changes made to another object where one string is equivalent.  In this case, I have a custom object (Customobj__c) that has a trigger on it to update a case with fields from that custom object based on a field that they both have in common.  One of the fields I want to update is a multi-select picklist, too (Event_Types__c).  The primary weirdness I am encountering is that the multi-select picklist doesn't always update when I save the Custom Object record, but I also suspect my code isn't laid out efficiently.  Can anyone help?

 

Here's the code:

 

trigger OnCustobjCaseFields on Customobj__c (before insert, before update)
{
String objSharedField; String objRecordId; String objAccount; String objEventTypes; for(Customobj__c obj: Trigger.new) { if (obj.Shared_Field__c != Null) { objSharedField = obj.Shared_Field__c; objRecordId = obj.Id; objAccount = obj.Account__c; objEventTypes = obj.Event_Types__c; } } List<Case> cases = new List<Case>([SELECT Id, Shared_Field__c, AccountId, Expected_Event_Types__c FROM Case WHERE Shared_Field__c = :objSharedField]); List<Case> caseUpdateList = new List<Case>(); for(Case c: cases) { c.AccountId = objAccount; c.App__c = objRecordId; c.Expected_Event_Types__c = objEventTypes; caseUpdateList.add(c); } if(caseUpdateList.size() > 0) { update caseUpdateList; } }

 

  • September 30, 2013
  • Like
  • 0

I'm trying to write a trigger that fills in a custom lookup field on a case by looking for a custom object with an exact match in a text field.

 

Basically, what I would like to do is when the custom object (custobj) is created or saved, and the text field (Package ID) has a value, find all cases with the same value in an equivalent custom field, and add the custom object to a lookup field on the case.

 

All attempts at writing this run into the same issue which is how to form the SOQL query to pull the related cases to update.  The following query is obviously totally wrong, but hopefully it highlights where my problem is.  What I think I need to do is pull all cases that have the Package_ID__c field which matches a Package_ID__c field on the custom object.  Can anyone help clarify how to do this?

 

List<Case> cases = new List<Case>([SELECT Id, Package_ID__c FROM Case WHERE Package_ID__c = custobj.Package_ID__c);

Thanks!!

  • September 13, 2013
  • Like
  • 0

How do I write a test class to check whether a new Opportunity Contact Role was created from a custom field based off of an Opportunity insert or update?

 

I've written a Trigger (mostly repurposed code) to create an Opportunity Contact Role when an Opportunity is created or updated with a Primary Contact (custom Contact Lookup field).  I'm not sure how to write a test class for it, though.  The trigger seems to work well enough.

 

Here's what it does: If a new Opp is created with a Primary Contact (required field), a new OCR is created as a Primary Contact and with a Decision Maker Role.  If the Primary Contact is changed, the old OCR is deleted, then the recreated as an OCR with the Influencer Role, while the new Primary Contact is created as the Primary with a Decision Maker Role.  If the insert fails, a friendly error message is thrown.

 

trigger OnOpportunityPrimaryContactRole on Opportunity (after insert, after update) {    
    
    List<OpportunityContactRole> newCRList = new List<OpportunityContactRole>();
    List<OpportunityContactRole> oldCRList = new List<OpportunityContactRole>();
    Set<Id> OppId = new Set<Id>();
    Set<Id> ContactId = new Set<Id>();  
    
    for(Opportunity o: Trigger.new)
    {
        //Checks if the Opportunity is being inserted
        if(Trigger.isInsert)
        {
            if(o.Primary_Contact__c != null)
            {
                //Creates the new OCR
                newCRList.add(new OpportunityContactRole (ContactId=o.Primary_Contact__c, OpportunityId=o.Id, Role='Decision Maker',IsPrimary=TRUE));                                                                       
            }
        }
        else if(o.Primary_Contact__c != null && Trigger.oldMap.get(o.Id).Primary_Contact__c != null)
            	{
                	//Gets the Contact and Opportunity Id from the prior values and adds to this set              
                	Opportunity oldOppObj=Trigger.oldMap.get(o.Id);
                    OppId.add(OldoppObj.id);
                	ContactId.add(oldOppObj.Primary_Contact__c);
                    	//Selects old OCRs
        				if (OppId.size()>0) oldCRList=[Select Id from OpportunityContactRole where ContactId in : ContactId  and OpportunityId in : OppId];
      
        				//Deletes old OCRs
       					if (oldCRList.size()>0) delete oldCRList;
                    
                		if(oldOppObj.Primary_Contact__c != o.Primary_Contact__c)
                    	{
                        	newCRList.add(new OpportunityContactRole (ContactId=o.Primary_Contact__c, OpportunityId=o.Id, Role='Decision Maker',IsPrimary=TRUE));
                            newCRList.add(new OpportunityContactRole (ContactId=oldOppObj.Primary_Contact__c, OpportunityId=o.Id, Role='Influencer',IsPrimary=False));
                    	}
                }      
    }  
    try
    {
        //Inserts new OCRs
        if(newCRList.size()>0) insert newCRList;
    }    
    catch(Exception e)
    {
        System.debug(e);
        trigger.new[0].addError('Uh Oh...  A technical error has occurred creating the Opportunity Contact Role. Please email the SFDC admin or try again later.');
    }
}

 

 

Hi - the issue I am having is that when using validation on a junction object, it looks like everything should be right, but just doesn't work at all.

 

For the purposes of this, the primary objects I'm working with are a custom promotional event object and the standard account object.

 

The logic is that I want to check if:

 

1. On the promotional event, the Event State (custom picklist field) is "NY"

2. On the account, the account state is one of a list of values

 

If the criteria is met, an error should be returned

 

If anyone can help or suggest a better way, I'm all ears!

 

Here's my validation formula:

 

AND(ISPICKVAL(Promotional_Event__r.Event_State__c,"NY"),Account__r.BillingState = "AK", Account__r.BillingState = "AZ",Account__r.BillingState = "AR",Account__r.BillingState = "CA",Account__r.BillingState = "CO",Account__r.BillingState = "HI",Account__r.BillingState = "ID",Account__r.BillingState = "IA",Account__r.BillingState = "KS",Account__r.BillingState = "LA",Account__r.BillingState = "MN",Account__r.BillingState = "MO",Account__r.BillingState = "MT",Account__r.BillingState = "NE",Account__r.BillingState = "NV",Account__r.BillingState = "NM",Account__r.BillingState = "ND",Account__r.BillingState = "OK",Account__r.BillingState = "OR",Account__r.BillingState = "SD",Account__r.BillingState = "TX",Account__r.BillingState = "UT",Account__r.BillingState = "WA",Account__r.BillingState = "WY")