0

Hopefully someone has any idea about this issue because im ripping my hair out.

My goal is to add the field OCR__c on the campaignMember to a PDF. The PDF is generated and all fields except the OCR__c field, which is set to null.

The OCR is set without issues by another method and the campaign member is updated.

When the campaignmember is queried in getPDFCampaign OCR__c is returned with the newly updated value.

However, when the same query is made in getPDF OCR__c is returned as null. Even though the query is run AFTER the OCR__c has been set, and getPDFCampaign was able to query for the field correctly.

For some reason its like the query in getPDF runs on an "cached" version of the database?

Would really appreciate all of your inputs

getPDFCampaign:

public class getPDFCampaign {
public getPDFCampaign(){}
public void generateAll(string id){
    List<CampaignMember> allMembers = [
        SELECT  Id, Name, LeadId, FirstName, LastName, Title, Phone, City, State, PostalCode, Country, Street, OCR__c, SystemModStamp
        FROM CampaignMember
        WHERE CampaignId = :id

    ];

    for(CampaignMember member : allMembers){
        System.debug('GetPDFCampaign for: ' + member);
        PageReference defaultPage = new PageReference('/apex/getPDF');
        defaultPage.getParameters().put('member',member.Id);
        Blob pageData;
        if(!Test.isRunningTest()){
            pageData = defaultPage.getContentAsPDF();
        } else {
            pageData = Blob.valueOf('This is a test.');
        }
        Attachment att = new Attachment(
                            ParentId=member.LeadId,
                            Body=pageData,
                            Name='AVI ' + DateTime.now().formatLong() + '.pdf'
                        );
        insert att;
    }
}

}

getPDF

public class GetPDF {

public String priceList {get;private set;} // For branding purpose

public class memberWrapper{
    public Id Id{get;private set;}
    public String Name{get;private set;}
    public dateTime SystemModStamp{get;private set;}
    public String DeliveryCity{get;private set;}
    public String DeliveryCountry{get;private set;}
    public String DeliveryState{get;private set;}
    public String DeliveryStreet{get;private set;}
    public String DeliveryZip{get;private set;}
    public String CurrencyIsoCode{get;private set;}
    public String OCR{get;private set;}
    public String Title{get;private set;}
    public String Phone{get;private set;}
}

public memberWrapper pp{get;private set;}

public getPDF(){

    List<CampaignMember> currentMember = [
        SELECT  Id, Name, FirstName, LastName, Title, Phone, City, State, PostalCode, Country, Street, OCR__c, SystemModStamp
        FROM CampaignMember
        WHERE Id = :ApexPages.currentPage().getParameters().get('member')
        LIMIT 1
    ];



    pp=new memberWrapper();
    pp.Id=currentMember[0].Id;
    pp.Title=currentMember[0].Title;
    pp.Phone=currentMember[0].Phone;
    pp.Name=currentMember[0].FirstName + ' ' + currentMember[0].LastName;
    pp.SystemModStamp=currentMember[0].SystemModStamp;
    pp.DeliveryCity=currentMember[0].City;
    pp.DeliveryCountry=currentMember[0].Country;
    pp.OCR=currentMember[0].OCR__c;
    pp.DeliveryState=currentMember[0].State;
    pp.DeliveryStreet=currentMember[0].Street;
    pp.DeliveryZip=currentMember[0].PostalCode;

    System.debug('getPDF query: ' + currentMember);

 }

public memberWrapper getcurrentmemberWrapper() {
    return pp;
}

}

2

The problem is that getContentAsPDF is now implemented as a callout.

What this means is that when you make that call from within a transaction (that will only be committed at the end of the request), that call does not see the changes you are making from within the transaction. Generally, this is a good thing: other code either sees none (before the commit) or all (after the commit) of your changes. But it causes pain in this case.

Haven't looked in detail at your logic, but one solution to this is to move the getContentAsPDF and the following logic out into an @future method so the call runs after the transaction containing the changes you want in the PDF has committed.

| improve this answer | |
  • Thank you, this was what the problem was. When adding @future(callout=true) to the method which calls the PDF generation the OCR could be found in the SOQL query. Thanks! – Cnork Nov 21 '18 at 12:06
  • @user3167191 No problem. It's a very confusing problem that I've suffered too in the past. – Keith C Nov 21 '18 at 12:14

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.