0

I am having a custom Object. And when i insert a record in that, i have to create an asset with the values Account,product and Serial Number from my Custom Object. I have wrote a trigger. But while testing the same, I am the error saying " Integrity error. Asset must have an Account Id and/or contact Id". pls help me on this.

Also, I have commented out the product assignment in the trigger because of the error " Illegal assignment from Id to Product 2" . Both sides of the = operator are of lookup(product) type fields. I am not sure , what should be write on that part too.

trigger create_asset on rstk__sowarr__c (after insert) { Asset [] ass = new Asset[]{}; for(rstk__sowarr__c siw : trigger.new){ Asset a = new Asset(); a.Name = siw.rstk__sowarr_srlno__c ; a.AccountId = siw.rstk__sowarr_soldtocust__r.rstk__socust_sf_account__c; // a.Product2 = siw.rstk__sowarr_soprod__r.rstk__soprod_sf_product__c; // I added a new field linkToSiwarr__c which is of type lookup relationship a.linkToSiwarr__c = siw.id; ass.add(a); } insert ass; }

1 Answer 1

1

I think the issue is account id should not be null, and instead of product2 field, you will have to use Product2Id field.

 trigger create_asset on rstk__sowarr__c (after insert) {
        Asset [] ass = new Asset[]{};
        for(rstk__sowarr__c siw : [Select id, rstk__sowarr_srlno__c, rstk__sowarr_soldtocust__r.rstk__socust_sf_account__c, siw.rstk__sowarr_soprod__r.rstk__soprod_sf_product__c, From rstk__sowarr__c Where Id in:trigger.new]){
           if(siw.rstk__sowarr_soldtocust__r.rstk__socust_sf_account__c != null 
                    && siw.rstk__sowarr_soprod__r.rstk__soprod_sf_product__c != null){
                Asset a = new Asset();
                    a.Name = siw.rstk__sowarr_srlno__c ;
                    a.AccountId = siw.rstk__sowarr_soldtocust__r.rstk__socust_sf_account__c;
                    a.Product2Id = siw.rstk__sowarr_soprod__r.rstk__soprod_sf_product__c;
                    a.linkToSiwarr__c = siw.id;
                    ass.add(a);
                }
           }
        if(ass != null && ass.size() > 0)  {  
            insert ass;
        }
    }
2
  • Yes Lakhan. There is a no error now. But still the asset is not getting created . I think the account is getting null. have checked the log. Any idea what is going wrong here. Thank you. Commented Dec 15, 2016 at 9:32
  • Hi @ReshmiSmiju, just noticed that you are referring some cross object fields. Unfortunately they are not available directly from trigger context variables. You will have to perform the SOQL with those fields.
    – LakhanP
    Commented Dec 15, 2016 at 11:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .