0

I am trying to figure why I am getting this error, even if I add a check null it comes back with the error for the if statement. I was trying to copy the solution from here: Copying attachments from object to object trigger.

I have an object where I need to have it copied as it reaches the due date along with the attachments. I had a different code solution but it proved to not be working with batch TBF updates and need something that will map the attachments properly to the correct parentId.

public static void createRiskCheck(Risk_Check__c[] riskcheckcreate){

    Schema.DescribeSObjectResult d = Schema.SObjectType.Risk_Check__c; 
    rtMapByName = d.getRecordTypeInfosByName();
    rtRiskCheck = rtMapByName.get('Risk Check');
    rtInitialRiskCheck = rtMapByName.get('Initial Risk Check Creation');
    Risk_Check__c newriskcheck;
    List<Attachment> newattclist = new list<Attachment>();
    list<Attachment> attachlist = new list<Attachment>();
    list<Note> notelist = new list<Note>();
    list<Note> newntelist = new list<Note>();
    Set<Id> initialId = new Set<Id>();
    List<Risk_Check__c> newriskchecklist = new list<Risk_Check__c>();

    for(Risk_Check__c initialcreate : riskcheckcreate){
        initialId.add(initialcreate.id);
        newriskcheck = new Risk_Check__c();
        newriskcheck.Name = initialcreate.Name;
        newriskcheck.Reason_for_check__c = initialcreate.Reason_for_check__c;
        newriskcheck.Check_Due_Date__c = initialcreate.Next_Review_Date__c;
        newriskcheck.Client_Name__c = initialcreate.Client_Name__c;
        newriskcheck.member_number_new__c = initialcreate.member_number_new__c;
        newriskcheck.Superfund__c = initialcreate.Superfund__c;  
        newriskcheck.Initial_Risk_Check_Creation__c = initialcreate.Id; 
        newriskcheck.Related_Account__c = initialcreate.Related_Account__c;
        newriskcheck.Stage__c = 'To Be Assigned';
        newriskcheck.RecordTypeId = rtRiskCheck.getRecordTypeId();
        newriskchecklist.add(newriskcheck);
    }
    //need to insert the new risk check first so we can use the Id when cloning any attachments on the initial create object 
    insert newriskchecklist;

    attachlist = [select Id, SystemModstamp, ParentId, OwnerId, Name, LastModifiedDate, LastModifiedById, IsPrivate, 
                           IsDeleted,  Description, CreatedDate, CreatedById, ContentType, BodyLength, Body from Attachment where ParentId in: initialId];

    if(initialId != null && initialId.size() > 0){

        Map<Id,Risk_Check__c> riskcheckMap = new Map<Id,Risk_Check__c>([select Id,Initial_Risk_Check_Creation__c 
                                                                        from Risk_Check__c where Initial_Risk_Check_Creation__c in : initialId]); 

        List<Attachment> attachments = new List<Attachment>();

        for(Attachment file : attachlist){
            Attachment newFile = file.clone();
            if(newFile != null){
                newFile.ParentId = riskcheckMap.get(file.ParentId).Id;
            }
            attachments.add(newFile);
        }
        insert attachments;

    }


}

The line I am getting the error is 45. I have tried changing it to the following but then the if statement line returns the same error:

if(riskcheckMap.get(file.ParentId).Id != null){
     newFile.ParentId = riskcheckMap.get(file.ParentId).Id;
}
2
  • riskcheckMap doesn't have a value with a key of file.ParentId.
    – Nick C
    Commented Sep 5, 2016 at 5:17
  • What can I do to fix it?
    – Michael
    Commented Sep 5, 2016 at 5:44

1 Answer 1

2

Change your if condition

if(riskcheckMap.containsKey(file.ParentId) && riskcheckMap.get(file.ParentId).Id != null){
     newFile.ParentId = riskcheckMap.get(file.ParentId).Id;
}

this will first check if you have key exist and then check for the Id.

2
  • Thanks! This fixed the error however the code is not working as intended, when the next custom object creates none of the attachments come across. It clones the attachment to the original object.
    – Michael
    Commented Sep 5, 2016 at 6:28
  • ok, I did thanks: salesforce.stackexchange.com/questions/139489/…
    – Michael
    Commented Sep 5, 2016 at 7:07

You must log in to answer this question.

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