0

I have a helper class that upserts custom object records based off the Opportunitylineitem. In October we switched a lot of logic around and are using new picklist/lookups and we did not update historical OpportunityLineitem data. But we are updating the custom object records. So, if a new record is created with the new fields everything works. However, if a historical OLI record is updated it updates null values into picklists that I manually populated.

Example:

public static void createRevPipe(List<OpportunityLineItem> scope)
{   
    List<Revenue_Pipeline__c> revPipelineToUpsert = new List<Revenue_Pipeline__c>();

    for(OpportunityLineItem oli : scope)
    {
        revPipelineToUpsert.add(new Revenue_Pipeline__c(
            Account__c = oli.Opportunity.AccountId,
            Product_Type__c = oli.Product_Type__c,

Product_Type__c is my new field if it's populated the mapping works, if it's not it's pushes null which clears out the field on the Revenue_Pipeline__c if I've come along after and done a data load to populate it.

I know I can do something like:

String pType;
if(oli.Product_Type__c != null)
   pType = oli.Product_Type__c;

then Product_type__c = ptype;

I'm struggling with how I can check for a null value and stop the mapping of the field.

Thoughts?

Edit:

You cannot do if statements in between parenthesis (). The question is how to do an if in the () if you cannot.

5
  • 1
    is the field returning null or an empty string when it is not populated? once you figure that one out, you should be able to properly handle validation, it is my understanding that picklists validation should be handled with isEmpty() == true. – glls Oct 18 '18 at 18:12
  • 1
    Possible duplicate of Apex check for blank picklist value – glls Oct 18 '18 at 18:15
  • so I can check that it's empty in code how can I stop the field being update without knowing if the destination field is populated? – Dan Wooding Oct 18 '18 at 18:17
  • New question new post =P just avoiding extended discussions in the comments. Thanks =) – glls Oct 18 '18 at 18:20
  • That's what I was trying to get to in my question. – Dan Wooding Oct 18 '18 at 18:23
1

You're close, you just need to extract the revenue pipeline record in to a variable:

    Revenue_Pipeline__c temp = new Revenue_Pipeline__c(
      /* put all values you know you're copying here */
    );
    if(oli.Product_Type__c != null) {
      temp.Product_Type__c = oli.Product_Type__c;
    }
    revPipelineToUpsert.add(temp);

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.