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
Nash12345Nash12345 

Create trigger to auto populate purchase item's supplier lookup field

Hi All,

I'm trying to create a...well the title says it basically.

The code I currently have doesn't work as I"m getting an "Error: Compile Error: Illegal assignment from List<Purchase_Order__c> to Id at line 7 column 13"

Here's the code:

trigger AutoPopAccountIdOnPi on Purchase_Item__c (before insert) {
    for (Purchase_Item__c PIInloop : Trigger.new) {
        IF(PIInloop.Supplier__c = null) {
            List<Purchase_Order__c> POlist = [SELECT Supplier__c FROM Purchase_Order__c WHERE Name = :PIInloop.Supplier__c];
            
            If(POlist.size() > 0){
            PIInloop.Supplier__c = POlist;
            } else { PIInloop.Supplier__c = null;
            }
   }
}
}


Any advice would be much appreciated.

Thanks
 
SandhyaSandhya (Salesforce Developers) 
Hi Nenad Atanasovski,

Please try this code hope it works, sorry there may be some typo errors.
trigger AutoPopAccountIdOnPi on Purchase_Item__c (before insert, before update) {

Set<id>ids=new set<id>();
list< Purchase_Order__c >polst=new list< Purchase_Order__c >();
	For(Purchase_Item__c PIInloop:trigger.new){
		if(PIInloop.Supplier__c!=Null){
			ids.add(PIInloop.Supplier__c); 
		}
	}
	if(!ids.Isempty()){
		list< Purchase_Order__c>po=[select id,name from Purchase_Order__c where Name=Null AND id in:ids];
		for(Purchase_Order__c op:po){
		 
			for(Purchase_Item__c p: trigger.new){
				op.name=p.id;
				polst.add(op);
			}
		}
		update oplst;
	}   
}

Please accept my solution as Best Answer if my answer was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.

Thanks and Regards
sandhya
 
 
 
Nash12345Nash12345

Hi Sandya, thanks for your quick reply.

Would you be able to describe what your code actually does? 

Reason why I'm asking is because there is still an error on line 15: Purchase_Order__c.Name is not writeable.

Just to add a bit more info:

The Supplier name will already be located in the purchase order. The supplier object is in fact account.

The relationship is as follows Account = Master to Purchase Order = Master to Purchase Item.

Hope this clarifies the situation a bit.

Nash12345Nash12345

Additional info:

I want the trigger to always activate whenever a new purchase item is created.

SandhyaSandhya (Salesforce Developers) 
Hi Nenad Atanasovski,

what I have assumed is 

Firstly you will create Purchase_Order__c then you create Purchase_Item__c when Purchase_Item__c is created you want this Purchase_Item__c name to be updated in the lookup field on Purchase_Order__c? 


Thanks and Regards
sandhya

 
Nash12345Nash12345

Correct

 

Thanks for looking at it again Sandhya

SandhyaSandhya (Salesforce Developers) 
Hi Nenad,
 What I have observed is an error on line 15: Purchase_Order__c.Name is not writeable.Is this Purchase_Order__c.Name field read-only for you?
 Want the trigger always to activate whenever a new purchase item is created----   you can just remove 'before update 'in the trigger.

Thanks and Regards
sandhya




 
Nash12345Nash12345
Purchase_Order.Name = Auto number