function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
byrdsbyrds 

Simple Trigger - 2 small issues. Trigger for Newbie

I am new to Apex - Just completed the online SF Apex training, so please go easy on me.

 

I am trying to better understand Triggers so I have written some simple code to better my understanding.

 

Goal: When a new Assignment (custom object) is created or updated the trigger should create or update the record on  TestObject (another custom object) and bring over the field values 'Name', 'Waybill', 'Hello' from Assignment Obj.  

 

trigger TriggerOne on Assignment__c (before insert, before update) {

     

     list<TestObject__c> to = new list<TestObject__c> ();

     for (Assignment__c Ass :Trigger.new) { 

     to.add(new TestObject__c(Name = Ass.Truck__c
                               Waybill__c = Ass.Waybill_Number__c,
                                Hello__c = 'byrd'));
     }

     

     insert to;

}

 

 

The code runs with no errors.

 

First issue: Every time I update a field on the Assignment Obj that was created, the trigger fires and creates another record on the TestObj.

 

Second issue: The Truck field is a required lookup field on Assignment Obj and is writting some id string on the newly created record for TestObject rather that the actual truck number e.g.' a0EL0000000YvRJMA0' rather than '210'.

 

 

Thanks in advance for any help.

 

 

 

 

Vinit_KumarVinit_Kumar

Byrds,

 

How objects are related.Can you explain that,then I can help you out.

:) :) :) :) :):) :) :) :) :)

For your first issue. you can differentiate events using context variable like

 

trigger myAccountTrigger on Account (before insert,after insert,before update,after update) {
if(Trigger.isUpdate==true && Trigger.isBefore==true){
// Your code here
}
if(Trigger.isUpdate==true && Trigger.isAfter==true){
// Your code
}
if(Trigger.isInsert==true && Trigger.isAfter==true){
// Your code 
}
 if(Trigger.isInsert==true && Trigger.isBefore==true){
//Your code
}
}

 

for your second issue you need.

 

Name = get(Ass.Truck__c).Name // sort of  get(Ass.Obj_ID).Field_Name

 

 

byrdsbyrds

They are not related.

 

Thanks

byrdsbyrds

Ketan, I will give that a try and let you know.

 

Thanks

byrdsbyrds

Ketan,

 

In regards to the second solution:

 

When I put this line in I get a save error: 'Variable does not exist: Name'

 

Perhaps you have skipped some info that I should know, im new.

Will this line of code be added above the for loop, or does it matter?

Is this a stand alone line or should I be replacing an existing part of the code?

 

Thanks

 

:) :) :) :) :):) :) :) :) :)

Sorry about my little info.

 

what is trunk number field type? I mean Is it Autonumber Or any Custom Text field or Number Field.

 

Here you are just associating custom object to Name field so it is only returning ID of that record not actual number you want.

 

For that you need to traverse to that particular field using relationship queries. 

 

I am assuming here that your field type is number and it's name is Truck_Number__C;

 

so you need to put  to.add(new TestObject__c(Name = Ass.Truck__r.Truck_Number__C
                               Waybill__c = Ass.Waybill_Number__c,
                                Hello__c = 'byrd'));

 

I hope you'll get some idea from here.

 

For more info on relationships.

 

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_relationships.htm

 

Thanks!

 

Vinit_KumarVinit_Kumar

Ketan,

 

I would like to differ in a way that you need to query the field using relationship query and then use it your creation record,however as the object are not related I am surprised how 

byrdsbyrds

Ketan,

 

The Assignment Object holds a lookup field Truck.  The Look up field Truck is linked to a custom Object Truck where the standard field 'Name' holds the truck number for the Truck record.

 

When the trigger fires on Assignmet It should take data from the 3 Assignment fields and write this data to the custom object TestObject.  Waybill, Hello and Truck are fields on the Assignment object - Truck is the lookup field.

 

to.add(new TestObject__c(Name = Ass.Truck__r.Name

 

So in the line above, Truck__r is to read the Truck Object and the ".FieldToReadOnTheObject"?

If this is correct It looks like the Field Name on the cutom object Truck is 'Name' (standard field) and is a text data type.

 

Using the line above still returns a16L000000099IO rather than the truck number'123'.  

Thaks guys for the help so far.

 

byrdsbyrds

Vinit,

 

It looks like an id of a record but It is not the same id of the Truck record (looking at the id in the address bar of the browser while on Truck record 123).  Looking at the Truck 123's record th id in the browser is: a0EL0000000YvRJ.  

 

I will look in to a relationship field and see if this helps.

 

Thanks again.

Vinit_KumarVinit_Kumar

Hi 

trigger TriggerOne on Assignment__c (before insert, before update) {

list<TestObject__c> to = new list<TestObject__c> ();

List<Id> truckIds = new List<Id>();

for (Assignment__c Ass :Trigger.new) {
truckIds.add(ass.Truck__c);

}

List<Truck__c> truckList = [select Truck__r.name from Truck__c where id in:truckIds];
if(Trigger.IsInsert){
for (Assignment__c Ass :Trigger.new) {

to.add(new TestObject__c(Name = truckList[0].Truck__r.name,
Waybill__c = Ass.Waybill_Number__c,
Hello__c = 'byrd'));
}

insert to;
}

if(Trigger.IsUpdate){
for (Assignment__c Ass :Trigger.new) {

to.add(new TestObject__c(id=Ass.id,Name = truckList[0].Truck__r.name,
Waybill__c = Ass.Waybill_Number__c,
Hello__c = 'byrd'));
}

update to;
}
}

:) :) :) :) :):) :) :) :) :)

// It looks like you have Field type Autonumber, though it's Label is Name

 

trigger TriggerOne Assignment__c(after insert, after update) {

list<TestObject__c> to = new list<TestObject__c> ();

ID AssID;
for (Assignment__c Ass :Trigger.new) {


AssId = Ass.truck__c;

}


List<Truck__c> number1 = [select Name from truck__c where id =: CustId];


for(Truck__c num : number1) {

 

/// if your Truck Number field type is Text then you need to convert number to text using String.valueOf()


to.add(new TestObjects__c(Truck_Number__c = String.valueOf(num.Name), 

                      Waybill__c = Ass.Waybill_Number__c,
                      Hello__c = 'byrd'))); 
}
insert to;

}

byrdsbyrds

Vinit,

 

Error for the following line of code:

 

List<Truck__c> truckList = [select Truck__r.name from Truck__c where id in:truckIds];

 

Description Resource Path Location Type
Save error: Didn't understand relationship 'Truck__r' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. TriggerOne.trigger /SandBoxOne/src/triggers line 11 Force.com save problem

 

Kudos for staying with me guys - Ketan I have not tried your solution just yet as we are having network issues today.

Thanks Again

byrdsbyrds

Ketan,

 

The Truck number field on the truck record is not auto number.