+ Start a Discussion
reema_agarwalreema_agarwal 

Help with apex triggers

I have made a lookup field of contacts in ma asset object.

I have made a default contact as No user in ma contacts as contact is a compulsory field in assets.

whenever i add a contact and the value is not 'No User' the status of asset should change to 'assigned' from the picklist.

when i free dat asset from that contact or make it as No User the status the status should change from assigned  to instock.

Can Someone Please help me.

 

 

trigger updateAsset on Asset (after insert, after update) {
if(trigger.new[0].Name !=null && trigger.new[0].contactid != 'No User'){
Asset assetsOwned= [select Name,status from asset where Name=:trigger.new[0].Name];
assetsOwned.status = 'Assigned';
update assetsOwned;
}
/*if( trigger.isUpdate && trigger.old[0].id !=null && trigger.old[0].id != trigger.new[0].id){
Asset assetsOwnedOld = [select id, status from asset where id=:trigger.old[0].id];
assetsOwnedOld.status = 'Instock';
update assetsOwnedOld;


}*/
}

Best Answer chosen by Admin (Salesforce Developers) 
Prady01Prady01

Hello there, I read your post, the requirement seems like you have update a filed on asset depending the other filed thats also on asset right!!?? If so, then you can use the filed update on the workflow to make it happen... Hope my suggestion helps!! sorry if it didnt!!

All Answers

Henry AkpalaHenry Akpala

Since your trigger is on asset, you don't have to do a DML operation on the asset trigger.  All you need to do is set the value on the status field in the trigger.new and that should update the field and change the trigger to before update/insert.

 

trigger updateAsset on Asset (before insert, before update) {
if(trigger.new[0].Name !=null && trigger.new[0].contactid != 'No User'){
Asset assetsOwned= [select Name,status from asset where Name=:trigger.new[0].Name];
trigger.new[0].status = 'Assigned';
}

 

Regards

-H

Prady01Prady01

Hello there, I read your post, the requirement seems like you have update a filed on asset depending the other filed thats also on asset right!!?? If so, then you can use the filed update on the workflow to make it happen... Hope my suggestion helps!! sorry if it didnt!!

This was selected as the best answer
reema_agarwalreema_agarwal

Hello Henry the code is still not working.Could u please help me out with something.

I will tell u the scenario,it is as follows:

I have made a lookup field of contacts in my asset object.

I have made a default contact as No user in my contacts as contact is a compulsory field in assets due to parent child relation.

whenever i add an asset and the contact lookup value is not 'No User' the status of asset should change to 'assigned' from the picklist.

when i free that asset from that contact or make it as No User the status the status should change from assigned  to instock.

 

  Thanks!

Starz26Starz26

You should be able to do this via workflow and not a trigger.

 

Set the workflow to check the value of your user lookup and see if the ID = the ID of the 'No User' record. If so, update the fields as desired.

 

Use a secod workflow to set it if theID does not equal

reema_agarwalreema_agarwal

Thanks Pradeep and Henry.I could do it using workflows.