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
reema_agarwalreema_agarwal 

Trigger to change the status of an asset

Hi

 

I am new to salesforce platform.I need help with triggers.

I have created a look up field in contacts named Assets_owned. whenever a particular asset is selected in assets_owned i want to change the asset status to assigned from the picklist in the assets tab.

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
jungleeejungleee

Hi ,

 

Below solution is a simple one, this cannot be used for bulk records updates

 

trigger updateAssetsOwnedStatus on Contact (after insert, after update) {
//I am assuming the API name of the field on the Contacts as Assets_Owned__c and the Status field as assets_Status__c
if(trigger.new[0].Assets_Owned__c !=null){

Assets_Owned__c assetsOwned = [select id, assets_status__c from assets_owned__c where id=:trigger.new[0].Assets_Owned__c];
assetsOwned.assets_status__c = 'Assigned';

update assetsOwned;
}

}

 

Hope this helps!

 

Regards

Sameer

All Answers

jungleeejungleee

Hi ,

 

Below solution is a simple one, this cannot be used for bulk records updates

 

trigger updateAssetsOwnedStatus on Contact (after insert, after update) {
//I am assuming the API name of the field on the Contacts as Assets_Owned__c and the Status field as assets_Status__c
if(trigger.new[0].Assets_Owned__c !=null){

Assets_Owned__c assetsOwned = [select id, assets_status__c from assets_owned__c where id=:trigger.new[0].Assets_Owned__c];
assetsOwned.assets_status__c = 'Assigned';

update assetsOwned;
}

}

 

Hope this helps!

 

Regards

Sameer

This was selected as the best answer
reema_agarwalreema_agarwal

Thanks a lot sameer it worked!!

reema_agarwalreema_agarwal

I also want to change the asset status as Instock when it is deleted from that particular contact or i update the contact with another asset.

 

jungleeejungleee

trigger updateAssetsOwnedStatus on Contact (after insert, after update) {
    //I am assuming the API name of the field on the Contacts as Assets_Owned__c and the Status field as assets_Status__c
    if(trigger.new[0].Assets_Owned__c !=null){

        Assets_Owned__c assetsOwned = [select id, assets_status__c from assets_owned__c where id=:trigger.new[0].Assets_Owned__c];
        assetsOwned.assets_status__c = 'Assigned';
        update assetsOwned;
    }

    if( trigger.isUpdate && trigger.old[0].assets_owned__c !=null && trigger.old[0].assets_owned__c != trigger.new[0].Assets_Owned__c){
        Assets_Owned__c assetsOwnedOld = [select id, assets_status__c from assets_owned__c where id=:trigger.old[0].Assets_Owned__c];
        assetsOwnedOld.assets_status__c = 'Instock';
        update assetsOwnedOld;
    }
}

 

 

I think this will work, Please try it out. Again this cannot be used for bulk records.

 

Regards

Sam

farah sheriffarah sherif
I want to ask 
how to create a trigger that updates the asset status to cancelled when the account is cancelled