You need to sign in to do that
Don't have an account?
Need Help with Triggers
Hi All,
My trigger was working fine before, but now that i have changed the Serial_Number_New__c field from data type text to a lookup, the trigger wont work. No error message is shown when i save the trigger. But when i try creating a case nothing happens.
Please help!!!
trigger UpdateWarranty on Case (before insert, before update) {
for(Case c : Trigger.new){
If(c.RecordTypeId == '012D0000000AoAZ'){
String cid = c.Serial_Number_New__c;
List<Warranty__c> Serials = [SELECT Engine_Number__c, Model_Number__c FROM Warranty__c WHERE Name = :cid];
c.Engine_s_n__c = Serials.get(0).Engine_Number__c;
c.Model_Number__c = Serials.get(0).Model_Number__c;
}
}
}
If you changed the field type from text to a lookup
This line:
String cid = c.Serial_Number_New__c;
Now sets "cid" to a ID datatype.
And this line
List<Warranty__c> Serials = [SELECT Engine_Number__c, Model_Number__c FROM Warranty__c WHERE Name = :cid];
Attempts to find that ID in a name field.
Is that what you intended? Normally I would expect to say
Warranty__c myWarranty = [SELECT Id, Engine_Number__c, Model_Number__c FROM Warranty__c WHERE Id = :cid];
Since you are querying by a known good ID field, you can assign to a single sobject instead of a list.
HTH
All Answers
If you changed the field type from text to a lookup
This line:
String cid = c.Serial_Number_New__c;
Now sets "cid" to a ID datatype.
And this line
List<Warranty__c> Serials = [SELECT Engine_Number__c, Model_Number__c FROM Warranty__c WHERE Name = :cid];
Attempts to find that ID in a name field.
Is that what you intended? Normally I would expect to say
Warranty__c myWarranty = [SELECT Id, Engine_Number__c, Model_Number__c FROM Warranty__c WHERE Id = :cid];
Since you are querying by a known good ID field, you can assign to a single sobject instead of a list.
HTH
Your a legend!!!
That worked perefectly, i didnt realise that the data type had been set to ID for cid. Ahhhh Crazy!!!
I still got lots to learn...
Thank you very much.
Few observations:
a) Serial_Number_New__c is Id field ( because it is lookup) so in select query use " where id = :cid" not the Name.
b) Do not use select query in main loop, prepare map before the main loop and use it within loop.
This think better solution is :
If you have lookup ref then you can use formula fields for "Engine_s_n__c " and "Model_Number__c" instead using trigger.
Thank you for the two pointers.
The 1) point i have already incorporated into my code. However, can you please explain how do i use Map. I'm still fairly new to Apex coding and so would appreciate any help.
Before thinking about MAP:
Why dont you use formula fields; this will avoid trigger or coding,
This think better solution is :
If you have lookup ref then you can use formula fields for "Engine_s_n__c " and "Model_Number__c" instead using trigger.
So Varun's idea is to let a formula field do the work for you instead of a trigger.
Since you already have the lookup to the warranty on the record, could make a formula field with a value of something like
warranty__r.Engine_s_n__c
And another with
warranty__r.Model_Number__c
Right?
Yes
Guys,
The problem with formula's is that the field only appears once the save button is selected. Until than you cant see the Model_Number__c etc fields.
This is why i would prefer to use the trigger.