Auto update record date

akin9
Tera Contributor

Hello Experts,

We have Contracts and under contract related lists "Assets covered" are there

created Before BR insert /update to update the start and end dates its working fine.

Before BR insert /update

Table - clm_m2m_contract_asset

 

Requirement

Same w want to achieve for the after update the dates needs to update automatically.

We have tried but no luck, kindly support..

 

(function executeRule(current, previous /*null when async*/) {
	var recordUpdated=false;
	var asset = new GlideRecord("alm_asset");
	asset.addQuery('sys_id',current.asset+"");
	asset.query();
	if (asset.next()){
		if (current.contract.starts!=""){
			asset.u_lease_contract_start=current.contract.starts+" 00:00:01";
		}
		if (current.contract.ends!=""){
			asset.u_lease_contract_end=current.contract.ends+" 23:59:59";
		}
		asset.u_lease_contract=true;
		asset.update();	
	}
})(current, previous);

 

1 ACCEPTED SOLUTION

Hi @akin9 

Oops. Just correct these line below. it should do the trick.

grAsset.u_lease_contract_start=current.starts+" 00:00:01";
grAsset.u_lease_contract_end=current.ends+" 23:59:59";

 

Cheers,

Tai Vu

View solution in original post

10 REPLIES 10

Hey @akin9 

I'm not sure if I conveyed my point clearly regarding your initial business rule. While it might work in term of functionality, it often leads to a critical problem known as recursive business rule.

 

The second business rule should be applied to the Contract [ast_contract] table.

Timi_0-1709793042641.png

 

Cheers,

Tai Vu

 

akin9
Tera Contributor

Hi @Tai Vu ,

I have tried below but its not working!

pls guide me if wrong.

 

akin9_1-1709796281329.png

akin9_2-1709796331952.png

 

 

 

Hi @akin9 

Oops. Just correct these line below. it should do the trick.

grAsset.u_lease_contract_start=current.starts+" 00:00:01";
grAsset.u_lease_contract_end=current.ends+" 23:59:59";

 

Cheers,

Tai Vu

akin9
Tera Contributor

Hi @Tai Vu 

Working fine thanks!

Maddysunil
Kilo Sage

@akin9 

Please try with below code in after update business also apply some log 

 

(function executeRule(current, previous /*null when async*/) {
    try {
        if (current.contract.starts.changes() || current.contract.ends.changes()) {
            var asset = new GlideRecord("alm_asset");
            asset.addQuery('sys_id', current.asset + "");
            asset.query();
            if (asset.next()) {
                if (current.contract.starts != "") {
                    asset.u_lease_contract_start = current.contract.starts + " 00:00:01";
                }
                if (current.contract.ends != "") {
                    asset.u_lease_contract_end = current.contract.ends + " 23:59:59";
                }
                asset.u_lease_contract = true;
                asset.update();
                gs.info("Updated asset record with lease contract dates");
            } else {
                gs.info("No asset record found for the given asset sys_id: " + current.asset);
            }
        }
    } catch (ex) {
        gs.error("An error occurred while updating asset record: " + ex);
    }
})(current, previous);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks