function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
StaciStaci 

creating junction object record help

What code does:
When the Update button is clicked on an Alert record, it creates a new one, pulling all information from previous Alert and updates the Master Alert if it isn't empty.  Would also like it to create the junction object within this class.  I've tried with some code I use on other junction object creation, but I'm getting the following error:
insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Case]: [Case]

I have it updating the Case and Alert fields that are required, but I'm obviously not doing something right.  Can anyone please help?

Class:
public class CW_alertUpdate{
public final Alerts__c alertID;
public Alerts__c newAlert{get;set;}
      public CW_alertUpdate(ApexPages.StandardController controller) {
        //gets fields from previous case
        alertID = [SELECT Id, Master_Alert_Lookup__c FROM Alerts__c WHERE Id = :ApexPages.currentPage().getParameters().get('alertID')];
     }
     public pageReference autorun(){
         //create a new Alert
         Alerts__c newAlert = new Alerts__c(Id = ApexPages.currentPage().getParameters().get('newid'));
        // Fill in the values for the new record from previous case
        if(alertID.Master_Alert_Lookup__c == NULL)
        {
         newAlert.Master_Alert_Lookup__c = alertID.Id;
         }
         //Update the new Alert record
        update newAlert;
       
        //create a new Case Alert Association - Junction Object
        Case_Alert_Association__c newCAA = new Case_Alert_Association__c();

         // Fill in the values for the new record
         newCAA.Alerts__c = newAlert.id;
         newCAA.Case__c = newAlert.Incident_Ticket_Number__c;
              
         //Insert the new Case Alert Association record
          Database.insert (newCAA);


        system.debug(newAlert);
        PageReference retPage = new PageReference('/' + newAlert.id);
        return retPage;
       } }

Button Code:
/a0H/e?retURL={!Alerts__c.Id}&00Ne0000000jigO={!Alerts__c.Incident_Start__c}&00Ne0000000jifu={!Alerts__c.Product__c}&00Ne0000000jigd={!Alerts__c.Team_List__c}&CF00Ne0000000jigi={!Alerts__c.Incident_Manager__c}&CF00Ne0000000jigi_lkid={!Alerts__c.Incident_ManagerId__c}&00Ne0000000jigT={!Alerts__c.Incident_Reported__c}&00Ne0000000jifk={!Alerts__c.Subject__c}&CF00Ne0000000jigs={!Alerts__c.Account_Name__c}&CF00Ne0000000jigs_lkid={!Alerts__c.Account_NameId__c}&
00Ne0000000jifz={!Alerts__c.Customer_Impact__c}&00Ne0000000jig4={!Alerts__c.Completed_Actions__c}&00Ne0000000jig9={!Alerts__c.Next_Actions__c}&CF00Ne0000000jigE={!Alerts__c.Incident_Ticket_Number__c}&CF00Ne0000000jigE_lkid={!Alerts__c.Incident_Ticket_NumberId__c}&00Ne0000000dZZQ={!Alerts__c.CGM_P1_AMERICAS_MANNED__c}&00Ne0000000dZZL={!Alerts__c.CGM_P1_APD_MANNED__c}&00Ne0000000dZZH={!Alerts__c.CGM_P1_Command__c}&00Ne0000000dZZG={!Alerts__c.CGM_P1_EAME_MANNED__c}&CF00Ne0000000uccj={!Alerts__c.Master_Alert_Lookup__c}&CF00Ne0000000uccj_lkid={!Alerts__c.Master_Alert_LookupId__c}&saveURL=/apex/CW_AlertUpdate?alertID={!Alerts__c.Id}
Shashank SrivatsavayaShashank Srivatsavaya
Hi,

At this line,

newCAA.Case__c = newAlert.Incident_Ticket_Number__c;

Case__c should be assigned the case record ID and not the case number. Does Incident_Ticket_Number__c return the case ID or case number?
StaciStaci
thanks for your fast response Shashank.  Its a lookup to the Case, its returning the actual case number, not the 15 digit ID.  How do I rememdy that?
ShashForceShashForce
You can query for the case Id with the case number using SOQL.
StaciStaci
I'm at a loss as to how to do that with my existing code across objects... Can you help me out please?
ShashForceShashForce
Is "newAlert.Incident_Ticket_Number__c" a lookup field to case object? In that case it should ideally return the Id. Can you do a system.debug and check what it is returning?
StaciStaci
yes its a lookup to Case.  Here's the debug log:

27.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
12:30:04.027 (27364126)|EXECUTION_STARTED
12:30:04.027 (27410323)|CODE_UNIT_STARTED|[EXTERNAL]|066e00000000itm|VF: /apex/CW_AlertUpdate
12:30:04.030 (30643248)|CODE_UNIT_STARTED|[EXTERNAL]|01pe00000005jWN|CW_alertUpdate <init>
12:30:04.030 (30661563)|SYSTEM_MODE_ENTER|true
12:30:04.031 (31473556)|METHOD_ENTRY|[1]|01pe00000005jWN|CW_alertUpdate.CW_alertUpdate()
12:30:04.031 (31488087)|METHOD_EXIT|[1]|CW_alertUpdate
12:30:04.031 (31676456)|SYSTEM_METHOD_ENTRY|[8]|ApexPages.currentPage()
12:30:04.031 (31727218)|SYSTEM_METHOD_EXIT|[8]|ApexPages.currentPage()
12:30:04.031 (31762216)|SYSTEM_METHOD_ENTRY|[8]|System.PageReference.getParameters()
12:30:04.031 (31820386)|SYSTEM_METHOD_EXIT|[8]|System.PageReference.getParameters()
12:30:04.031 (31858135)|SYSTEM_METHOD_ENTRY|[8]|MAP<String,String>.get(Object)
12:30:04.031 (31876670)|SYSTEM_METHOD_EXIT|[8]|MAP<String,String>.get(Object)
12:30:04.032 (32364494)|SOQL_EXECUTE_BEGIN|[8]|Aggregations:0|select Id, Master_Alert_Lookup__c, Incident_Ticket_Number__c from Alerts__c where Id = :tmpVar1
12:30:04.036 (36016945)|SOQL_EXECUTE_END|[8]|Rows:1
12:30:04.036 (36242386)|CODE_UNIT_FINISHED|CW_alertUpdate <init>
12:30:04.037 (37153323)|CODE_UNIT_STARTED|[EXTERNAL]|01pe00000005jWN|CW_alertUpdate invoke(autorun)
12:30:04.037 (37319322)|SYSTEM_METHOD_ENTRY|[12]|ApexPages.currentPage()
12:30:04.037 (37356296)|SYSTEM_METHOD_EXIT|[12]|ApexPages.currentPage()
12:30:04.037 (37376874)|SYSTEM_METHOD_ENTRY|[12]|System.PageReference.getParameters()
12:30:04.037 (37401469)|SYSTEM_METHOD_EXIT|[12]|System.PageReference.getParameters()
12:30:04.037 (37427254)|SYSTEM_METHOD_ENTRY|[12]|MAP<String,String>.get(Object)
12:30:04.037 (37442007)|SYSTEM_METHOD_EXIT|[12]|MAP<String,String>.get(Object)
12:30:04.037 (37593352)|DML_BEGIN|[20]|Op:Update|Type:Alerts__c|Rows:1
12:30:04.089 (89875315)|CODE_UNIT_STARTED|[EXTERNAL]|Workflow:01Ie00000008rcp
12:30:04.102 (102711181)|WF_RULE_EVAL_BEGIN|Workflow
12:30:04.102 (102737040)|WF_CRITERIA_BEGIN|[Alerts: IA-00000175-14 a0He0000002Pwx5]|Alert for CGM_P1_Command|01Qe00000004kYx|ON_CREATE_ONLY
12:30:04.102 (102743759)|WF_RULE_NOT_EVALUATED
12:30:04.102 (102750592)|WF_CRITERIA_BEGIN|[Alerts: IA-00000175-14 a0He0000002Pwx5]|Alert for CGM_P1_EAME_MANNED|01Qe00000004kZR|ON_CREATE_ONLY
12:30:04.102 (102754572)|WF_RULE_NOT_EVALUATED
12:30:04.102 (102759825)|WF_CRITERIA_BEGIN|[Alerts: IA-00000175-14 a0He0000002Pwx5]|Alert for CGM-P1-AMERICAS-MANNED|01Qe00000000ReW|ON_CREATE_ONLY
12:30:04.102 (102763905)|WF_RULE_NOT_EVALUATED
12:30:04.102 (102769034)|WF_CRITERIA_BEGIN|[Alerts: IA-00000175-14 a0He0000002Pwx5]|Alert for CGM_P1_APD_MANNED|01Qe00000004kZM|ON_CREATE_ONLY
12:30:04.102 (102772841)|WF_RULE_NOT_EVALUATED
12:30:04.102 (102786137)|WF_SPOOL_ACTION_BEGIN|Workflow
12:30:04.102 (102793958)|WF_ACTION| None
12:30:04.102 (102797711)|WF_RULE_EVAL_END
12:30:04.102 (102840834)|WF_ACTIONS_END| None
12:30:04.102 (102847270)|CODE_UNIT_FINISHED|Workflow:01Ie00000008rcp
12:30:04.103 (103689197)|DML_END|[20]
12:30:04.103 (103774934)|SYSTEM_METHOD_ENTRY|[21]|System.debug(ANY)
12:30:04.103 (103822980)|USER_DEBUG|[21]|DEBUG|null
12:30:04.103 (103838760)|SYSTEM_METHOD_EXIT|[21]|System.debug(ANY)
12:30:04.104 (104328130)|SYSTEM_METHOD_ENTRY|[29]|Database.insert(SObject)
12:30:04.104 (104367640)|DML_BEGIN|[29]|Op:Insert|Type:Case_Alert_Association__c|Rows:1
12:30:04.123 (123209845)|DML_END|[29]
12:30:04.123 (123282460)|VF_PAGE_MESSAGE|Required fields are missing: [Case]
12:30:04.123 (123369436)|EXCEPTION_THROWN|[29]|System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Case]: [Case]
12:30:04.125 (125259070)|SYSTEM_METHOD_EXIT|[29]|Database.insert(SObject)
12:30:04.125 (125350468)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Case]: [Case]

Class.CW_alertUpdate.autorun: line 29, column 1
12:30:04.125 (125363109)|CODE_UNIT_FINISHED|CW_alertUpdate invoke(autorun)
StaciStaci
Here's a better log, its pulling 2 of the Alert ID's and not the actual Incident ticket numberID:

27.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
12:36:21.027 (27997813)|EXECUTION_STARTED
12:36:21.028 (28050571)|CODE_UNIT_STARTED|[EXTERNAL]|066e00000000itm|VF: /apex/CW_AlertUpdate
12:36:21.042 (42064153)|CODE_UNIT_STARTED|[EXTERNAL]|01pe00000005jWN|CW_alertUpdate <init>
12:36:21.042 (42090622)|SYSTEM_MODE_ENTER|true
12:36:21.042 (42859083)|METHOD_ENTRY|[1]|01pe00000005jWN|CW_alertUpdate.CW_alertUpdate()
12:36:21.042 (42871583)|METHOD_EXIT|[1]|CW_alertUpdate
12:36:21.043 (43046267)|SYSTEM_METHOD_ENTRY|[8]|ApexPages.currentPage()
12:36:21.043 (43112904)|SYSTEM_METHOD_EXIT|[8]|ApexPages.currentPage()
12:36:21.043 (43153880)|SYSTEM_METHOD_ENTRY|[8]|System.PageReference.getParameters()
12:36:21.043 (43203703)|SYSTEM_METHOD_EXIT|[8]|System.PageReference.getParameters()
12:36:21.043 (43234121)|SYSTEM_METHOD_ENTRY|[8]|MAP<String,String>.get(Object)
12:36:21.043 (43254168)|SYSTEM_METHOD_EXIT|[8]|MAP<String,String>.get(Object)
12:36:21.043 (43740500)|SOQL_EXECUTE_BEGIN|[8]|Aggregations:0|select Id, Master_Alert_Lookup__c, Incident_Ticket_Number__c from Alerts__c where Id = :tmpVar1
12:36:21.048 (48194478)|SOQL_EXECUTE_END|[8]|Rows:1
12:36:21.048 (48583676)|CODE_UNIT_FINISHED|CW_alertUpdate <init>
12:36:21.050 (50965309)|CODE_UNIT_STARTED|[EXTERNAL]|01pe00000005jWN|CW_alertUpdate invoke(autorun)
12:36:21.051 (51231831)|SYSTEM_METHOD_ENTRY|[12]|ApexPages.currentPage()
12:36:21.051 (51306197)|SYSTEM_METHOD_EXIT|[12]|ApexPages.currentPage()
12:36:21.051 (51350969)|SYSTEM_METHOD_ENTRY|[12]|System.PageReference.getParameters()
12:36:21.051 (51395698)|SYSTEM_METHOD_EXIT|[12]|System.PageReference.getParameters()
12:36:21.051 (51444324)|SYSTEM_METHOD_ENTRY|[12]|MAP<String,String>.get(Object)
12:36:21.051 (51469583)|SYSTEM_METHOD_EXIT|[12]|MAP<String,String>.get(Object)
12:36:21.051 (51686216)|DML_BEGIN|[20]|Op:Update|Type:Alerts__c|Rows:1
12:36:21.104 (104910406)|CODE_UNIT_STARTED|[EXTERNAL]|Workflow:01Ie00000008rcp
12:36:21.198 (198432474)|WF_RULE_EVAL_BEGIN|Workflow
12:36:21.198 (198484710)|WF_CRITERIA_BEGIN|[Alerts: IA-00000176-14 a0He0000002PwxA]|Alert for CGM_P1_Command|01Qe00000004kYx|ON_CREATE_ONLY
12:36:21.198 (198493630)|WF_RULE_NOT_EVALUATED
12:36:21.198 (198500753)|WF_CRITERIA_BEGIN|[Alerts: IA-00000176-14 a0He0000002PwxA]|Alert for CGM_P1_EAME_MANNED|01Qe00000004kZR|ON_CREATE_ONLY
12:36:21.198 (198508127)|WF_RULE_NOT_EVALUATED
12:36:21.198 (198514144)|WF_CRITERIA_BEGIN|[Alerts: IA-00000176-14 a0He0000002PwxA]|Alert for CGM-P1-AMERICAS-MANNED|01Qe00000000ReW|ON_CREATE_ONLY
12:36:21.198 (198518234)|WF_RULE_NOT_EVALUATED
12:36:21.198 (198523523)|WF_CRITERIA_BEGIN|[Alerts: IA-00000176-14 a0He0000002PwxA]|Alert for CGM_P1_APD_MANNED|01Qe00000004kZM|ON_CREATE_ONLY
12:36:21.198 (198527264)|WF_RULE_NOT_EVALUATED
12:36:21.198 (198545856)|WF_SPOOL_ACTION_BEGIN|Workflow
12:36:21.198 (198554127)|WF_ACTION| None
12:36:21.198 (198557958)|WF_RULE_EVAL_END
12:36:21.198 (198625193)|WF_ACTIONS_END| None
12:36:21.198 (198632682)|CODE_UNIT_FINISHED|Workflow:01Ie00000008rcp
12:36:21.199 (199637598)|DML_END|[20]
12:36:21.199 (199709366)|SYSTEM_METHOD_ENTRY|[21]|System.debug(ANY)
12:36:21.199 (199782381)|USER_DEBUG|[21]|DEBUG|Alerts__c:{Master_Alert_Lookup__c=a0He0000002PwZqEAK, Id=a0He0000002PwxAEAS}
12:36:21.199 (199793916)|SYSTEM_METHOD_EXIT|[21]|System.debug(ANY)
12:36:21.200 (200229279)|SYSTEM_METHOD_ENTRY|[29]|Database.insert(SObject)
12:36:21.200 (200261902)|DML_BEGIN|[29]|Op:Insert|Type:Case_Alert_Association__c|Rows:1
12:36:21.227 (227250614)|DML_END|[29]
12:36:21.227 (227347821)|VF_PAGE_MESSAGE|Required fields are missing: [Case]
12:36:21.227 (227461025)|EXCEPTION_THROWN|[29]|System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Case]: [Case]
12:36:21.228 (228617729)|SYSTEM_METHOD_EXIT|[29]|Database.insert(SObject)
12:36:21.228 (228735941)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Case]: [Case]

Class.CW_alertUpdate.autorun: line 29, column 1
12:36:21.228 (228749844)|CODE_UNIT_FINISHED|CW_alertUpdate invoke(autorun)
StaciStaci
I've fixed it now, 

I replaced 

newCAA.Case__c = newAlert.Incident_Ticket_Number__c;

with

newCAA.Case__c = alertID.Incident_Ticket_Number__c;