
Please can someone have a look at the code below and tell me where I am going wrong?
Thank you for your help,Kind regardsJoetrigger AttachmentIDtoCourseSale on Attachment (after insert) {
List course_saleList = new List();
Set course_saleIds = new Set();
for (Attachment att : trigger.New){
if(att.ParentId.getSobjectType() == course_sale.SobjectType){
course_saleIds.add(att.ParentId);
}
}
course_saleList = [select id, from course_sale where id in : course_saleIds];
if(course_saleList!=null && course_saleList.size()>0{
course_sale.AttachmentId__c = "att.Id";
}
update course_saleList;
}
Hello Joe,Try to use following Code.Apex Trigger:
Let me know if you have any question on this. Please mark this "Solved" if it helps.Thank You,Hitesh PatelEmail :- hiteshpatel.aspl@gmail.comhttp://mrjavascript.blogspot.in/trigger AttachmentIDtoCourseSale on Attachment (after insert) {
Map<Id, Id> mapCSId = new map<Id, Id>();
List<Course_Sal__c> lstCSUpdate = new List<Course_Sal__c>();
String objectName = '';
for(Attachment atc: trigger.new){
String myIdPrefix = string.valueOf(atc.ParentId).substring(0,3);
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
for(Schema.SObjectType stype : gd.values()){
Schema.DescribeSObjectResult r = stype.getDescribe();
String prefix = r.getKeyPrefix();
if(prefix!=null && prefix.equals(myIdPrefix)){
objectName = r.getName();
}
}
if(objectName == 'Course_Sal__c'){
mapCSId.put(atc.ParentId, atc.id);
}
}
if(!mapCSId.isEmpty()){
List<Course_Sal__c> lstCourseSal = [select id from Course_Sal__c where id in: mapCSId.keyset()];
for(Course_Sal__c cs: lstCourseSal){
cs.AttachmentId__c = mapCSId.get(cs.id);
}
if(!lstCourseSal.isEmpty()){
update lstCourseSal;
}
}
}