0

I have a VF page which inserts Sheet_c record and its child Responses__c at the same time. What I want to do is use these Responses__c and create a new Question in the same Sheet__c record but my trigger is failing. Can someone please point out what I'm doing wrong?

trigger CreateQuestionsnAnsRes2 on Sheet__c (after insert) {

    Set <Id> EISSets = New Set <Id> ();

    for (Sheet__c EIS : Trigger.New) {
        EISSets.add(EIS.id);
        system.debug('EISSets' + EISSets);
    }

    List <Responses__c> EIRLIst = [SELECT ID, Answer__c, Auto_Create__c, Question__c, Sheet__c,Questions__c
                                                 From Responses__c WHERE Sheet__c IN :EISSets];
    system.debug('EIRLIst' + EIRLIst);
    List <Questions__c> QuestionsList = New List <Questions__c> ();

    for (Responses__c EIR : EIRLIst) {
        Questions__c EIQ = New Questions__c ();
        EIQ.Questions__c = EIR.Questions__c ;
        EIQ.Sheet__c = EIR.Sheet__c;    
        QuestionsList.add(EIQ); 
    }

    if(QuestionsList.size() > 0) {
        insert QuestionsList;
    }    

    system.debug('QuestionsList' + QuestionsList );
}

Debug log below says EIRLIst is empty but I can see Responses__c entered by the users using VF page. Below is debug Log,

USER_DEBUG [6]|DEBUG|EISSets{a016F00001LN2feQAD}

USER_DEBUG [14]|DEBUG|EIRLIst()

USER_DEBUG [28]|DEBUG|QuestionsList()
3
  • Responses must certainly be added after the sheet. So they don't exist yet. You should change your logic to add a trigger to the response object. Mar 3, 2017 at 9:58
  • 1
    Also, you should read some tutorials about the Salesforce Process Builder. Because you can do all your requirements without code. Mar 3, 2017 at 10:04
  • Thank you Martin. I created a new trigger on response object and it worked. Appreciate your help.
    – Wendy
    Mar 3, 2017 at 11:40

1 Answer 1

-1

You have to do the same thing but with a trigger Responses__c after insert

trigger CreateQuestionsnAnsRes2 on Responses__c (after insert) {

List <Responses__c> EIRLIst = new List<Responses__c>();
for (Responses__c resp : Trigger.New) {
    if(resp.Sheet__c != null)       
        EIRLIst.add(resp);
}
system.debug('EIRLIst' + EIRLIst);
List <Questions__c> QuestionsList = New List <Questions__c> ();

for (Responses__c EIR : EIRLIst) {
    Questions__c EIQ = New Questions__c ();
    EIQ.Questions__c = EIR.Questions__c ;
    EIQ.Sheet__c = EIR.Sheet__c;    
    QuestionsList.add(EIQ); 
}

if(QuestionsList.size() > 0) {
    insert QuestionsList;
}    

system.debug('QuestionsList' + QuestionsList );

}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .