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
Kev BrierKev Brier 

Update Account Lookup when Opportunity is created from a Custom Object

I've been searching around to try to find the simplest solution to my problem and are somewhat confused on which way to go.

Basically I need the Account lookup field to auto populate on creating a new Opportunity from our custom object Site__c. I've been reading about Process Builder, Workflow Rules and my initial thought a trigger on Opportunity. 

Ideally I'd like to avoid having to write the trigger but assuming I do then I'm guessing this should be on the Opporunity before insert?

For clairty the custom object Site__c has a master detail relationship to the account and therefore I guessing this is where we map the Id?

Any guidance that could be offered would  be really appreciated.
 
Best Answer chosen by Kev Brier
sandeep sankhlasandeep sankhla
Hi Kev,

trigger UpdateAccountLookup on Opportunity (before insert) {

Set<id> setSiteids=new set<id>();
map<Id, String> mapsiteIdToopptId = new map<Id, String>();
Map<Id, String> mapOpptIdToAccId = new Map<Id, String>();


list<account> acclst = new list<account>();

    For(Opportunity o    :    trigger.new){
        
        setSiteids.add(o.site__c);
        mapsiteIdToopptId.put(o.site__c, o.OpptId);
    }
    
    for(Site__c objSite :   [Select Id , Account__c from Site__c where Id IN :setSiteids ])
    {
        mapOpptIdToAccId.put(mapsiteIdToopptId.get(objSite.Id), objSite.AccountId);
    }
    
    for(Opportunity 0 : trigger.new)
    {
        0.AccountId = mapOpptIdToAccId.get(0.Id);
    }
}

Your code will be like this..you will first get all siteIds from Opportunity and then you can get the account Id from Site and then you cna prepare a map opptId to accountId and assign the accountID to Opportunity


please provide the proper field API name and chck with this code it will work..Please implement and let me know if it helps you..

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 

All Answers

sandeep sankhlasandeep sankhla
Hi Kev,

I think you need somthing like this..

When you create a Site__c that time one opportunity should be created and we will get the Account Id from Custom object Site__c  adn map in opportunity which we are going to create..

Let me know if this is what you need so I can help you out..
Kev BrierKev Brier
Hi Sandeep,

Sounds like we're on wave length.

Basicall when creating a new Opportunity from a Site__c we want to map the account listed against the Site__c to prepopulate on the Opportunity.Account standard lookup field.

Assuming the trigger needs to be something like this:

trigger UpdateAccountLookup on Opportunity (before insert) {

Set<id>ids=new set<id>();
list<account>aclst=new list<account>();
    For(Opportunity o:trigger.new){
        if(o.Account!=Null){
            ids.add(o.Account);
        }
    }
    if(!ids.Isempty()){
        list<account>acc = [select id,name from account where Account=Null AND id in:ids];
        for(account ac:acc){
         
            for(opportunity op: trigger.new){
                op.Account = ac.Id;
                aclst.add(ac);
            }
        }
        update aclst;
    }   
}

This is the error I'm currently getting from the above - Error: Compile Error: Incompatible element type Account for collection of Id at line 7 column 13

Any help you could offer would be gratefully appreciated 
sandeep sankhlasandeep sankhla
Hi Kev,

trigger UpdateAccountLookup on Opportunity (before insert) {

Set<id> setSiteids=new set<id>();
map<Id, String> mapsiteIdToopptId = new map<Id, String>();
Map<Id, String> mapOpptIdToAccId = new Map<Id, String>();


list<account> acclst = new list<account>();

    For(Opportunity o    :    trigger.new){
        
        setSiteids.add(o.site__c);
        mapsiteIdToopptId.put(o.site__c, o.OpptId);
    }
    
    for(Site__c objSite :   [Select Id , Account__c from Site__c where Id IN :setSiteids ])
    {
        mapOpptIdToAccId.put(mapsiteIdToopptId.get(objSite.Id), objSite.AccountId);
    }
    
    for(Opportunity 0 : trigger.new)
    {
        0.AccountId = mapOpptIdToAccId.get(0.Id);
    }
}

Your code will be like this..you will first get all siteIds from Opportunity and then you can get the account Id from Site and then you cna prepare a map opptId to accountId and assign the accountID to Opportunity


please provide the proper field API name and chck with this code it will work..Please implement and let me know if it helps you..

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
This was selected as the best answer
Kev BrierKev Brier
Hi Sandeep,

Thanks for your help, please below - I will provide all API Names for clairty:

trigger UpdateAccountLookup on Opportunity (before insert) {

Set<id> setSiteids=new set<id>();
map<Id, String> mapsiteIdToopptId = new map<Id, String>();
Map<Id, String> mapOpptIdToAccId = new Map<Id, String>();


list<account> acclst = new list<account>();

    For(Opportunity o    :    trigger.new){
        
        setSiteids.add(o.Site_Location__c);
        mapsiteIdToopptId.put(o.Site_Location__c, o.Id);
    }
    
    for(Site__c objSite :   [Select Id , Parent_Account__c from Site__c where Id IN :setSiteids ])
    {
        mapOpptIdToAccId.put(mapsiteIdToopptId.get(objSite.Id), objSite.Parent_Account__c);
    }
    
    for(Opportunity o : trigger.new)
    {
        o.AccountId = mapOpptIdToAccId.get(o.Id);
    }
}

Custom Object where Opportunity is created from = site__c
Account lookup on the site__c = Parent_Account__c

Standard Object Opportunity, site lookup is = Site_Location__c
Account lookup on Opportunity = Account

Really appreciated your support - I can save the trigger but it does prepopulate the required field
 
sandeep sankhlasandeep sankhla
Hi Kev,

Are you getting any error while inserting the Opportunity ?
sandeep sankhlasandeep sankhla
Hi Kev,

I have implemented the same in my org and it is working correct, it is populating the Account field on Opportunity..

Please check once again and let me know if you are facing any issue...

Also please mark this as best answer if it solves your issue..

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
Kev BrierKev Brier
Hi Sandeep,

Receiving no errors and have tested in the environment. 

Basically when I create a new Opportunity from the Site the Site Lookup is propulated and the Account Lookup is left blank.

Kev
sandeep sankhlasandeep sankhla
Hi kev,

Got it...Once you click save then it will populate automatcially..Please save the record and then you will be able to see the related account in Account lookup...

Thanks
Sandeep
sandeep sankhlasandeep sankhla
Hi Kev,

there is no way to pre-populate the field value in standard page before save....In our custom page we can do this....

Also did you try with the above trigger after save it should populate the value in account lookup...and I just noticed this can be handle by work flow only..no need of this trigger...

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer Sandeep
Kev BrierKev Brier
Hi Sandeep,

Our Account lookup field is mandatory and therefore wont allow the record to be saved without updating the Account. In fact I believed this was standard setup for Salesforce, I might be wrong. Custom page using the standard controller in VF?

How can this be handled via workflow? Believe that Lookup fields can't be updated via workflow?

Please advise..

Kev
sandeep sankhlasandeep sankhla
Hi kev,

I am not sure where you are missing anything..even if it is mandatory it should work..I have implemented the same in my org and it is working fine..

I am creating Opportunity from site and on click of save it is getting saved and in Opporty there is one lookup to account in this lookp site.Account value is getting populated..

Please check once again or share your latest code which you are using...

And mandatory field you are having on Opportunity ???Account Name field?

 
sandeep sankhlasandeep sankhla
Hi Kev,

I got the issue..The account Name field is standard field but it is mandatory from page layout...In my org it is not mandatory so it is not throwing any error...

You can remove requiredness from edit layout and then it will work for you..or let me check if is there any workaround for this..

Thanks,
Sandeep
sandeep sankhlasandeep sankhla
Hi kev,

I confirmed, in this scenario we can only avoid this error if it is not mandatory..but if it is mandatory then it will throw error because Validation rule fire before Trigger and workflow and it will keep throwing the error to you...

Solution is you can create a custom page where you will add all these fields and there we can auto poulate the values ..
Another solution depends on your business feasibility..If you can remove the requiredness then it will work..

Please check and let me know the feedback..

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
Kev BrierKev Brier
Hi Sandeep,

Perfect - Marked your response as best answer.

Basically removed the requiredness from the layouts and have added a validation rule to handle if both values are left blank. Ulimately this stops the user creating a new opportunity from scratch without manually linking the Account. If the site location is entered or the opportunity is created from this object then the trigger will fire populating the Account field. 

Thanks for all your support, works like a dream.

Kev