You need to sign in to do that
Don't have an account?
SF Dev Ceno
From Process Builder To Trigger: Invoke Apex Class From Trigger
All,
I'm attempting to simply invoke my apex class from a trigger instead of the process builder. How do I invoke the following class in my trigger? It doesn't fire, yet my debug log shows the objIds.size() = 1.
Class:
Trigger:
I'm attempting to simply invoke my apex class from a trigger instead of the process builder. How do I invoke the following class in my trigger? It doesn't fire, yet my debug log shows the objIds.size() = 1.
Class:
global class WR_UpdateGeo { @InvocableMethod public static void getAccountIds(List<Id> objIds) { //Get the sObject token from the first ID Schema.SObjectType token = objIds[0].getSObjectType(); Schema.DescribeSObjectResult dr = token.getDescribe(); String Scountry = 'country'; if (dr.getName() == 'account') Scountry = 'BillingCountry'; // Construct query dynamically. String queryString = 'SELECT id, ' + Scountry + ' FROM ' + dr.getName() + ' WHERE '; for(ID objId : objIds) { queryString += 'Id=\'' + objId + '\' OR '; } queryString = queryString.subString(0, queryString.length() - 4); sObject[] objDBList = Database.query(queryString); // GEO String geo = ''; List<Country_GEO_Mapping__c> lst_countryGeoMapping = [SELECT Country_Name__c, GEO_Name__c FROM Country_GEO_Mapping__c ]; Map<String, String> map_countryGeoMapping = new Map<String, String>(); for(Country_GEO_Mapping__c cg: lst_CountryGeoMapping){ map_countryGeoMapping.put(cg.Country_Name__c, cg.GEO_Name__c); } // Update the GEO on the sObjects for(Integer i = 0; i < objDBList.size(); i++) { if (dr.getName() == 'account') { if(map_countryGeoMapping.containsKey(String.valueOf(objDBList[i].get('BillingCountry')))) { geo = map_countryGeoMapping.get(String.valueOf(objDBList[i].get('BillingCountry'))); } objDBList[i].put('GEO__c', geo); } //geo = 'APAC';} //objDBList[i].put('GEO__c', Utility_Class.FindGeoFromCountry(String.valueOf(objDBList[i].get('BillingCountry')))); else { if(map_countryGeoMapping.containsKey(String.valueOf(objDBList[i].get('country')))) { geo = map_countryGeoMapping.get(String.valueOf(objDBList[i].get('country'))); } objDBList[i].put('GEO__c', geo); } //objDBList[i].put('GEO__c', Utility_Class.FindGeoFromCountry(String.valueOf(objDBList[i].get('country')))); } Database.SaveResult[] srList = Database.update(objDBList, false); } }
Trigger:
trigger WR_BeforeLead on Lead (before insert, before update) { List<Id> objIds = new List<Id>(); if (Trigger.isBefore && Trigger.isUpdate) { for(Lead l : Trigger.New){ if(l.Id != null) { objIds.add(l.Id); } } if(objIds.size() > 0){ System.debug(objIds.size()); WR_UpdateGeo.getAccountIds(objIds); } } }
Below Link will definately help you out with this problem!!
https://salesforce.stackexchange.com/questions/54425/problem-with-database-update
All Answers
You are facing this proble because Triggers can’t reference invocable methods.
so i would suggest you to make apex Trigger and a Action class and call this class from your Trigger.
Thanks
Ashish
Mark this answer best if it helps you!!
Yes that should also work ,defifning triggers in a class will help to know the flows better and manage plus it will help you to add methods later in the same trigger class which we generally call Helper Classes.you can take the help of below Link.
http://blog.mirketa.com/salesforce-developer-guide-use-of-helperhandler-class-to-manage-trigger-execution/
thanks
Hey Ashish,
I commented out the InvocableMethod annotation, confirmed the objIds.size() > 0, yet it's still not updating the Geo field on the Lead. Any ideas why this wouldn't execute the update?
Below Link will definately help you out with this problem!!
https://salesforce.stackexchange.com/questions/54425/problem-with-database-update