+ Start a Discussion
Edward Scott 17Edward Scott 17 

opportunity contact count

Hi,

I am working on a DML statement that will first collect every opportunity where the close date equals 11/26/2018. Those opps will then be added to a second list and if they have a name of test then the opportunity will be updated. Most of it is working now. The problem I am having is updating the opportunity after it is found. I want to update the Primary Source Campaign (Campaign.name) which is a standard field on the opportunity with the value of the Primary Contact's (Primary_Contact__c a custom field on the opportunity that looks up to the Contact) Source Campaign (Source_Campaign__c another custom field on the contact page).

Long story short I am not sure how to write the apex code to look up the value of a custom field. I am pasting the code below.

Thanks in advance for your help,
Ed
 
//collect all opportunities
Date closeDate = Date.newInstance(2018, 11, 26);
List<Opportunity> oppList = [Select Id, Name, Description, Type, StageName, Primary_Contact__c, Campaign.name from Opportunity Where Closedate =:closeDate];

//Create a list to store the records that we need to update
List<Opportunity> updatedOppList = new List <Opportunity>();

for (Opportunity obj:oppList)
{
    if(obj.name =='test'){
        obj.campaign.name = Primary_Contact__c.Source_Campaign__c;
        updatedOppList.add(obj);
    }
}

//DML Statement to update the invoice status
update updatedOppList;

//Prints the value of updated invoices
System.debug('List has been updated and updated value of records are'+updatedOppList);

 
Maharajan CMaharajan C
Hi Edward,

Change the query like below: 

List<Opportunity> oppList = [Select Id, Name, Description, Type, StageName, Campaign, Primary_Contact__c, Primary_Contact__r.Source_Campaign__c, Campaign.name from Opportunity Where Closedate =:closeDate];

and inside the for loop change the below line as like below: 

        obj.campaign = obj.Primary_Contact__r.Source_Campaign__c;  


Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C
Raj VakatiRaj Vakati
Try this 
 
//collect all opportunities
Date closeDate = Date.newInstance(2018, 11, 26);
List<Opportunity> oppList = [Select Id, Name, Description, Type, StageName, Primary_Contact__c, Campaign.name from Opportunity Where Closedate =:closeDate];

//Create a list to store the records that we need to update
List<Opportunity> updatedOppList = new List <Opportunity>();

for (Opportunity obj:oppList)
{
    if(obj.name =='test'){
        obj.campaignId = obj.Primary_Contact__c.Source_Campaign__r.Id;
        updatedOppList.add(obj);
    }
}

//DML Statement to update the invoice status
update updatedOppList;

//Prints the value of updated invoices
System.debug('List has been updated and updated value of records are'+updatedOppList);

 
Edward Scott 17Edward Scott 17
Thanks, it was actually a combination of both I am posting the code below. Also, one more question. I tried to update this to look for all the opportunities that were created in 2018. I keep getting an error. I know createddate is a date time field so I tried to say datetime createddate = datetime.year(2018) but it didn't work. Thanks again for your help.
 
//collect all opportunities
Date closeDate = Date.newInstance(2018, 11, 21);
List<Opportunity> oppList = [Select Id, Name, Description, Type, StageName, Primary_Contact__c, Campaign.name, Primary_Contact__r.Source_Campaign__r.Id, createdDate from Opportunity Where CloseDate =:closeDate];

//Create a list to store the records that we need to update
List<Opportunity> updatedOppList = new List <Opportunity>();

for (Opportunity obj:oppList)
{
    if(obj.name =='test'){
        obj.campaignId = obj.Primary_Contact__r.Source_Campaign__r.Id;
        updatedOppList.add(obj);
    }
}

//DML Statement to update the invoice status
update updatedOppList;

//Prints the value of updated invoices
System.debug('List has been updated and updated value of records are'+updatedOppList);