You need to sign in to do that
Don't have an account?
Soundar
Contact Update based on Account Code
I am trying to update a contact based on accout Code not a account name (field of contact). this Account code is available in Account.
When i create a new contact i will give a account code , at that time account name need to update based on this account code only...
Here is the following Code, can you please give me a proper trigger for this process
********************************************************************************
trigger ContactMapToAccount on Contact (before insert , before update) {
//Set Of Id In Contact Code & Id
Set<Id> conId = New Set<Id>();
Set<Id> conCode = New Set<Id>();
for(Contact c : Trigger.New){
conId.add(c.Id);
}
List<Contact> lstCont = [Select id, Account_Code__c, name from Contact Where ID IN :conId];
Map<Id,Account> acMap = new Map<Id,Account>();
for(Account a : lstCont){
acMap.put(a.id, a);
}
for(Contact c : lstCont){
c.Account_Code__c = acMap.get(a.Account_Code__c);
}
}
**************************
Thnaks in advance
When i create a new contact i will give a account code , at that time account name need to update based on this account code only...
Here is the following Code, can you please give me a proper trigger for this process
********************************************************************************
trigger ContactMapToAccount on Contact (before insert , before update) {
//Set Of Id In Contact Code & Id
Set<Id> conId = New Set<Id>();
Set<Id> conCode = New Set<Id>();
for(Contact c : Trigger.New){
conId.add(c.Id);
}
List<Contact> lstCont = [Select id, Account_Code__c, name from Contact Where ID IN :conId];
Map<Id,Account> acMap = new Map<Id,Account>();
for(Account a : lstCont){
acMap.put(a.id, a);
}
for(Contact c : lstCont){
c.Account_Code__c = acMap.get(a.Account_Code__c);
}
}
**************************
Thnaks in advance
Hi Vadivel(Merfantz),
Thanks for your quick response , yeah we can't get Id before insert . I make some changes in my coding and it's working as well.
Regards,
Soundar Raj
+91- 7418425418
All Answers
"for(Account a : lstCont){"
Use List of Account instead of List of contact.
Thanks
Ajay Rawat
In before insert not able get id for insertion time. I think your total code approach is wrong. You can add list codes in list and query the account with list and Map the contact.
Hearty Thanks for your quick Reply
Now i have maken a few changes in the code . now i getting invalid Id Error while save new contact.
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger ContactMapToAccount caused an unexpected exception, contact your administrator: ContactMapToAccount: execution of BeforeInsert caused by: System.StringException: Invalid id: AP001: External entry point
******************************************************************************************************************
**********************************************************************************
May I Know Why This Error is Showing Right Now!!
Hi Vadivel(Merfantz),
Thanks for your quick response , yeah we can't get Id before insert . I make some changes in my coding and it's working as well.
Regards,
Soundar Raj
+91- 7418425418