0

I am trying to create a trigger that creates a new audit record and updates it with values based on input from a form. (the form updates the Container_answer object records which are tied to everything with a global id.) I am setting the global Ids of the specific question-answer pairs in the form in the beginning of the trigger so I can pull the supplied answers from them in the If/else statements.

So I need it to create an audit record, fill the yumd3m01__Container_Instance__c lookup with the value from yumd3m01__Container_Instance__r.yumd3m01__Global_Id__c and then set the Name with any value right now, I can change later and then fill the other fields in the audit record with the other question answer pairs in the If Else statements.

code below:

trigger AuditPushDataTrig2 on yumd3m01__Container_Answer__c (before insert, after update) {

    system.debug('AuditPushDataTrig2');
    map<string, string> questionIds = new map<string, string>();
    questionIds.put('passFail', 'a0x41000000aJstAAE');
    questionIds.put('auditScore', 'a0x41000000aJsuAAE');
    questionIds.put('tradingPartner', 'a0x41000000aJsmAAE');
    questionIds.put('brand', 'a0x41000000aJszAAE');
    questionIds.put('targetScore', 'a0x41000000aJt1AAE');
    questionIds.put('auditorName', 'a0x41000000aJsxAAE');
    questionIds.put('auditorId', 'a0x41000000aJsyAAE');
    questionIds.put('auditDate', 'a0x41000000aJssAAE');

    Set<string> idSet = new Set<string>(questionIds.values());
    List<yumd3m01__Audit__c> AuditsToUpdate = new List<yumd3m01__Audit__c>();

        for(yumd3m01__Audit__c CreateAudit : Trigger.new);
            if(idSet.Contains(answer.yumd3m01__Container_Instance__c)){

         yumd3m01__Audit__c auditnew = new yumd3m01__Audit__c(Name = 'Triggered Audit', yumd3m01__Global_ID__c = CreateAudit.yumd3m01__Container_Instance__c);
                yumd3m01__Audit__c.add(auditnew);{

   }
    for(yumd3m01__Container_Answer__c answer : Trigger.New){
        if(idSet.Contains(answer.yumd3m01__Question__c)){

            string containerInstanceId = answer.yumd3m01__Container_Instance__c;
     }
         yumd3m01__Audit__c audit = new yumd3m01__Audit__c();{
            audit = [
                SELECT yumd3m01__Global_ID__c
                FROM yumd3m01__Audit__c
                WHERE yumd3m01__Container_Instance__r.yumd3m01__Global_Id__c =: containerInstanceId
            ];
            system.debug('answer: ' + answer);
            system.debug('audit: ' + audit);
            system.debug('answer.yumd3m01__Supplied_Answer__c: ' + answer.yumd3m01__Supplied_Answer__c);
            system.debug('answer.yumd3m01__Selected_Answer_Option__r.yumd3m01__Value__c: ' + answer.yumd3m01__Selected_Answer_Option__r.yumd3m01__Value__c);

            if(answer.yumd3m01__Question__c == questionIds.get('passFail')){
                system.debug('updating audit pass/fail');
                audit.yumd3m01__Audit_Pass_Fail__c = answer.yumd3m01__Container_Answer__c;
            }
            else if(answer.yumd3m01__Question__c == questionIds.get('auditScore')){
                system.debug('updating audit audit score');
                audit.yumd3m01__Audit_Score__c = answer.yumd3m01__Supplied_Answer__c;
            }

            else if(answer.yumd3m01__Question__c == questionIds.get('tradingPartner')){
            system.debug('updating trading partner name');
            audit.yumd3m01__Entity_Audited__c = answer.yumd3m01__Supplied_Answer__c;
            }

             else if(answer.yumd3m01__Question__c == questionIds.get('auditorName')){
            system.debug('updating auditor Name');
            audit.yumd3m01__Auditor_Name__c = answer.yumd3m01__Supplied_Answer__c;
            }
                else if(answer.yumd3m01__Question__c == questionIds.get('auditorId')){
            system.debug('updating auditor Id ');
            audit.yumd3m01__Auditor_Id__c  = answer.yumd3m01__Supplied_Answer__c;
            }
                else if(answer.yumd3m01__Question__c == questionIds.get('auditDate')){
            system.debug('updating audit Date ');
            audit.yumd3m01__Audit_Date__c  = answer.yumd3m01__Supplied_Answer__c;
            }               

            update audit;
            } 

     }
   }
}
5
  • From your debugging where are you seeing the issue at in your code? What does the record look like that you are inserting. Help us a bit here. And DO NOT perform and update DMl in a loop
    – Eric
    Dec 12, 2016 at 7:16
  • @ Eric, I am sorry should have included errors. error 1: Error: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, yumd3m01.AuditPushDataTrig2: execution of BeforeInsert caused by: System.QueryException: List has no rows for assignment to SObject Trigger.yumd3m01.AuditPushDataTrig2: line 28, column 1: [] After that i have no debugs because the trigger didn't create the record and move to the next for loop i guess
    – Jason
    Dec 12, 2016 at 7:21
  • Do a search on that error. Been discussed ad nauseam .
    – Eric
    Dec 12, 2016 at 7:24
  • @Eric , Yea. I have since changed my code around and I getting an error on line 25, yumd3m01__Audit__c auditnew = new yumd3m01__Audit__c(Name = 'Triggered Audit', yumd3m01__Global_ID__c = CreateAudit.yumd3m01__Container_Instance__c); yumd3m01__Audit__c.add(auditnew);{ So what I am trying to do is create a record and update all the values from another object. but also satisfy the required fields on the created record first. Name and the Global Id (lookup to container instance) just running into issues "Variable does not exist: CreateAudit.yumd3m01__Container_Instance__c"
    – Jason
    Dec 12, 2016 at 7:30
  • I am a novice when it comes to code so if I am doing things wrong or there is an easier/better way I am all ears
    – Jason
    Dec 12, 2016 at 7:32

1 Answer 1

0

What I ended up doing is creating a second trigger on before Insert that creates the audit record and fills the required fields and changed this trigger around to just After Insert. works now.

I was able to do this because the firing order of the data that I need to pull over needs to be after insert (it's getting populated by form responses). So I can have this audit record created, and only on a specific container instance as I am using a condition that it is an instance of another specific sObject record.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.