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
cml9cml9 

Help Mapping Account Id from Contact in Trigger

Hi Experts,

Please help me in mapping out the Account Id from Contact so that I can assign the contact ID to the Opportunity that I created on the Trigger.

Please see code below: 
 
trigger DirectOpp on Direct_Opportunities__c (before insert) {
   
    for (Direct_Opportunities__c d : Trigger.new) {
        date myDate = date.valueOf(system.today());
     
        Opportunity o = new Opportunity();
        o.Name =  ' Direct Opportunity ';
        o.CloseDate = myDate + 7;
        o.StageName = 'New';
        o.OwnerId = d.Assigned_BC__c;
    //  o.AccountId =   I need the account Id here but I was not able to do it
        o.Contact__c = d.Contact__c;
        o.CampaignId = d.Campaign__c;
   
     
        insert o;
        
        Task t = new Task();
        
        t.Subject = 'Subject Type';
        t.ActivityDate = myDate + 1;
	t.Status = 'Not Started';
        t.Priority = 'High';
        t.OwnerId = d.Assigned_BC__c;
        t.WhatId = o.Id;
        
        insert t;
        
        
    }   
}

 
cloudSavvyProgcloudSavvyProg
Hi,

Try the following for AccountId:

o.AccountId = d.Contact__r.AccountId;

'Contact__r' should be relationship name of Contact field from the custom object.Check the contact field for relationship name and replace it if necessary.

Regards,
CloudSavvyProg
 
cml9cml9
I was able to fix this by using this code.
 
accountId = [select AccountId from Contact where id in :contactId].get(0).accountId;