Skip to main content
Modifying some existing code that updates a record and then redirects to a new page. I need to change it so that it runs a query to identify the correct record to update, and then updates the record.

 

public PageReference saveAttendee(){

List<CnP_PaaS_EVT__Event_attendee_session__c> attendee = [SELECT Id, Name, DTCI_Agreement_Active__c, CnP_PaaS_EVT__First_name__c, CnP_PaaS_EVT__Last_name__c, DTCI_Ticket_Level__c, CnP_PaaS_EVT__EventId__r.CnP_PaaS_EVT__category__r.name

FROM CnP_PaaS_EVT__Event_attendee_session__c

WHERE CnP_PaaS_EVT__Registrant_session_Id__c =:registrantId

AND DTCI_Ticket_Level__c =: ticketLevel

AND DTCI_Attendee_Info_Assigned__c = FALSE

ORDER BY DTCI_Ticket_Order__c

LIMIT 1

];

attendeeId = attendee.Id;

attendeeFirstName = attendee.CnP_PaaS_EVT__First_name__c;

attendeeLastName = attendee.CnP_PaaS_EVT__Last_name__c;

mailingCity = attendee.DTCI_Mailing_City__c;

mailingCountry = attendee.DTCI_Mailing_Country__c;

mailingCounty = attendee.DTCI_Mailing_County__c;

mailingState = attendee.DTCI_Mailing_State__c;

mailingStreet = attendee.DTCI_Mailing_Street__c;

mailingZip = attendee.DTCI_Mailing_ZIP_Postal_Code__c;

update attendee;

PageReference sendToReleaseForm = new PageReference('/apex/DTCI_Release_Form_Page_AB' + '?attendeePageId=' + attendee.id + '&registrantPageId=' + registrantId + '&eventPageId=' + eventId + '&releasePageType=' + attendee.DTCI_Release_Type__c);

sendToReleaseForm.setRedirect(true);

return sendToReleaseForm;

}

How can I query the object to find the next suitable record to update and still create the PageReference I need to move forward?

 
18 answers
  1. Nov 10, 2015, 9:48 PM

    Something like this might be helpful. Have the PageReference method call the List<CnP_PaaS_EVT__Event_attendee_session__c> method... This way the list method can still be called from other places in the code.

    public PageReference saveAttendee(){

    CnP_PaaS_EVT__Event_attendee_session__c attendee;

    // use for loop to get the first result, then break out of the loop.

    for (CnP_PaaS_EVT__Event_attendee_session__c firstResult : attendeeListFor(registrantId, ticketLevel) {

    attendee = firstResult;

    break;

    }

    // unable to proceed if attendee is not found...

    if (attendee == null)

    return null;

    attendeeId = attendee.Id;

    attendeeFirstName = attendee.CnP_PaaS_EVT__First_name__c;

    attendeeLastName = attendee.CnP_PaaS_EVT__Last_name__c;

    mailingCity = attendee.DTCI_Mailing_City__c;

    mailingCountry = attendee.DTCI_Mailing_Country__c;

    mailingCounty = attendee.DTCI_Mailing_County__c;

    mailingState = attendee.DTCI_Mailing_State__c;

    mailingStreet = attendee.DTCI_Mailing_Street__c;

    mailingZip = attendee.DTCI_Mailing_ZIP_Postal_Code__c;

    update attendee;

    PageReference sendToReleaseForm = new PageReference('/apex/DTCI_Release_Form_Page_AB');

    sendToReleaseForm.getParameters().put('attendeePageId', attendee.id);

    sendToReleaseForm.getParameters().put('registrantPageId', registrantId );

    sendToReleaseForm.getParameters().put('eventPageId', eventId );

    sendToReleaseForm.getParameters().put('releasePageType', attendee.DTCI_Release_Type__c);

    sendToReleaseForm.setRedirect(true);

    return sendToReleaseForm;

    }

    // this method still returns a list, but your PageReference method can call it...

    public List<CnP_PaaS_EVT__Event_attendee_session__c> attendeeListFor(String registrantId, String ticketLevel) {

    return [SELECT Id, Name, DTCI_Agreement_Active__c, CnP_PaaS_EVT__First_name__c, CnP_PaaS_EVT__Last_name__c, DTCI_Ticket_Level__c, CnP_PaaS_EVT__EventId__r.CnP_PaaS_EVT__category__r.name

    FROM CnP_PaaS_EVT__Event_attendee_session__c

    WHERE CnP_PaaS_EVT__Registrant_session_Id__c =:registrantId

    AND DTCI_Ticket_Level__c =: ticketLevel

    AND DTCI_Attendee_Info_Assigned__c = FALSE

    ORDER BY DTCI_Ticket_Order__c

    ];

    }

     
0/9000