You need to sign in to do that
Don't have an account?

Using sobjects in trigger
Hello all-
I have a scenario where the three objects trigger have relatively same logic for beforeInsert and beforeUpdate. Even the fields of these objects that i use in trigger are same. this is my below code. i want to use sobject so that i dont mention the object name Bill_To__c in the below code. Instead I use sobject so that my other two objects Sold_To__c and Running_Account__c can use the same trigger. Can anyone help?
public static void relateWCSSToAccount(Lis<Bill_To__c> wcssList,Map<Id,Bill_To__c> oldMap) {
List<String> guids = new List<String>();
Map<String,Id> accountMap = new Map<String,Id>();
for(Bill_To__c wcss:wcssList)
{
// In case of Update, Add GUID when trigger old map GUID is not equal to trigger new list GUID.
if(oldMap!=null && oldMap.size()>0)
{
if(wcss.GUID__c!=(oldMap.get(wcss.Id).GUID__c))
guids.add(wcss.GUID__c);
}
// Incase of insert; get the new GUIDs
if(wcss.GUID__c!=null)
{
guids.add(wcss.GUID__c);
}
}
if(guids.size()>0)
{
//Find matching accounts based on GUID
for(Account a : [SELECT Id,GUID__c FROM Account WHERE GUID__c in:guids])
{
accountMap.put(a.GUID__c,a.Id);
}
}
if(accountMap!=null && accountMap.keyset().size()>0)
{
// Iterate through Trigger.New list of WCSS
for(WCSS_Bill_To__c wcssNew : wcssList)
{
if(wcssNew.GUID__c!=null)
{
if(accountMap.get(wcssNew.GUID__c)!=null)
{
// Update Account on each WCSS_Bill_To_c object.
wcssNew.Account__c = accountMap.get(wcssNew.GUID__c);
}
}
}
}
}
I have a scenario where the three objects trigger have relatively same logic for beforeInsert and beforeUpdate. Even the fields of these objects that i use in trigger are same. this is my below code. i want to use sobject so that i dont mention the object name Bill_To__c in the below code. Instead I use sobject so that my other two objects Sold_To__c and Running_Account__c can use the same trigger. Can anyone help?
public static void relateWCSSToAccount(Lis<Bill_To__c> wcssList,Map<Id,Bill_To__c> oldMap) {
List<String> guids = new List<String>();
Map<String,Id> accountMap = new Map<String,Id>();
for(Bill_To__c wcss:wcssList)
{
// In case of Update, Add GUID when trigger old map GUID is not equal to trigger new list GUID.
if(oldMap!=null && oldMap.size()>0)
{
if(wcss.GUID__c!=(oldMap.get(wcss.Id).GUID__c))
guids.add(wcss.GUID__c);
}
// Incase of insert; get the new GUIDs
if(wcss.GUID__c!=null)
{
guids.add(wcss.GUID__c);
}
}
if(guids.size()>0)
{
//Find matching accounts based on GUID
for(Account a : [SELECT Id,GUID__c FROM Account WHERE GUID__c in:guids])
{
accountMap.put(a.GUID__c,a.Id);
}
}
if(accountMap!=null && accountMap.keyset().size()>0)
{
// Iterate through Trigger.New list of WCSS
for(WCSS_Bill_To__c wcssNew : wcssList)
{
if(wcssNew.GUID__c!=null)
{
if(accountMap.get(wcssNew.GUID__c)!=null)
{
// Update Account on each WCSS_Bill_To_c object.
wcssNew.Account__c = accountMap.get(wcssNew.GUID__c);
}
}
}
}
}
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dynamic_dml.htm#apex_get_put_field_values
Please find below the code edited to handle sObject.
ublic static void relateWCSSToAccount(List<sObject> wcssList,Map<Id,sObject> oldMap) {
List<String> guids = new List<String>();
Map<String,Id> accountMap = new Map<String,Id>();
for(sObject wcss:wcssList)
{
// In case of Update, Add GUID when trigger old map GUID is not equal to trigger new list GUID.
if(oldMap!=null && oldMap.size()>0)
{
if(wcss.get('GUID__c')!=(oldMap.get(wcss.get('Id')).GUID__c))
guids.add(wcss.GUID__c);
}
// Incase of insert; get the new GUIDs
if(wcss.get('GUID__c')!=null)
{
guids.add(wcss.get('GUID__c'));
}
}
if(guids.size()>0)
{
//Find matching accounts based on GUID
for(Account a : [SELECT Id,GUID__c FROM Account WHERE GUID__c in:guids])
{
accountMap.put(a.GUID__c,a.Id);
}
}
if(accountMap!=null && accountMap.keyset().size()>0)
{
// Iterate through Trigger.New list of WCSS
for(Bill_To__c wcssNew : wcssList)
{
if(wcssNew.get('GUID__c')!=null)
{
if(accountMap.get(wcssNew.get('GUID__c'))!=null)
{
// Update Account on each WCSS_Bill_To_c object.
wcssNew.put('Account__c', accountMap.get(wcssNew.GUID__c));
}
}
}
}
}
I have not tested this but this is the way it should be handled for sObjects. Hope this helps!
Please let me know if it works for you.
Thanks
List<String> guids = new List<String>();
Map<String,Id> accountMap = new Map<String,Id>();
for(sobject s:soList)
{
Id id = (Id) s.get('Id');
String guid = (String) s.get('GUID__c');
// In case of Update, Add GUID when trigger old map GUID is not equal to trigger new list GUID.
if(oldMap!=null && oldMap.size()>0)
{
String oldGuid = (String) oldMap.get(id).get('GUID__c');
if(guid!=oldGuid)
{
guids.add(guid);
}
}
// Incase of insert; get the new GUIDs
if(guid!=null)
{
guids.add(guid);
}
}
if(guids.size()>0)
{
//Find matching accounts based on GUID
for(Account a : [SELECT Id,GUID__c FROM Account WHERE GUID__c in:guids])
{
accountMap.put(a.GUID__c,a.Id);
}
}
if(accountMap!=null && accountMap.keyset().size()>0)
{
// Iterate through Trigger.New list of WCSS
for(sobject so : soList)
{
String newGUID = (String) so.get('GUID__c');
if(newGUID!=null)
{
if(accountMap.get(newGUID)!=null)
{
// Update Account on each WCSS_Bill_To_c object.
so.put('Account__c',accountMap.get(newGUID));
}
}
}
}
}