Skip to main content
Kev Brier (OCS Group UK) 님이 #Apex에 질문했습니다
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.

 
답변 14개
  1. 2015년 4월 1일 오전 11:19
    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 
0/9000