• Tina Dam
  • NEWBIE
  • 10 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 1
    Replies
Hi,

I have a custom object called Invoice_Header_Object that has a Master-Detail relationship with the Contact (a lookup field on the Contact object called Customer__c)

I also have another lookup field on the custom object that looks up the Account object (lookup field called Account__c)

I have a trigger that updates the Account__c field based on the Contact lookup field (grabs the Account on the Contact) and it works

My question is, how do I update the trigger to have it where if the Contact's Account is changed to another Account, have the trigger update the Account__c to reflect the new Account and also delete the associated Invoice Header from the previous Account

Here is my trigger:

trigger UpdateAccountIDInvoice on Invoice_Header__c (before insert,before update){
if (trigger.isBefore &&(trigger.isInsert||trigger.isUpdate))
{
set<Id> ContactIds = new set<ID>();
for(Invoice_Header__c cm: trigger.new)
{
contactIds.add(cm.customer__c);
}
Map<Id, Id> contactToAccountMap = new Map<Id, Id>();
for(contact c :[select Id, AccountId from contact where id in: contactIds])
{
      contactToAccountMap.put(c.Id,c.AccountId);}
for(Invoice_Header__c cm: trigger.new)
{
       cm.Account__c = contactToAccountMap.get(cm.customer__c);}   
}
}

thank you
I have a visualforce page that i'd like to have the "returl" to be directed to a field listed on the record (acc.Opportunity__c)
is that possible?

<apex:page Controller="RetUrlSearchController">
  <apex:form >
    <apex:pageBlock >
      <apex:pageBlockSection title="Criteria">
      <apex:outputLabel value="Enter Name Snippet"/>
      <apex:inputText value="{!nameQuery}"/>
      <apex:commandButton action="{!executeSearch}" value="Search"/>
    </apex:pageBlockSection>
   <apex:pageBlockTable value="{!accounts}" var="acc">
      <apex:column headerValue="Name">
         <apex:outputLink value="/{!acc.id}/e?retURL={!URLENCODE('/apex/InvoiceHeaderSearch?query='+nameQuery)}">{!acc.Name}</apex:outputLink>
      </apex:column>
      <apex:column value="{!acc.Invoice_Date__c}"/>
      <apex:column value="{!acc.Territory__c}"/>
   </apex:pageBlockTable>
</apex:pageBlock>

  </apex:form>
</apex:page>
Hi,

I may have this more complicated than it should be or there may be an easier way to do this.

I have a custom object (Invoice) that is a child to the Opportunity. We require the reps to look up an Invoice in Salesfore and attach the corresponding Opportunity to the custom lookup field Opportunity__c on the Invoice object

I wanted to make it as easy as possible for them, with as less clicks as possible. So I created a custom list button on the Invoice related list on the Opportunity page that brings them to a Visualforce page where they can search for an Invoice.
User-added image


Custom list button:  https://c.cs8.visual.force.com/apex/InvoiceHeaderSearch       I'd like to add this to the end to pass over the id (?id={!Opportunity.Id})


The visualforce page includes a search function for the Invoice and return results. It also includes an Edit link next to the results

Visualforce page:
My question is: Is it possible to pass the Opportunity ID over to the Visualforce page and then have it pass to the Edit link, so when the reps click on the Edit link, the Opportunity__c field is auto populated with the Opportunity ID?

Here is my visualforce page:
<apex:page standardController="Invoice_Header__c" extensions="Search" >

  <style type = "text/css">
    .Bld { font-weight:bold;}
  </style>
  
  <apex:form >
  
    <apex:pageblock title="Invoice" >
      <table align = "center" width = "100%" cellspacing = "5">
        <tr>
          <td class = "Bld">Invoice Name</td>
          <td><apex:inputText value="{!invName}" /></td>          
        </tr>
        <tr>
          <td align = "center" class = "Bld" colspan = "2"><apex:commandButton value="Search" action="{!find}"/></td> 
        </tr>        
      </table>
       
    </apex:pageblock>
        
    <apex:pageBlock title="Search Result">
      <apex:pageblockTable value="{!invList}" var="a">
        <apex:column >
        <apex:outputLink title="" value="/{!a.id}/e?retURL=/apex/{!$CurrentPage.Name}" style="font-weight:bold">Edit</apex:outputLink>&nbsp;|&nbsp;
          <apex:outputlink value="https://cs8.salesforce.com/{!a.id}">{!a.Name}</apex:outputlink>
        </apex:column>
        <apex:column value="{!a.id}"/>
      </apex:pageBlockTable>     
    </apex:pageBlock>    
      
  </apex:form>
</apex:page>
               
User-added image Controller: public with sharing class invsearchcontroller {      public list <Invoice_Header__c> acc {get;set;}      public string searchstring {get;set;}      public invsearchcontroller(ApexPages.StandardSetController controller) {      }      public void search(){        string searchquery='select name, Invoice_Date__c, Total__c,Territory__c,id from Invoice_Header__c where name like \'%'+searchstring+'%\' Limit 20';        acc= Database.query(searchquery);      }      public void clear(){      acc.clear();      }    } Thanks
Hi,

I'm very new to coding and am getting an error message every time I run my test class.

I wrote a simple trigger for a custom object "TIMBA_Survey_Summaries" that has a lookup on the Contact object. The trigger updates the Contact checkbox field "Elevate_Survey_Responded__c" to TRUE when a new Timba Survey Summary record with "TIMBASURVEYS__Complete_Survey_Name__c == 'Elevate Survey'" is inserted into the system.

Here is the trigger:

// Update Contact field: "Elevate_Survey_Responded__c" to TRUE when a new TIMBASURVEYS__Survey_Summary__c record is inserted

trigger UpdateElevateSurveyResponded on TIMBASURVEYS__Survey_Summary__c (after insert) {

// Will store related Contact record ID

map< id, contact > contacts = new map< id, contact >();

// Create trigger for new or selected TIMBASURVEYS__Survey_Summary__c

for(TIMBASURVEYS__Survey_Summary__c record:trigger.new)       

if(record.TIMBASURVEYS__Complete_Survey_Name__c == 'Elevate Survey')
if(record.TIMBASURVEYS__Complete_Survey_Name__c != null)

     // Update checkbox field on the parent Contact record to TRUE   

     contacts.put(record.TIMBASURVEYS__RelatedContact__c, new contact(id=record.TIMBASURVEYS__RelatedContact__c, Elevate_Survey_Responded__c = TRUE));     



update contacts.values();

This is the test class:

@isTest
private class UpdateElevateSurveyResponded {

    static testmethod void UpdateElevateSurveyResponded() {
    User u = [SELECT Id FROM User WHERE Alias = 'kdam'];
        // set up some test data       
        Contact c1 = new Contact(
            LastName='Dam'
            );
        insert c1;
                    
        // start the test execution context
        Test.startTest();

        // create a survey that runs through trigger
        System.runAs(u) {
        TIMBASURVEYS__Survey_Summary__c survey1 = new TIMBASURVEYS__Survey_Summary__c(
            TIMBASURVEYS__Complete_Survey_Name__c='Elevate Survey'
            );
        insert survey1; 

        System.assertEquals('Elevate Survey',survey1.TIMBASURVEYS__Complete_Survey_Name__c);  
       
        //Bulk validation  
        System.debug('Inserting 200 survey records... (bulk validation)');
       
        List<TIMBASURVEYS__Survey_Summary__c> survey2 = new List<TIMBASURVEYS__Survey_Summary__c>();
        for(integer i=0; i<200; i++) {
            survey2.add( new TIMBASURVEYS__Survey_Summary__c(TIMBASURVEYS__Complete_Survey_Name__c='Elevate Survey') );
        }
        insert survey2;
      
                }//end RunAs(u1) 
               
        // stop the test
        Test.stopTest();
        }
    }


This is the error message I keep getting:

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateElevateSurveyResponded: execution of AfterInsert

caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []

Trigger.UpdateElevateSurveyResponded: line 22, column 1: []

any help would be greatly appreciated. thanks in advance
Hi,

I'm very new to coding and am getting an error message every time I run my test class.

I wrote a simple trigger for a custom object "TIMBA_Survey_Summaries" that has a lookup on the Contact object. The trigger updates the Contact checkbox field "Elevate_Survey_Responded__c" to TRUE when a new Timba Survey Summary record with "TIMBASURVEYS__Complete_Survey_Name__c == 'Elevate Survey'" is inserted into the system.

Here is the trigger:

// Update Contact field: "Elevate_Survey_Responded__c" to TRUE when a new TIMBASURVEYS__Survey_Summary__c record is inserted

trigger UpdateElevateSurveyResponded on TIMBASURVEYS__Survey_Summary__c (after insert) {

// Will store related Contact record ID

map< id, contact > contacts = new map< id, contact >();

// Create trigger for new or selected TIMBASURVEYS__Survey_Summary__c

for(TIMBASURVEYS__Survey_Summary__c record:trigger.new)       

if(record.TIMBASURVEYS__Complete_Survey_Name__c == 'Elevate Survey')
if(record.TIMBASURVEYS__Complete_Survey_Name__c != null)

     // Update checkbox field on the parent Contact record to TRUE   

     contacts.put(record.TIMBASURVEYS__RelatedContact__c, new contact(id=record.TIMBASURVEYS__RelatedContact__c, Elevate_Survey_Responded__c = TRUE));     



update contacts.values();

This is the test class:

@isTest
private class UpdateElevateSurveyResponded {

    static testmethod void UpdateElevateSurveyResponded() {
    User u = [SELECT Id FROM User WHERE Alias = 'kdam'];
        // set up some test data       
        Contact c1 = new Contact(
            LastName='Dam'
            );
        insert c1;
                    
        // start the test execution context
        Test.startTest();

        // create a survey that runs through trigger
        System.runAs(u) {
        TIMBASURVEYS__Survey_Summary__c survey1 = new TIMBASURVEYS__Survey_Summary__c(
            TIMBASURVEYS__Complete_Survey_Name__c='Elevate Survey'
            );
        insert survey1; 

        System.assertEquals('Elevate Survey',survey1.TIMBASURVEYS__Complete_Survey_Name__c);  
       
        //Bulk validation  
        System.debug('Inserting 200 survey records... (bulk validation)');
       
        List<TIMBASURVEYS__Survey_Summary__c> survey2 = new List<TIMBASURVEYS__Survey_Summary__c>();
        for(integer i=0; i<200; i++) {
            survey2.add( new TIMBASURVEYS__Survey_Summary__c(TIMBASURVEYS__Complete_Survey_Name__c='Elevate Survey') );
        }
        insert survey2;
      
                }//end RunAs(u1) 
               
        // stop the test
        Test.stopTest();
        }
    }


This is the error message I keep getting:

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateElevateSurveyResponded: execution of AfterInsert

caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []

Trigger.UpdateElevateSurveyResponded: line 22, column 1: []

any help would be greatly appreciated. thanks in advance