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
frank2anilfrank2anil 

Updating lookup fields on 2 different objects

Hi guys,

 

I am new to salesforce .i am facing a problem can any body help me out.

 

Problem:-

 

I had a lookup filed called broker onto( contact) in opportunity.

And another lookup filed in a custom object (loan)with the same name broker on to (contact).

my requirement is if i update the  broker in loan it should update the opportunity broker field.

if i update with latest value in opportunity broker then it should update the same value in loan object broker filed.

 

please help me in these in writing the trigger.

 

Thanks in Advance.

Best Answer chosen by Admin (Salesforce Developers) 
cml26sepcml26sep

here is the modified part:

 

List<Opportunity> opptyList=[select Id,Borrower__c from Opportunity where Borrower__c IN :  loanOldBrokerMap.values() ] ;

 

What is the error you are getting after adding ":" ?

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippet as reference:

trigger CopyValueTrigger on Client__c (before insert,before update)

{

 

List<Client__c>client=trigger.new;

List<Account> acc=[select  Id,Name, BillingStreet,BillingCity,BillingState,BillingPostalCode,BillingCountry from Account where Id=:client[0].Account__c];

List<Contact> con=[select  Id,Name,MailingStreet,MailingCity,MailingState,MailingPostalCode,MailingCountry from Contact where Id=:client[0].Contact__c];

String AccountName=acc[0].Name;

String Street=acc[0].BillingStreet;

String City=acc[0].BillingCity;

String State=acc[0].BillingState;

String PostalCode=acc[0].BillingPostalCode;

String Country=acc[0].BillingCountry;

Contact contact=new Contact();

 

for(Contact a: con){

 

   a.MailingStreet=Street;

   a.MailingCity= City;

   a.MailingState =State;

   a.MailingPostalCode =PostalCode;

   a.MailingCountry =Country;

 

update a;

                   }

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

cmlcml

trigger loanTrigger on Loan (after insert, after update)

 

{

  Map<Id,Id> loanOldBrokerMap=new Map<Id,Id>;

  List<Loan> loanList=new List<Loan>();

  Map<Id,Id> OldBrokerToNewBrokerMap=new Map<Id,Id>;

  for(Loan l: trigger.New)

  {

   if(Trigger.isInsert || (trigger.isUpdate && trigger.oldMap.get(l.Id).broker!=l.broker))

    {

     loanOldBrokerMap.put(l.Id, trigger.oldMap.get(l.Id).broker);

     loanList.add(l);

     OldBrokerToNewBrokerMap.put(trigger.oldMap.get(l.Id).broker,l.broker);

    }

  }

 

 if(loanList.size()>0)

{

 List<Opportunity> opptyList=[select Id,Broker from Opprtunity where broker IN loanOldBrokerMap.values() ] ;

  for(Opprtunity oppty: opptyList) 

 {

  oppty.Broker =OldBrokerToNewBrokerMap.get(oppty.Broker);

 } 

 update opptyList;

}

}

 

Similarly you have to write trigger on Opprtunity to update Loan Broker field. Do not forget to check for change in Broker value field on Opportuniy trigger also

 

I hope this helps.

frank2anilfrank2anil

Hi cml,

 

Thanks for ur reply.

 

This was not saving and giving a error . 

 

 

trigger uploan on Loan__c (before insert) {

Map<Id,Id> loanOldBrokerMap = new Map<Id,Id>();
List<Loan__c> loanList=new List<Loan__c>();
Map<Id,Id> OldBrokerToNewBrokerMap=new Map<Id,Id> ();

for(Loan__c l: trigger.New){
if(Trigger.isInsert || (trigger.isUpdate && trigger.oldMap.get(l.Id).Borrower__c!=l.Borrower__c)){
loanOldBrokerMap.put(l.Id, trigger.oldMap.get(l.Id).Borrower__c);
loanList.add(l);
OldBrokerToNewBrokerMap.put(trigger.oldMap.get(l.Id).Borrower__c,l.Borrower__c);
}
}

if(loanList.size()>0){
List<Opportunity> opptyList=[select Id,Borrower__c from Opportunity where Borrower__c IN loanOldBrokerMap.values() ] ; // unexpected token: 'loanOldBrokerMap.values' at line 16 column 94 

for(Opportunity oppty: opptyList) {
oppty.Borrower__c =OldBrokerToNewBrokerMap.get(oppty.Borrower__c);
}
update opptyList;
}
}

 

Please help me 

 

Thanks

Frank

cml26sepcml26sep

You need to bind variables when using them in SOQL.

Just add ':' before loanOldBrokerMap.values() and it should work fine.

 

frank2anilfrank2anil

Hi cml

 

If u dont think other wise .please post me the modified code.

because iam getting errors when adding " :"  before to  loanOldBrokerMap.values()

 

Thanks 

Frank

cml26sepcml26sep

here is the modified part:

 

List<Opportunity> opptyList=[select Id,Borrower__c from Opportunity where Borrower__c IN :  loanOldBrokerMap.values() ] ;

 

What is the error you are getting after adding ":" ?

This was selected as the best answer