function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DJAISHDJAISH 

Related Lookup Update/insert ignored

I've tried this just about every way I can spin it.

I have an object that creates opportunities based on criteria given, basically it's used for a payment plan situation. On this other object is a lookup to Contact and to Campaign.

I am trying to put Account and Campaign on the created opportunity. Shouldn't be hard. Here's what happens:

I get the information:

system.debug(thisCamp); //gives me the campaign ID I want
Opportunity thisOpp = new Opportunity(
            Name=OppName,
            Campaign=[SELECT Id, Name FROM Campaign WHERE Id=:thisCamp] //returns 1, and the right one when debug was run on it.
        );
Opportunities.add(thisOpp); // add to list to be inserted later
system.debug(thisOpp); //Ignores my Campaign field returns Opportunity:{Name=DefaultNameForTest}

 The above is a very simplified version of the code, since everything else works. The opportunity will get created, all the other fields (aside from account which gives me the same issue) will insert just fine. 

Is this something regarding standard field lookups? It works fine if I create a custom lookup to campaign, but that means I have to rewrite any logic that might be used based on the standard fields elsewhere ... doesn't seem right.

The trigger is run on creation of the parent record (let's call it PaymentPlan). 

 

Thank you

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

You already have an ID, so why would you query the campaign?

 

Just do this:

 

Opportunity thisOpp = new Opportunity(
    Name = OppName,
    CampaignId = thisCamp,
    StageName = 'Prospecting',
    CloseDate = System.Today()+90);

Bascially, lookup fields have two versions, the SObject version and the ID version. The ID version will have "ID" at the end of it, like in this case. You'd use the version you're trying to use when using upsert and attempting to automatically associate the campaign by an External ID field instead of ID.