You need to sign in to do that
Don't have an account?
sudhirn@merunetworks.com
Trigger is not updating values
Hi,
Wrote a below trigger after insert and after update for some reasons its not firing please suggest me what might be the issue.
Thanks
Sudhir
Wrote a below trigger after insert and after update for some reasons its not firing please suggest me what might be the issue.
trigger territory_lookup on Lead (After Insert, After Update) { Try { set<id> gid = new set<id>(); Set<String> gzip = new Set<String>(); Set<String> gstate = new Set<String>(); Set<String> gcountry = new Set<String>(); Integer intzip; lead ld = [select id,postalcode,state,country from lead where id = :Trigger.newMap.keySet() limit 1]; intzip=Integer.valueof(ld.postalcode); system.debug(intzip); String ldstate; String ldcountry; ldstate = ld.state; ldcountry = ld.country; Territory_Lookup__c tl = [select Theater__c,Region__c,Area__c,User__c FROM Territory_Lookup__c where Zip_Start__c <= :intzip and Zip_End__c >= :intzip and State__c in :gstate and Country__c in :gcountry limit 1]; list<lead> led = [select id,postalcode,state,country from lead where id = :Trigger.newMap.keySet() limit 1]; for(lead uld : led) { uld.Territory_Area__c = tl.Area__c; uld.Territory_Region__c = tl.Region__c; uld.Territory_Theater__c = tl.Theater__c; } update led; } catch(Exception ex){ system.debug(ex); } }
Thanks
Sudhir
All Answers
On the other note, i suggest to do this before insert/update.
and why limit to 1?
My reqyirement is as user change the postalcode in lead trigger my fire and update the values below code is working fine in developer console but it is not working when applied in trigger after insert and after update please suggest me how to fix this issue
Thanks
Sudhir
How about change line 28 to 39 to test if it works.
lead led = [select id,postalcode,state,country from lead where id = :Trigger.newMap.keySet() limit 1];
led.Territory_Area__c = tl.Area__c;
led.Territory_Region__c = tl.Region__c;
led.Territory_Theater__c = tl.Theater__c;
update led;
Hi David,
I am hardcoding the lead id to test it in developer console its working fine. I need to convert this into after insert and after update events.
Reason I need this is because as user changes the value it must automatically update to that value let me know if my requirement is not clear will explain in another post with code
Thanks
Sudhir
I am getting below error while saving the class file Please suggest how to change
Error: Compile Error: unexpected token: 'Territory_Lookup__c' at line 1 column 7
Thanks
Sudhir
Error: Compile Error: unexpected token: public at line 40 column 1
Let me explain my requirement again. In lead when ever user changes the ping code or state or country trigger should be able to lookup from object Territory_Lookup__c and display region area and theater information please suggest me how to get this real time as and when user updates the value in lead please suggest me looks like this should be achieved from after insert and after update concept
Thanks
Sudhir
Yes i think i understand the requirement, and already created the code/trigger to achieve that behaviour. ther thing is, you cannot do it via AFTER trigger because it will have an 'infinite call' on the trigger, that's why i had it on BEFORE trigger. Please try to review the code. thanks.
Above code is working fine in developer console
Thanks
Sudhir
I am getting Please suggest me how to fix this issue
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger territory_lookup caused an unexpected exception, contact your administrator: territory_lookup: execution of BeforeUpdate caused by: System.NullPointerException: Argument cannot be null.: ()
Thanks
Sudhir
Please replace this
Zip_Start__c <= :gzip and Zip_End__c >=:gzip
with
Zip_Start__c <= :upperBound and Zip_End__c >=:lowerBound
gzip is a list and the zip comparision should be by integer thats why i created those 2 variables.
and also please remove the limit 1 on all queries,
regards,
For example if lead dont have zip have only country I am trying to get the soql belonging to that country if lead have only zip try to lookup to get this value.
Problem now is when I change country from one value to another its not updating when run the trigger for multiple times its working please suggest me how to fix this issue.
Do we have to change the event of trigger to after update need your suggestion
Thanks
Sudhir
Below is the code which i modifed as you suggested for some reasons only zip works perfect please suggest me for combination values like if zip i null and country only exist it must be able to pick value from the territory list and return
Thanks
Sudhir
Can you please help me with your code I am stuck in this trigger with you logic
Trigger below works perfect with zip code filters
Same trigger am adding only country filter its not working real time when we make a update its working only when multiple times it is updated. Please suggest me what is the issue in the logic
Need you suggestion its valuable for me now
Thanks
Sudhir