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;
}
}
}
}